This source file includes following definitions.
- ffs_update
- ffs_truncate
- ffs_indirtrunc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mount.h>
38 #include <sys/proc.h>
39 #include <sys/file.h>
40 #include <sys/buf.h>
41 #include <sys/vnode.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/resourcevar.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #include <ufs/ufs/quota.h>
49 #include <ufs/ufs/inode.h>
50 #include <ufs/ufs/ufsmount.h>
51 #include <ufs/ufs/ufs_extern.h>
52
53 #include <ufs/ffs/fs.h>
54 #include <ufs/ffs/ffs_extern.h>
55
56 int ffs_indirtrunc(struct inode *, daddr_t, daddr_t, daddr_t, int, long *);
57
58
59
60
61
62
63
64
65
66
67 int
68 ffs_update(struct inode *ip, struct timespec *atime,
69 struct timespec *mtime, int waitfor)
70 {
71 struct vnode *vp;
72 struct fs *fs;
73 struct buf *bp;
74 int error;
75 struct timespec ts;
76
77 vp = ITOV(ip);
78 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
79 ip->i_flag &=
80 ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
81 return (0);
82 }
83
84 if ((vp->v_mount->mnt_flag & MNT_NOATIME) &&
85 !(ip->i_flag & (IN_CHANGE | IN_UPDATE))) {
86 ip->i_flag &= ~IN_ACCESS;
87 }
88
89 if ((ip->i_flag &
90 (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)) == 0 &&
91 waitfor != MNT_WAIT)
92 return (0);
93
94 getnanotime(&ts);
95
96 if (ip->i_flag & IN_ACCESS) {
97 DIP_ASSIGN(ip, atime, atime ? atime->tv_sec : ts.tv_sec);
98 DIP_ASSIGN(ip, atimensec, atime ? atime->tv_nsec : ts.tv_nsec);
99 }
100
101 if (ip->i_flag & IN_UPDATE) {
102 DIP_ASSIGN(ip, mtime, mtime ? mtime->tv_sec : ts.tv_sec);
103 DIP_ASSIGN(ip, mtimensec, mtime ? mtime->tv_nsec : ts.tv_nsec);
104 ip->i_modrev++;
105 }
106
107 if (ip->i_flag & IN_CHANGE) {
108 DIP_ASSIGN(ip, ctime, ts.tv_sec);
109 DIP_ASSIGN(ip, ctimensec, ts.tv_nsec);
110 }
111
112 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
113 fs = ip->i_fs;
114
115
116
117
118
119 if (fs->fs_magic == FS_UFS1_MAGIC && fs->fs_inodefmt < FS_44INODEFMT) {
120 ip->i_din1->di_ouid = ip->i_ffs1_uid;
121 ip->i_din1->di_ogid = ip->i_ffs1_gid;
122 }
123
124 error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
125 (int)fs->fs_bsize, NOCRED, &bp);
126 if (error) {
127 brelse(bp);
128 return (error);
129 }
130
131 if (DOINGSOFTDEP(vp))
132 softdep_update_inodeblock(ip, bp, waitfor);
133 else if (ip->i_effnlink != DIP(ip, nlink))
134 panic("ffs_update: bad link cnt");
135
136 #ifdef FFS2
137 if (ip->i_ump->um_fstype == UM_UFS2)
138 *((struct ufs2_dinode *)bp->b_data +
139 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
140 else
141 #endif
142 *((struct ufs1_dinode *)bp->b_data +
143 ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
144
145 if (waitfor && !DOINGASYNC(vp)) {
146 return (bwrite(bp));
147 } else {
148 bdwrite(bp);
149 return (0);
150 }
151 }
152
153 #define SINGLE 0
154 #define DOUBLE 1
155 #define TRIPLE 2
156
157
158
159
160
161 int
162 ffs_truncate(struct inode *oip, off_t length, int flags, struct ucred *cred)
163 {
164 struct vnode *ovp;
165 daddr64_t lastblock;
166 daddr64_t bn, lbn, lastiblock[NIADDR], indir_lbn[NIADDR];
167 daddr64_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
168 struct fs *fs;
169 struct buf *bp;
170 int offset, size, level;
171 long count, nblocks, vflags, blocksreleased = 0;
172 int i, aflags, error, allerror;
173 off_t osize;
174
175 if (length < 0)
176 return (EINVAL);
177 ovp = ITOV(oip);
178
179 if (ovp->v_type != VREG &&
180 ovp->v_type != VDIR &&
181 ovp->v_type != VLNK)
182 return (0);
183
184 if (DIP(oip, size) == length)
185 return (0);
186
187 if (ovp->v_type == VLNK &&
188 (DIP(oip, size) < ovp->v_mount->mnt_maxsymlinklen ||
189 (ovp->v_mount->mnt_maxsymlinklen == 0 &&
190 oip->i_din1->di_blocks == 0))) {
191 #ifdef DIAGNOSTIC
192 if (length != 0)
193 panic("ffs_truncate: partial truncate of symlink");
194 #endif
195 memset(SHORTLINK(oip), 0, (size_t) DIP(oip, size));
196 DIP_ASSIGN(oip, size, 0);
197 oip->i_flag |= IN_CHANGE | IN_UPDATE;
198 return (UFS_UPDATE(oip, MNT_WAIT));
199 }
200
201 if ((error = getinoquota(oip)) != 0)
202 return (error);
203
204 uvm_vnp_setsize(ovp, length);
205 oip->i_ci.ci_lasta = oip->i_ci.ci_clen
206 = oip->i_ci.ci_cstart = oip->i_ci.ci_lastw = 0;
207
208 if (DOINGSOFTDEP(ovp)) {
209 if (length > 0 || softdep_slowdown(ovp)) {
210
211
212
213
214
215
216
217
218
219 if ((error = VOP_FSYNC(ovp, cred, MNT_WAIT,
220 curproc)) != 0)
221 return (error);
222 } else {
223 (void)ufs_quota_free_blocks(oip, DIP(oip, blocks),
224 NOCRED);
225 softdep_setup_freeblocks(oip, length);
226 (void) vinvalbuf(ovp, 0, cred, curproc, 0, 0);
227 oip->i_flag |= IN_CHANGE | IN_UPDATE;
228 return (UFS_UPDATE(oip, 0));
229 }
230 }
231
232 fs = oip->i_fs;
233 osize = DIP(oip, size);
234
235
236
237
238
239 if (osize < length) {
240 if (length > fs->fs_maxfilesize)
241 return (EFBIG);
242 aflags = B_CLRBUF;
243 if (flags & IO_SYNC)
244 aflags |= B_SYNC;
245 error = UFS_BUF_ALLOC(oip, length - 1, 1,
246 cred, aflags, &bp);
247 if (error)
248 return (error);
249 DIP_ASSIGN(oip, size, length);
250 uvm_vnp_setsize(ovp, length);
251 (void) uvm_vnp_uncache(ovp);
252 if (aflags & B_SYNC)
253 bwrite(bp);
254 else
255 bawrite(bp);
256 oip->i_flag |= IN_CHANGE | IN_UPDATE;
257 return (UFS_UPDATE(oip, MNT_WAIT));
258 }
259 uvm_vnp_setsize(ovp, length);
260
261
262
263
264
265
266
267
268
269 offset = blkoff(fs, length);
270 if (offset == 0) {
271 DIP_ASSIGN(oip, size, length);
272 } else {
273 lbn = lblkno(fs, length);
274 aflags = B_CLRBUF;
275 if (flags & IO_SYNC)
276 aflags |= B_SYNC;
277 error = UFS_BUF_ALLOC(oip, length - 1, 1,
278 cred, aflags, &bp);
279 if (error)
280 return (error);
281
282
283
284
285
286
287
288
289 if (DOINGSOFTDEP(ovp) && lbn < NDADDR &&
290 fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
291 (error = VOP_FSYNC(ovp, cred, MNT_WAIT, curproc)) != 0)
292 return (error);
293 DIP_ASSIGN(oip, size, length);
294 size = blksize(fs, oip, lbn);
295 (void) uvm_vnp_uncache(ovp);
296 if (ovp->v_type != VDIR)
297 bzero((char *)bp->b_data + offset,
298 (u_int)(size - offset));
299 bp->b_bcount = size;
300 if (aflags & B_SYNC)
301 bwrite(bp);
302 else
303 bawrite(bp);
304 }
305
306
307
308
309
310
311 lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
312 lastiblock[SINGLE] = lastblock - NDADDR;
313 lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
314 lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
315 nblocks = btodb(fs->fs_bsize);
316
317
318
319
320
321
322
323 for (level = TRIPLE; level >= SINGLE; level--) {
324 oldblks[NDADDR + level] = DIP(oip, ib[level]);
325 if (lastiblock[level] < 0) {
326 DIP_ASSIGN(oip, ib[level], 0);
327 lastiblock[level] = -1;
328 }
329 }
330
331 for (i = 0; i < NDADDR; i++) {
332 oldblks[i] = DIP(oip, db[i]);
333 if (i > lastblock)
334 DIP_ASSIGN(oip, db[i], 0);
335 }
336
337 oip->i_flag |= IN_CHANGE | IN_UPDATE;
338 if ((error = UFS_UPDATE(oip, MNT_WAIT)) != 0)
339 allerror = error;
340
341
342
343
344
345
346
347 for (i = 0; i < NDADDR; i++) {
348 newblks[i] = DIP(oip, db[i]);
349 DIP_ASSIGN(oip, db[i], oldblks[i]);
350 }
351
352 for (i = 0; i < NIADDR; i++) {
353 newblks[NDADDR + i] = DIP(oip, ib[i]);
354 DIP_ASSIGN(oip, ib[i], oldblks[NDADDR + i]);
355 }
356
357 DIP_ASSIGN(oip, size, osize);
358 vflags = ((length > 0) ? V_SAVE : 0) | V_SAVEMETA;
359 allerror = vinvalbuf(ovp, vflags, cred, curproc, 0, 0);
360
361
362
363
364 indir_lbn[SINGLE] = -NDADDR;
365 indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
366 indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
367 for (level = TRIPLE; level >= SINGLE; level--) {
368 bn = DIP(oip, ib[level]);
369 if (bn != 0) {
370 error = ffs_indirtrunc(oip, indir_lbn[level],
371 fsbtodb(fs, bn), lastiblock[level], level, &count);
372 if (error)
373 allerror = error;
374 blocksreleased += count;
375 if (lastiblock[level] < 0) {
376 DIP_ASSIGN(oip, ib[level], 0);
377 ffs_blkfree(oip, bn, fs->fs_bsize);
378 blocksreleased += nblocks;
379 }
380 }
381 if (lastiblock[level] >= 0)
382 goto done;
383 }
384
385
386
387
388 for (i = NDADDR - 1; i > lastblock; i--) {
389 long bsize;
390
391 bn = DIP(oip, db[i]);
392 if (bn == 0)
393 continue;
394
395 DIP_ASSIGN(oip, db[i], 0);
396 bsize = blksize(fs, oip, i);
397 ffs_blkfree(oip, bn, bsize);
398 blocksreleased += btodb(bsize);
399 }
400 if (lastblock < 0)
401 goto done;
402
403
404
405
406
407 bn = DIP(oip, db[lastblock]);
408 if (bn != 0) {
409 long oldspace, newspace;
410
411
412
413
414
415 oldspace = blksize(fs, oip, lastblock);
416 DIP_ASSIGN(oip, size, length);
417 newspace = blksize(fs, oip, lastblock);
418 if (newspace == 0)
419 panic("ffs_truncate: newspace");
420 if (oldspace - newspace > 0) {
421
422
423
424
425
426 bn += numfrags(fs, newspace);
427 ffs_blkfree(oip, bn, oldspace - newspace);
428 blocksreleased += btodb(oldspace - newspace);
429 }
430 }
431 done:
432 #ifdef DIAGNOSTIC
433 for (level = SINGLE; level <= TRIPLE; level++)
434 if (newblks[NDADDR + level] != DIP(oip, ib[level]))
435 panic("ffs_truncate1");
436 for (i = 0; i < NDADDR; i++)
437 if (newblks[i] != DIP(oip, db[i]))
438 panic("ffs_truncate2");
439 #endif
440
441
442
443 DIP_ASSIGN(oip, size, length);
444 DIP_ADD(oip, blocks, -blocksreleased);
445 if (DIP(oip, blocks) < 0)
446 DIP_ASSIGN(oip, blocks, 0);
447 oip->i_flag |= IN_CHANGE;
448 (void)ufs_quota_free_blocks(oip, blocksreleased, NOCRED);
449 return (allerror);
450 }
451
452 #ifdef FFS2
453 #define BAP(ip, i) (((ip)->i_ump->um_fstype == UM_UFS2) ? bap2[i] : bap1[i])
454 #else
455 #define BAP(ip, i) bap1[i]
456 #endif
457
458
459
460
461
462
463
464
465
466
467 int
468 ffs_indirtrunc(struct inode *ip, daddr_t lbn, daddr_t dbn, daddr_t lastbn,
469 int level, long *countp)
470 {
471 int i;
472 struct buf *bp;
473 struct fs *fs = ip->i_fs;
474 struct vnode *vp;
475 void *copy = NULL;
476 daddr_t nb, nlbn, last;
477 long blkcount, factor;
478 int nblocks, blocksreleased = 0;
479 int error = 0, allerror = 0;
480 int32_t *bap1 = NULL;
481 #ifdef FFS2
482 int64_t *bap2 = NULL;
483 #endif
484
485
486
487
488
489
490 factor = 1;
491 for (i = SINGLE; i < level; i++)
492 factor *= NINDIR(fs);
493 last = lastbn;
494 if (lastbn > 0)
495 last /= factor;
496 nblocks = btodb(fs->fs_bsize);
497
498
499
500
501
502
503
504
505 vp = ITOV(ip);
506 bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0);
507 if (!(bp->b_flags & (B_DONE | B_DELWRI))) {
508 curproc->p_stats->p_ru.ru_inblock++;
509 bp->b_flags |= B_READ;
510 if (bp->b_bcount > bp->b_bufsize)
511 panic("ffs_indirtrunc: bad buffer size");
512 bp->b_blkno = dbn;
513 VOP_STRATEGY(bp);
514 error = biowait(bp);
515 }
516 if (error) {
517 brelse(bp);
518 *countp = 0;
519 return (error);
520 }
521
522 #ifdef FFS2
523 if (ip->i_ump->um_fstype == UM_UFS2)
524 bap2 = (int64_t *)bp->b_data;
525 else
526 #endif
527 bap1 = (int32_t *)bp->b_data;
528
529 if (lastbn != -1) {
530 MALLOC(copy, void *, fs->fs_bsize, M_TEMP, M_WAITOK);
531 bcopy(bp->b_data, copy, (u_int) fs->fs_bsize);
532
533 for (i = last + 1; i < NINDIR(fs); i++)
534 BAP(ip, i) = 0;
535
536 if (!DOINGASYNC(vp)) {
537 error = bwrite(bp);
538 if (error)
539 allerror = error;
540 } else {
541 bawrite(bp);
542 }
543
544 #ifdef FFS2
545 if (ip->i_ump->um_fstype == UM_UFS2)
546 bap2 = (int64_t *)copy;
547 else
548 #endif
549 bap1 = (int32_t *)copy;
550 }
551
552
553
554
555 for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
556 i--, nlbn += factor) {
557 nb = BAP(ip, i);
558 if (nb == 0)
559 continue;
560 if (level > SINGLE) {
561 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
562 (daddr_t)-1, level - 1,
563 &blkcount);
564 if (error)
565 allerror = error;
566 blocksreleased += blkcount;
567 }
568 ffs_blkfree(ip, nb, fs->fs_bsize);
569 blocksreleased += nblocks;
570 }
571
572
573
574
575 if (level > SINGLE && lastbn >= 0) {
576 last = lastbn % factor;
577 nb = BAP(ip, i);
578 if (nb != 0) {
579 error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
580 last, level - 1, &blkcount);
581 if (error)
582 allerror = error;
583 blocksreleased += blkcount;
584 }
585 }
586 if (copy != NULL) {
587 FREE(copy, M_TEMP);
588 } else {
589 bp->b_flags |= B_INVAL;
590 brelse(bp);
591 }
592
593 *countp = blocksreleased;
594 return (allerror);
595 }