Merge git://git.kernel.org/pub/scm/linux/kernel/git/hskinnemoen/avr32-2.6
[deliverable/linux.git] / fs / xfs / xfs_vnodeops.c
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_types.h"
22 #include "xfs_bit.h"
23 #include "xfs_log.h"
24 #include "xfs_inum.h"
25 #include "xfs_trans.h"
26 #include "xfs_sb.h"
27 #include "xfs_ag.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
44 #include "xfs_bmap.h"
45 #include "xfs_acl.h"
46 #include "xfs_attr.h"
47 #include "xfs_rw.h"
48 #include "xfs_error.h"
49 #include "xfs_quota.h"
50 #include "xfs_utils.h"
51 #include "xfs_rtalloc.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_log_priv.h"
54 #include "xfs_filestream.h"
55 #include "xfs_vnodeops.h"
56
57 int
58 xfs_setattr(
59 struct xfs_inode *ip,
60 struct iattr *iattr,
61 int flags)
62 {
63 xfs_mount_t *mp = ip->i_mount;
64 struct inode *inode = VFS_I(ip);
65 int mask = iattr->ia_valid;
66 xfs_trans_t *tp;
67 int code;
68 uint lock_flags;
69 uint commit_flags=0;
70 uid_t uid=0, iuid=0;
71 gid_t gid=0, igid=0;
72 int timeflags = 0;
73 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
74 int need_iolock = 1;
75
76 xfs_itrace_entry(ip);
77
78 if (mp->m_flags & XFS_MOUNT_RDONLY)
79 return XFS_ERROR(EROFS);
80
81 if (XFS_FORCED_SHUTDOWN(mp))
82 return XFS_ERROR(EIO);
83
84 code = -inode_change_ok(inode, iattr);
85 if (code)
86 return code;
87
88 olddquot1 = olddquot2 = NULL;
89 udqp = gdqp = NULL;
90
91 /*
92 * If disk quotas is on, we make sure that the dquots do exist on disk,
93 * before we start any other transactions. Trying to do this later
94 * is messy. We don't care to take a readlock to look at the ids
95 * in inode here, because we can't hold it across the trans_reserve.
96 * If the IDs do change before we take the ilock, we're covered
97 * because the i_*dquot fields will get updated anyway.
98 */
99 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
100 uint qflags = 0;
101
102 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
103 uid = iattr->ia_uid;
104 qflags |= XFS_QMOPT_UQUOTA;
105 } else {
106 uid = ip->i_d.di_uid;
107 }
108 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
109 gid = iattr->ia_gid;
110 qflags |= XFS_QMOPT_GQUOTA;
111 } else {
112 gid = ip->i_d.di_gid;
113 }
114
115 /*
116 * We take a reference when we initialize udqp and gdqp,
117 * so it is important that we never blindly double trip on
118 * the same variable. See xfs_create() for an example.
119 */
120 ASSERT(udqp == NULL);
121 ASSERT(gdqp == NULL);
122 code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
123 qflags, &udqp, &gdqp);
124 if (code)
125 return code;
126 }
127
128 /*
129 * For the other attributes, we acquire the inode lock and
130 * first do an error checking pass.
131 */
132 tp = NULL;
133 lock_flags = XFS_ILOCK_EXCL;
134 if (flags & XFS_ATTR_NOLOCK)
135 need_iolock = 0;
136 if (!(mask & ATTR_SIZE)) {
137 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
138 (mp->m_flags & XFS_MOUNT_WSYNC)) {
139 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
140 commit_flags = 0;
141 if ((code = xfs_trans_reserve(tp, 0,
142 XFS_ICHANGE_LOG_RES(mp), 0,
143 0, 0))) {
144 lock_flags = 0;
145 goto error_return;
146 }
147 }
148 } else {
149 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
150 !(flags & XFS_ATTR_DMI)) {
151 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
152 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
153 iattr->ia_size, 0, dmflags, NULL);
154 if (code) {
155 lock_flags = 0;
156 goto error_return;
157 }
158 }
159 if (need_iolock)
160 lock_flags |= XFS_IOLOCK_EXCL;
161 }
162
163 xfs_ilock(ip, lock_flags);
164
165 /*
166 * Change file ownership. Must be the owner or privileged.
167 */
168 if (mask & (ATTR_UID|ATTR_GID)) {
169 /*
170 * These IDs could have changed since we last looked at them.
171 * But, we're assured that if the ownership did change
172 * while we didn't have the inode locked, inode's dquot(s)
173 * would have changed also.
174 */
175 iuid = ip->i_d.di_uid;
176 igid = ip->i_d.di_gid;
177 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
178 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
179
180 /*
181 * Do a quota reservation only if uid/gid is actually
182 * going to change.
183 */
184 if (XFS_IS_QUOTA_RUNNING(mp) &&
185 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
186 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
187 ASSERT(tp);
188 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
189 capable(CAP_FOWNER) ?
190 XFS_QMOPT_FORCE_RES : 0);
191 if (code) /* out of quota */
192 goto error_return;
193 }
194 }
195
196 /*
197 * Truncate file. Must have write permission and not be a directory.
198 */
199 if (mask & ATTR_SIZE) {
200 /* Short circuit the truncate case for zero length files */
201 if (iattr->ia_size == 0 &&
202 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
203 xfs_iunlock(ip, XFS_ILOCK_EXCL);
204 lock_flags &= ~XFS_ILOCK_EXCL;
205 if (mask & ATTR_CTIME)
206 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
207 code = 0;
208 goto error_return;
209 }
210
211 if (S_ISDIR(ip->i_d.di_mode)) {
212 code = XFS_ERROR(EISDIR);
213 goto error_return;
214 } else if (!S_ISREG(ip->i_d.di_mode)) {
215 code = XFS_ERROR(EINVAL);
216 goto error_return;
217 }
218
219 /*
220 * Make sure that the dquots are attached to the inode.
221 */
222 code = xfs_qm_dqattach_locked(ip, 0);
223 if (code)
224 goto error_return;
225
226 /*
227 * Now we can make the changes. Before we join the inode
228 * to the transaction, if ATTR_SIZE is set then take care of
229 * the part of the truncation that must be done without the
230 * inode lock. This needs to be done before joining the inode
231 * to the transaction, because the inode cannot be unlocked
232 * once it is a part of the transaction.
233 */
234 if (iattr->ia_size > ip->i_size) {
235 /*
236 * Do the first part of growing a file: zero any data
237 * in the last block that is beyond the old EOF. We
238 * need to do this before the inode is joined to the
239 * transaction to modify the i_size.
240 */
241 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
242 }
243 xfs_iunlock(ip, XFS_ILOCK_EXCL);
244
245 /*
246 * We are going to log the inode size change in this
247 * transaction so any previous writes that are beyond the on
248 * disk EOF and the new EOF that have not been written out need
249 * to be written here. If we do not write the data out, we
250 * expose ourselves to the null files problem.
251 *
252 * Only flush from the on disk size to the smaller of the in
253 * memory file size or the new size as that's the range we
254 * really care about here and prevents waiting for other data
255 * not within the range we care about here.
256 */
257 if (!code &&
258 ip->i_size != ip->i_d.di_size &&
259 iattr->ia_size > ip->i_d.di_size) {
260 code = xfs_flush_pages(ip,
261 ip->i_d.di_size, iattr->ia_size,
262 XFS_B_ASYNC, FI_NONE);
263 }
264
265 /* wait for all I/O to complete */
266 xfs_ioend_wait(ip);
267
268 if (!code)
269 code = xfs_itruncate_data(ip, iattr->ia_size);
270 if (code) {
271 ASSERT(tp == NULL);
272 lock_flags &= ~XFS_ILOCK_EXCL;
273 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
274 goto error_return;
275 }
276 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
277 if ((code = xfs_trans_reserve(tp, 0,
278 XFS_ITRUNCATE_LOG_RES(mp), 0,
279 XFS_TRANS_PERM_LOG_RES,
280 XFS_ITRUNCATE_LOG_COUNT))) {
281 xfs_trans_cancel(tp, 0);
282 if (need_iolock)
283 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
284 return code;
285 }
286 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
287 xfs_ilock(ip, XFS_ILOCK_EXCL);
288
289 xfs_trans_ijoin(tp, ip, lock_flags);
290 xfs_trans_ihold(tp, ip);
291
292 /*
293 * Only change the c/mtime if we are changing the size
294 * or we are explicitly asked to change it. This handles
295 * the semantic difference between truncate() and ftruncate()
296 * as implemented in the VFS.
297 */
298 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
299 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
300
301 if (iattr->ia_size > ip->i_size) {
302 ip->i_d.di_size = iattr->ia_size;
303 ip->i_size = iattr->ia_size;
304 if (!(flags & XFS_ATTR_DMI))
305 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
306 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
307 } else if (iattr->ia_size <= ip->i_size ||
308 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
309 /*
310 * signal a sync transaction unless
311 * we're truncating an already unlinked
312 * file on a wsync filesystem
313 */
314 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
315 XFS_DATA_FORK,
316 ((ip->i_d.di_nlink != 0 ||
317 !(mp->m_flags & XFS_MOUNT_WSYNC))
318 ? 1 : 0));
319 if (code)
320 goto abort_return;
321 /*
322 * Truncated "down", so we're removing references
323 * to old data here - if we now delay flushing for
324 * a long time, we expose ourselves unduly to the
325 * notorious NULL files problem. So, we mark this
326 * vnode and flush it when the file is closed, and
327 * do not wait the usual (long) time for writeout.
328 */
329 xfs_iflags_set(ip, XFS_ITRUNCATED);
330 }
331 } else if (tp) {
332 xfs_trans_ijoin(tp, ip, lock_flags);
333 xfs_trans_ihold(tp, ip);
334 }
335
336 /*
337 * Change file ownership. Must be the owner or privileged.
338 */
339 if (mask & (ATTR_UID|ATTR_GID)) {
340 /*
341 * CAP_FSETID overrides the following restrictions:
342 *
343 * The set-user-ID and set-group-ID bits of a file will be
344 * cleared upon successful return from chown()
345 */
346 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
347 !capable(CAP_FSETID)) {
348 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
349 }
350
351 /*
352 * Change the ownerships and register quota modifications
353 * in the transaction.
354 */
355 if (iuid != uid) {
356 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
357 ASSERT(mask & ATTR_UID);
358 ASSERT(udqp);
359 olddquot1 = xfs_qm_vop_chown(tp, ip,
360 &ip->i_udquot, udqp);
361 }
362 ip->i_d.di_uid = uid;
363 inode->i_uid = uid;
364 }
365 if (igid != gid) {
366 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
367 ASSERT(!XFS_IS_PQUOTA_ON(mp));
368 ASSERT(mask & ATTR_GID);
369 ASSERT(gdqp);
370 olddquot2 = xfs_qm_vop_chown(tp, ip,
371 &ip->i_gdquot, gdqp);
372 }
373 ip->i_d.di_gid = gid;
374 inode->i_gid = gid;
375 }
376
377 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
378 timeflags |= XFS_ICHGTIME_CHG;
379 }
380
381 /*
382 * Change file access modes.
383 */
384 if (mask & ATTR_MODE) {
385 umode_t mode = iattr->ia_mode;
386
387 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
388 mode &= ~S_ISGID;
389
390 ip->i_d.di_mode &= S_IFMT;
391 ip->i_d.di_mode |= mode & ~S_IFMT;
392
393 inode->i_mode &= S_IFMT;
394 inode->i_mode |= mode & ~S_IFMT;
395
396 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
397 timeflags |= XFS_ICHGTIME_CHG;
398 }
399
400 /*
401 * Change file access or modified times.
402 */
403 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
404 if (mask & ATTR_ATIME) {
405 inode->i_atime = iattr->ia_atime;
406 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
407 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
408 ip->i_update_core = 1;
409 }
410 if (mask & ATTR_MTIME) {
411 inode->i_mtime = iattr->ia_mtime;
412 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
413 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
414 timeflags &= ~XFS_ICHGTIME_MOD;
415 timeflags |= XFS_ICHGTIME_CHG;
416 }
417 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
418 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
419 }
420
421 /*
422 * Change file inode change time only if ATTR_CTIME set
423 * AND we have been called by a DMI function.
424 */
425
426 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
427 inode->i_ctime = iattr->ia_ctime;
428 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
429 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
430 ip->i_update_core = 1;
431 timeflags &= ~XFS_ICHGTIME_CHG;
432 }
433
434 /*
435 * Send out timestamp changes that need to be set to the
436 * current time. Not done when called by a DMI function.
437 */
438 if (timeflags && !(flags & XFS_ATTR_DMI))
439 xfs_ichgtime(ip, timeflags);
440
441 XFS_STATS_INC(xs_ig_attrchg);
442
443 /*
444 * If this is a synchronous mount, make sure that the
445 * transaction goes to disk before returning to the user.
446 * This is slightly sub-optimal in that truncates require
447 * two sync transactions instead of one for wsync filesystems.
448 * One for the truncate and one for the timestamps since we
449 * don't want to change the timestamps unless we're sure the
450 * truncate worked. Truncates are less than 1% of the laddis
451 * mix so this probably isn't worth the trouble to optimize.
452 */
453 code = 0;
454 if (tp) {
455 if (mp->m_flags & XFS_MOUNT_WSYNC)
456 xfs_trans_set_sync(tp);
457
458 code = xfs_trans_commit(tp, commit_flags);
459 }
460
461 xfs_iunlock(ip, lock_flags);
462
463 /*
464 * Release any dquot(s) the inode had kept before chown.
465 */
466 xfs_qm_dqrele(olddquot1);
467 xfs_qm_dqrele(olddquot2);
468 xfs_qm_dqrele(udqp);
469 xfs_qm_dqrele(gdqp);
470
471 if (code)
472 return code;
473
474 /*
475 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
476 * update. We could avoid this with linked transactions
477 * and passing down the transaction pointer all the way
478 * to attr_set. No previous user of the generic
479 * Posix ACL code seems to care about this issue either.
480 */
481 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
482 code = -xfs_acl_chmod(inode);
483 if (code)
484 return XFS_ERROR(code);
485 }
486
487 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
488 !(flags & XFS_ATTR_DMI)) {
489 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
490 NULL, DM_RIGHT_NULL, NULL, NULL,
491 0, 0, AT_DELAY_FLAG(flags));
492 }
493 return 0;
494
495 abort_return:
496 commit_flags |= XFS_TRANS_ABORT;
497 /* FALLTHROUGH */
498 error_return:
499 xfs_qm_dqrele(udqp);
500 xfs_qm_dqrele(gdqp);
501 if (tp) {
502 xfs_trans_cancel(tp, commit_flags);
503 }
504 if (lock_flags != 0) {
505 xfs_iunlock(ip, lock_flags);
506 }
507 return code;
508 }
509
510 /*
511 * The maximum pathlen is 1024 bytes. Since the minimum file system
512 * blocksize is 512 bytes, we can get a max of 2 extents back from
513 * bmapi.
514 */
515 #define SYMLINK_MAPS 2
516
517 STATIC int
518 xfs_readlink_bmap(
519 xfs_inode_t *ip,
520 char *link)
521 {
522 xfs_mount_t *mp = ip->i_mount;
523 int pathlen = ip->i_d.di_size;
524 int nmaps = SYMLINK_MAPS;
525 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
526 xfs_daddr_t d;
527 int byte_cnt;
528 int n;
529 xfs_buf_t *bp;
530 int error = 0;
531
532 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
533 mval, &nmaps, NULL, NULL);
534 if (error)
535 goto out;
536
537 for (n = 0; n < nmaps; n++) {
538 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
539 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
540
541 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
542 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
543 error = XFS_BUF_GETERROR(bp);
544 if (error) {
545 xfs_ioerror_alert("xfs_readlink",
546 ip->i_mount, bp, XFS_BUF_ADDR(bp));
547 xfs_buf_relse(bp);
548 goto out;
549 }
550 if (pathlen < byte_cnt)
551 byte_cnt = pathlen;
552 pathlen -= byte_cnt;
553
554 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
555 xfs_buf_relse(bp);
556 }
557
558 link[ip->i_d.di_size] = '\0';
559 error = 0;
560
561 out:
562 return error;
563 }
564
565 int
566 xfs_readlink(
567 xfs_inode_t *ip,
568 char *link)
569 {
570 xfs_mount_t *mp = ip->i_mount;
571 int pathlen;
572 int error = 0;
573
574 xfs_itrace_entry(ip);
575
576 if (XFS_FORCED_SHUTDOWN(mp))
577 return XFS_ERROR(EIO);
578
579 xfs_ilock(ip, XFS_ILOCK_SHARED);
580
581 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
582 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
583
584 pathlen = ip->i_d.di_size;
585 if (!pathlen)
586 goto out;
587
588 if (ip->i_df.if_flags & XFS_IFINLINE) {
589 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
590 link[pathlen] = '\0';
591 } else {
592 error = xfs_readlink_bmap(ip, link);
593 }
594
595 out:
596 xfs_iunlock(ip, XFS_ILOCK_SHARED);
597 return error;
598 }
599
600 /*
601 * xfs_fsync
602 *
603 * This is called to sync the inode and its data out to disk. We need to hold
604 * the I/O lock while flushing the data, and the inode lock while flushing the
605 * inode. The inode lock CANNOT be held while flushing the data, so acquire
606 * after we're done with that.
607 */
608 int
609 xfs_fsync(
610 xfs_inode_t *ip)
611 {
612 xfs_trans_t *tp;
613 int error = 0;
614 int log_flushed = 0, changed = 1;
615
616 xfs_itrace_entry(ip);
617
618 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
619 return XFS_ERROR(EIO);
620
621 /*
622 * We always need to make sure that the required inode state is safe on
623 * disk. The inode might be clean but we still might need to force the
624 * log because of committed transactions that haven't hit the disk yet.
625 * Likewise, there could be unflushed non-transactional changes to the
626 * inode core that have to go to disk and this requires us to issue
627 * a synchronous transaction to capture these changes correctly.
628 *
629 * This code relies on the assumption that if the update_* fields
630 * of the inode are clear and the inode is unpinned then it is clean
631 * and no action is required.
632 */
633 xfs_ilock(ip, XFS_ILOCK_SHARED);
634
635 if (!ip->i_update_core) {
636 /*
637 * Timestamps/size haven't changed since last inode flush or
638 * inode transaction commit. That means either nothing got
639 * written or a transaction committed which caught the updates.
640 * If the latter happened and the transaction hasn't hit the
641 * disk yet, the inode will be still be pinned. If it is,
642 * force the log.
643 */
644
645 xfs_iunlock(ip, XFS_ILOCK_SHARED);
646
647 if (xfs_ipincount(ip)) {
648 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
649 XFS_LOG_FORCE | XFS_LOG_SYNC,
650 &log_flushed);
651 } else {
652 /*
653 * If the inode is not pinned and nothing has changed
654 * we don't need to flush the cache.
655 */
656 changed = 0;
657 }
658 } else {
659 /*
660 * Kick off a transaction to log the inode core to get the
661 * updates. The sync transaction will also force the log.
662 */
663 xfs_iunlock(ip, XFS_ILOCK_SHARED);
664 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
665 error = xfs_trans_reserve(tp, 0,
666 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
667 if (error) {
668 xfs_trans_cancel(tp, 0);
669 return error;
670 }
671 xfs_ilock(ip, XFS_ILOCK_EXCL);
672
673 /*
674 * Note - it's possible that we might have pushed ourselves out
675 * of the way during trans_reserve which would flush the inode.
676 * But there's no guarantee that the inode buffer has actually
677 * gone out yet (it's delwri). Plus the buffer could be pinned
678 * anyway if it's part of an inode in another recent
679 * transaction. So we play it safe and fire off the
680 * transaction anyway.
681 */
682 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
683 xfs_trans_ihold(tp, ip);
684 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
685 xfs_trans_set_sync(tp);
686 error = _xfs_trans_commit(tp, 0, &log_flushed);
687
688 xfs_iunlock(ip, XFS_ILOCK_EXCL);
689 }
690
691 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
692 /*
693 * If the log write didn't issue an ordered tag we need
694 * to flush the disk cache for the data device now.
695 */
696 if (!log_flushed)
697 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
698
699 /*
700 * If this inode is on the RT dev we need to flush that
701 * cache as well.
702 */
703 if (XFS_IS_REALTIME_INODE(ip))
704 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
705 }
706
707 return error;
708 }
709
710 /*
711 * Flags for xfs_free_eofblocks
712 */
713 #define XFS_FREE_EOF_TRYLOCK (1<<0)
714
715 /*
716 * This is called by xfs_inactive to free any blocks beyond eof
717 * when the link count isn't zero and by xfs_dm_punch_hole() when
718 * punching a hole to EOF.
719 */
720 STATIC int
721 xfs_free_eofblocks(
722 xfs_mount_t *mp,
723 xfs_inode_t *ip,
724 int flags)
725 {
726 xfs_trans_t *tp;
727 int error;
728 xfs_fileoff_t end_fsb;
729 xfs_fileoff_t last_fsb;
730 xfs_filblks_t map_len;
731 int nimaps;
732 xfs_bmbt_irec_t imap;
733
734 /*
735 * Figure out if there are any blocks beyond the end
736 * of the file. If not, then there is nothing to do.
737 */
738 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
739 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
740 map_len = last_fsb - end_fsb;
741 if (map_len <= 0)
742 return 0;
743
744 nimaps = 1;
745 xfs_ilock(ip, XFS_ILOCK_SHARED);
746 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
747 NULL, 0, &imap, &nimaps, NULL, NULL);
748 xfs_iunlock(ip, XFS_ILOCK_SHARED);
749
750 if (!error && (nimaps != 0) &&
751 (imap.br_startblock != HOLESTARTBLOCK ||
752 ip->i_delayed_blks)) {
753 /*
754 * Attach the dquots to the inode up front.
755 */
756 error = xfs_qm_dqattach(ip, 0);
757 if (error)
758 return error;
759
760 /*
761 * There are blocks after the end of file.
762 * Free them up now by truncating the file to
763 * its current size.
764 */
765 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
766
767 /*
768 * Do the xfs_itruncate_start() call before
769 * reserving any log space because
770 * itruncate_start will call into the buffer
771 * cache and we can't
772 * do that within a transaction.
773 */
774 if (flags & XFS_FREE_EOF_TRYLOCK) {
775 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
776 xfs_trans_cancel(tp, 0);
777 return 0;
778 }
779 } else {
780 xfs_ilock(ip, XFS_IOLOCK_EXCL);
781 }
782 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
783 ip->i_size);
784 if (error) {
785 xfs_trans_cancel(tp, 0);
786 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
787 return error;
788 }
789
790 error = xfs_trans_reserve(tp, 0,
791 XFS_ITRUNCATE_LOG_RES(mp),
792 0, XFS_TRANS_PERM_LOG_RES,
793 XFS_ITRUNCATE_LOG_COUNT);
794 if (error) {
795 ASSERT(XFS_FORCED_SHUTDOWN(mp));
796 xfs_trans_cancel(tp, 0);
797 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
798 return error;
799 }
800
801 xfs_ilock(ip, XFS_ILOCK_EXCL);
802 xfs_trans_ijoin(tp, ip,
803 XFS_IOLOCK_EXCL |
804 XFS_ILOCK_EXCL);
805 xfs_trans_ihold(tp, ip);
806
807 error = xfs_itruncate_finish(&tp, ip,
808 ip->i_size,
809 XFS_DATA_FORK,
810 0);
811 /*
812 * If we get an error at this point we
813 * simply don't bother truncating the file.
814 */
815 if (error) {
816 xfs_trans_cancel(tp,
817 (XFS_TRANS_RELEASE_LOG_RES |
818 XFS_TRANS_ABORT));
819 } else {
820 error = xfs_trans_commit(tp,
821 XFS_TRANS_RELEASE_LOG_RES);
822 }
823 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
824 }
825 return error;
826 }
827
828 /*
829 * Free a symlink that has blocks associated with it.
830 */
831 STATIC int
832 xfs_inactive_symlink_rmt(
833 xfs_inode_t *ip,
834 xfs_trans_t **tpp)
835 {
836 xfs_buf_t *bp;
837 int committed;
838 int done;
839 int error;
840 xfs_fsblock_t first_block;
841 xfs_bmap_free_t free_list;
842 int i;
843 xfs_mount_t *mp;
844 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
845 int nmaps;
846 xfs_trans_t *ntp;
847 int size;
848 xfs_trans_t *tp;
849
850 tp = *tpp;
851 mp = ip->i_mount;
852 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
853 /*
854 * We're freeing a symlink that has some
855 * blocks allocated to it. Free the
856 * blocks here. We know that we've got
857 * either 1 or 2 extents and that we can
858 * free them all in one bunmapi call.
859 */
860 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
861 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
862 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
863 ASSERT(XFS_FORCED_SHUTDOWN(mp));
864 xfs_trans_cancel(tp, 0);
865 *tpp = NULL;
866 return error;
867 }
868 /*
869 * Lock the inode, fix the size, and join it to the transaction.
870 * Hold it so in the normal path, we still have it locked for
871 * the second transaction. In the error paths we need it
872 * held so the cancel won't rele it, see below.
873 */
874 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
875 size = (int)ip->i_d.di_size;
876 ip->i_d.di_size = 0;
877 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
878 xfs_trans_ihold(tp, ip);
879 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
880 /*
881 * Find the block(s) so we can inval and unmap them.
882 */
883 done = 0;
884 xfs_bmap_init(&free_list, &first_block);
885 nmaps = ARRAY_SIZE(mval);
886 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
887 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
888 &free_list, NULL)))
889 goto error0;
890 /*
891 * Invalidate the block(s).
892 */
893 for (i = 0; i < nmaps; i++) {
894 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
895 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
896 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
897 xfs_trans_binval(tp, bp);
898 }
899 /*
900 * Unmap the dead block(s) to the free_list.
901 */
902 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
903 &first_block, &free_list, NULL, &done)))
904 goto error1;
905 ASSERT(done);
906 /*
907 * Commit the first transaction. This logs the EFI and the inode.
908 */
909 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
910 goto error1;
911 /*
912 * The transaction must have been committed, since there were
913 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
914 * The new tp has the extent freeing and EFDs.
915 */
916 ASSERT(committed);
917 /*
918 * The first xact was committed, so add the inode to the new one.
919 * Mark it dirty so it will be logged and moved forward in the log as
920 * part of every commit.
921 */
922 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
923 xfs_trans_ihold(tp, ip);
924 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
925 /*
926 * Get a new, empty transaction to return to our caller.
927 */
928 ntp = xfs_trans_dup(tp);
929 /*
930 * Commit the transaction containing extent freeing and EFDs.
931 * If we get an error on the commit here or on the reserve below,
932 * we need to unlock the inode since the new transaction doesn't
933 * have the inode attached.
934 */
935 error = xfs_trans_commit(tp, 0);
936 tp = ntp;
937 if (error) {
938 ASSERT(XFS_FORCED_SHUTDOWN(mp));
939 goto error0;
940 }
941 /*
942 * transaction commit worked ok so we can drop the extra ticket
943 * reference that we gained in xfs_trans_dup()
944 */
945 xfs_log_ticket_put(tp->t_ticket);
946
947 /*
948 * Remove the memory for extent descriptions (just bookkeeping).
949 */
950 if (ip->i_df.if_bytes)
951 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
952 ASSERT(ip->i_df.if_bytes == 0);
953 /*
954 * Put an itruncate log reservation in the new transaction
955 * for our caller.
956 */
957 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
958 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
959 ASSERT(XFS_FORCED_SHUTDOWN(mp));
960 goto error0;
961 }
962 /*
963 * Return with the inode locked but not joined to the transaction.
964 */
965 *tpp = tp;
966 return 0;
967
968 error1:
969 xfs_bmap_cancel(&free_list);
970 error0:
971 /*
972 * Have to come here with the inode locked and either
973 * (held and in the transaction) or (not in the transaction).
974 * If the inode isn't held then cancel would iput it, but
975 * that's wrong since this is inactive and the vnode ref
976 * count is 0 already.
977 * Cancel won't do anything to the inode if held, but it still
978 * needs to be locked until the cancel is done, if it was
979 * joined to the transaction.
980 */
981 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
982 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
983 *tpp = NULL;
984 return error;
985
986 }
987
988 STATIC int
989 xfs_inactive_symlink_local(
990 xfs_inode_t *ip,
991 xfs_trans_t **tpp)
992 {
993 int error;
994
995 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
996 /*
997 * We're freeing a symlink which fit into
998 * the inode. Just free the memory used
999 * to hold the old symlink.
1000 */
1001 error = xfs_trans_reserve(*tpp, 0,
1002 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1003 0, XFS_TRANS_PERM_LOG_RES,
1004 XFS_ITRUNCATE_LOG_COUNT);
1005
1006 if (error) {
1007 xfs_trans_cancel(*tpp, 0);
1008 *tpp = NULL;
1009 return error;
1010 }
1011 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1012
1013 /*
1014 * Zero length symlinks _can_ exist.
1015 */
1016 if (ip->i_df.if_bytes > 0) {
1017 xfs_idata_realloc(ip,
1018 -(ip->i_df.if_bytes),
1019 XFS_DATA_FORK);
1020 ASSERT(ip->i_df.if_bytes == 0);
1021 }
1022 return 0;
1023 }
1024
1025 STATIC int
1026 xfs_inactive_attrs(
1027 xfs_inode_t *ip,
1028 xfs_trans_t **tpp)
1029 {
1030 xfs_trans_t *tp;
1031 int error;
1032 xfs_mount_t *mp;
1033
1034 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1035 tp = *tpp;
1036 mp = ip->i_mount;
1037 ASSERT(ip->i_d.di_forkoff != 0);
1038 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1039 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1040 if (error)
1041 goto error_unlock;
1042
1043 error = xfs_attr_inactive(ip);
1044 if (error)
1045 goto error_unlock;
1046
1047 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1048 error = xfs_trans_reserve(tp, 0,
1049 XFS_IFREE_LOG_RES(mp),
1050 0, XFS_TRANS_PERM_LOG_RES,
1051 XFS_INACTIVE_LOG_COUNT);
1052 if (error)
1053 goto error_cancel;
1054
1055 xfs_ilock(ip, XFS_ILOCK_EXCL);
1056 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1057 xfs_trans_ihold(tp, ip);
1058 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1059
1060 ASSERT(ip->i_d.di_anextents == 0);
1061
1062 *tpp = tp;
1063 return 0;
1064
1065 error_cancel:
1066 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1067 xfs_trans_cancel(tp, 0);
1068 error_unlock:
1069 *tpp = NULL;
1070 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1071 return error;
1072 }
1073
1074 int
1075 xfs_release(
1076 xfs_inode_t *ip)
1077 {
1078 xfs_mount_t *mp = ip->i_mount;
1079 int error;
1080
1081 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1082 return 0;
1083
1084 /* If this is a read-only mount, don't do this (would generate I/O) */
1085 if (mp->m_flags & XFS_MOUNT_RDONLY)
1086 return 0;
1087
1088 if (!XFS_FORCED_SHUTDOWN(mp)) {
1089 int truncated;
1090
1091 /*
1092 * If we are using filestreams, and we have an unlinked
1093 * file that we are processing the last close on, then nothing
1094 * will be able to reopen and write to this file. Purge this
1095 * inode from the filestreams cache so that it doesn't delay
1096 * teardown of the inode.
1097 */
1098 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1099 xfs_filestream_deassociate(ip);
1100
1101 /*
1102 * If we previously truncated this file and removed old data
1103 * in the process, we want to initiate "early" writeout on
1104 * the last close. This is an attempt to combat the notorious
1105 * NULL files problem which is particularly noticable from a
1106 * truncate down, buffered (re-)write (delalloc), followed by
1107 * a crash. What we are effectively doing here is
1108 * significantly reducing the time window where we'd otherwise
1109 * be exposed to that problem.
1110 */
1111 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1112 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
1113 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1114 }
1115
1116 if (ip->i_d.di_nlink != 0) {
1117 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1118 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1119 ip->i_delayed_blks > 0)) &&
1120 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1121 (!(ip->i_d.di_flags &
1122 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1123
1124 /*
1125 * If we can't get the iolock just skip truncating
1126 * the blocks past EOF because we could deadlock
1127 * with the mmap_sem otherwise. We'll get another
1128 * chance to drop them once the last reference to
1129 * the inode is dropped, so we'll never leak blocks
1130 * permanently.
1131 */
1132 error = xfs_free_eofblocks(mp, ip,
1133 XFS_FREE_EOF_TRYLOCK);
1134 if (error)
1135 return error;
1136 }
1137 }
1138
1139 return 0;
1140 }
1141
1142 /*
1143 * xfs_inactive
1144 *
1145 * This is called when the vnode reference count for the vnode
1146 * goes to zero. If the file has been unlinked, then it must
1147 * now be truncated. Also, we clear all of the read-ahead state
1148 * kept for the inode here since the file is now closed.
1149 */
1150 int
1151 xfs_inactive(
1152 xfs_inode_t *ip)
1153 {
1154 xfs_bmap_free_t free_list;
1155 xfs_fsblock_t first_block;
1156 int committed;
1157 xfs_trans_t *tp;
1158 xfs_mount_t *mp;
1159 int error;
1160 int truncate;
1161
1162 xfs_itrace_entry(ip);
1163
1164 /*
1165 * If the inode is already free, then there can be nothing
1166 * to clean up here.
1167 */
1168 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1169 ASSERT(ip->i_df.if_real_bytes == 0);
1170 ASSERT(ip->i_df.if_broot_bytes == 0);
1171 return VN_INACTIVE_CACHE;
1172 }
1173
1174 /*
1175 * Only do a truncate if it's a regular file with
1176 * some actual space in it. It's OK to look at the
1177 * inode's fields without the lock because we're the
1178 * only one with a reference to the inode.
1179 */
1180 truncate = ((ip->i_d.di_nlink == 0) &&
1181 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1182 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1183 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1184
1185 mp = ip->i_mount;
1186
1187 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1188 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1189
1190 error = 0;
1191
1192 /* If this is a read-only mount, don't do this (would generate I/O) */
1193 if (mp->m_flags & XFS_MOUNT_RDONLY)
1194 goto out;
1195
1196 if (ip->i_d.di_nlink != 0) {
1197 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1198 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1199 ip->i_delayed_blks > 0)) &&
1200 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1201 (!(ip->i_d.di_flags &
1202 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1203 (ip->i_delayed_blks != 0)))) {
1204 error = xfs_free_eofblocks(mp, ip, 0);
1205 if (error)
1206 return VN_INACTIVE_CACHE;
1207 }
1208 goto out;
1209 }
1210
1211 ASSERT(ip->i_d.di_nlink == 0);
1212
1213 error = xfs_qm_dqattach(ip, 0);
1214 if (error)
1215 return VN_INACTIVE_CACHE;
1216
1217 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1218 if (truncate) {
1219 /*
1220 * Do the xfs_itruncate_start() call before
1221 * reserving any log space because itruncate_start
1222 * will call into the buffer cache and we can't
1223 * do that within a transaction.
1224 */
1225 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1226
1227 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1228 if (error) {
1229 xfs_trans_cancel(tp, 0);
1230 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1231 return VN_INACTIVE_CACHE;
1232 }
1233
1234 error = xfs_trans_reserve(tp, 0,
1235 XFS_ITRUNCATE_LOG_RES(mp),
1236 0, XFS_TRANS_PERM_LOG_RES,
1237 XFS_ITRUNCATE_LOG_COUNT);
1238 if (error) {
1239 /* Don't call itruncate_cleanup */
1240 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1241 xfs_trans_cancel(tp, 0);
1242 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1243 return VN_INACTIVE_CACHE;
1244 }
1245
1246 xfs_ilock(ip, XFS_ILOCK_EXCL);
1247 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1248 xfs_trans_ihold(tp, ip);
1249
1250 /*
1251 * normally, we have to run xfs_itruncate_finish sync.
1252 * But if filesystem is wsync and we're in the inactive
1253 * path, then we know that nlink == 0, and that the
1254 * xaction that made nlink == 0 is permanently committed
1255 * since xfs_remove runs as a synchronous transaction.
1256 */
1257 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1258 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1259
1260 if (error) {
1261 xfs_trans_cancel(tp,
1262 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1263 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1264 return VN_INACTIVE_CACHE;
1265 }
1266 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1267
1268 /*
1269 * If we get an error while cleaning up a
1270 * symlink we bail out.
1271 */
1272 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1273 xfs_inactive_symlink_rmt(ip, &tp) :
1274 xfs_inactive_symlink_local(ip, &tp);
1275
1276 if (error) {
1277 ASSERT(tp == NULL);
1278 return VN_INACTIVE_CACHE;
1279 }
1280
1281 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1282 xfs_trans_ihold(tp, ip);
1283 } else {
1284 error = xfs_trans_reserve(tp, 0,
1285 XFS_IFREE_LOG_RES(mp),
1286 0, XFS_TRANS_PERM_LOG_RES,
1287 XFS_INACTIVE_LOG_COUNT);
1288 if (error) {
1289 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1290 xfs_trans_cancel(tp, 0);
1291 return VN_INACTIVE_CACHE;
1292 }
1293
1294 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1295 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1296 xfs_trans_ihold(tp, ip);
1297 }
1298
1299 /*
1300 * If there are attributes associated with the file
1301 * then blow them away now. The code calls a routine
1302 * that recursively deconstructs the attribute fork.
1303 * We need to just commit the current transaction
1304 * because we can't use it for xfs_attr_inactive().
1305 */
1306 if (ip->i_d.di_anextents > 0) {
1307 error = xfs_inactive_attrs(ip, &tp);
1308 /*
1309 * If we got an error, the transaction is already
1310 * cancelled, and the inode is unlocked. Just get out.
1311 */
1312 if (error)
1313 return VN_INACTIVE_CACHE;
1314 } else if (ip->i_afp) {
1315 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1316 }
1317
1318 /*
1319 * Free the inode.
1320 */
1321 xfs_bmap_init(&free_list, &first_block);
1322 error = xfs_ifree(tp, ip, &free_list);
1323 if (error) {
1324 /*
1325 * If we fail to free the inode, shut down. The cancel
1326 * might do that, we need to make sure. Otherwise the
1327 * inode might be lost for a long time or forever.
1328 */
1329 if (!XFS_FORCED_SHUTDOWN(mp)) {
1330 cmn_err(CE_NOTE,
1331 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1332 error, mp->m_fsname);
1333 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1334 }
1335 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1336 } else {
1337 /*
1338 * Credit the quota account(s). The inode is gone.
1339 */
1340 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1341
1342 /*
1343 * Just ignore errors at this point. There is nothing we can
1344 * do except to try to keep going. Make sure it's not a silent
1345 * error.
1346 */
1347 error = xfs_bmap_finish(&tp, &free_list, &committed);
1348 if (error)
1349 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1350 "xfs_bmap_finish() returned error %d", error);
1351 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1352 if (error)
1353 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1354 "xfs_trans_commit() returned error %d", error);
1355 }
1356
1357 /*
1358 * Release the dquots held by inode, if any.
1359 */
1360 xfs_qm_dqdetach(ip);
1361 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1362
1363 out:
1364 return VN_INACTIVE_CACHE;
1365 }
1366
1367 /*
1368 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1369 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1370 * ci_name->name will point to a the actual name (caller must free) or
1371 * will be set to NULL if an exact match is found.
1372 */
1373 int
1374 xfs_lookup(
1375 xfs_inode_t *dp,
1376 struct xfs_name *name,
1377 xfs_inode_t **ipp,
1378 struct xfs_name *ci_name)
1379 {
1380 xfs_ino_t inum;
1381 int error;
1382 uint lock_mode;
1383
1384 xfs_itrace_entry(dp);
1385
1386 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1387 return XFS_ERROR(EIO);
1388
1389 lock_mode = xfs_ilock_map_shared(dp);
1390 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1391 xfs_iunlock_map_shared(dp, lock_mode);
1392
1393 if (error)
1394 goto out;
1395
1396 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1397 if (error)
1398 goto out_free_name;
1399
1400 xfs_itrace_ref(*ipp);
1401 return 0;
1402
1403 out_free_name:
1404 if (ci_name)
1405 kmem_free(ci_name->name);
1406 out:
1407 *ipp = NULL;
1408 return error;
1409 }
1410
1411 int
1412 xfs_create(
1413 xfs_inode_t *dp,
1414 struct xfs_name *name,
1415 mode_t mode,
1416 xfs_dev_t rdev,
1417 xfs_inode_t **ipp,
1418 cred_t *credp)
1419 {
1420 int is_dir = S_ISDIR(mode);
1421 struct xfs_mount *mp = dp->i_mount;
1422 struct xfs_inode *ip = NULL;
1423 struct xfs_trans *tp = NULL;
1424 int error;
1425 xfs_bmap_free_t free_list;
1426 xfs_fsblock_t first_block;
1427 boolean_t unlock_dp_on_error = B_FALSE;
1428 uint cancel_flags;
1429 int committed;
1430 xfs_prid_t prid;
1431 struct xfs_dquot *udqp = NULL;
1432 struct xfs_dquot *gdqp = NULL;
1433 uint resblks;
1434 uint log_res;
1435 uint log_count;
1436
1437 xfs_itrace_entry(dp);
1438
1439 if (XFS_FORCED_SHUTDOWN(mp))
1440 return XFS_ERROR(EIO);
1441
1442 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1443 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1444 dp, DM_RIGHT_NULL, NULL,
1445 DM_RIGHT_NULL, name->name, NULL,
1446 mode, 0, 0);
1447
1448 if (error)
1449 return error;
1450 }
1451
1452 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1453 prid = dp->i_d.di_projid;
1454 else
1455 prid = dfltprid;
1456
1457 /*
1458 * Make sure that we have allocated dquot(s) on disk.
1459 */
1460 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1461 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1462 if (error)
1463 goto std_return;
1464
1465 if (is_dir) {
1466 rdev = 0;
1467 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1468 log_res = XFS_MKDIR_LOG_RES(mp);
1469 log_count = XFS_MKDIR_LOG_COUNT;
1470 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1471 } else {
1472 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1473 log_res = XFS_CREATE_LOG_RES(mp);
1474 log_count = XFS_CREATE_LOG_COUNT;
1475 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1476 }
1477
1478 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1479
1480 /*
1481 * Initially assume that the file does not exist and
1482 * reserve the resources for that case. If that is not
1483 * the case we'll drop the one we have and get a more
1484 * appropriate transaction later.
1485 */
1486 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1487 XFS_TRANS_PERM_LOG_RES, log_count);
1488 if (error == ENOSPC) {
1489 /* flush outstanding delalloc blocks and retry */
1490 xfs_flush_inodes(dp);
1491 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1492 XFS_TRANS_PERM_LOG_RES, log_count);
1493 }
1494 if (error == ENOSPC) {
1495 /* No space at all so try a "no-allocation" reservation */
1496 resblks = 0;
1497 error = xfs_trans_reserve(tp, 0, log_res, 0,
1498 XFS_TRANS_PERM_LOG_RES, log_count);
1499 }
1500 if (error) {
1501 cancel_flags = 0;
1502 goto out_trans_cancel;
1503 }
1504
1505 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1506 unlock_dp_on_error = B_TRUE;
1507
1508 /*
1509 * Check for directory link count overflow.
1510 */
1511 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1512 error = XFS_ERROR(EMLINK);
1513 goto out_trans_cancel;
1514 }
1515
1516 xfs_bmap_init(&free_list, &first_block);
1517
1518 /*
1519 * Reserve disk quota and the inode.
1520 */
1521 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1522 if (error)
1523 goto out_trans_cancel;
1524
1525 error = xfs_dir_canenter(tp, dp, name, resblks);
1526 if (error)
1527 goto out_trans_cancel;
1528
1529 /*
1530 * A newly created regular or special file just has one directory
1531 * entry pointing to them, but a directory also the "." entry
1532 * pointing to itself.
1533 */
1534 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1535 prid, resblks > 0, &ip, &committed);
1536 if (error) {
1537 if (error == ENOSPC)
1538 goto out_trans_cancel;
1539 goto out_trans_abort;
1540 }
1541
1542 /*
1543 * At this point, we've gotten a newly allocated inode.
1544 * It is locked (and joined to the transaction).
1545 */
1546 xfs_itrace_ref(ip);
1547 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1548
1549 /*
1550 * Now we join the directory inode to the transaction. We do not do it
1551 * earlier because xfs_dir_ialloc might commit the previous transaction
1552 * (and release all the locks). An error from here on will result in
1553 * the transaction cancel unlocking dp so don't do it explicitly in the
1554 * error path.
1555 */
1556 IHOLD(dp);
1557 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1558 unlock_dp_on_error = B_FALSE;
1559
1560 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1561 &first_block, &free_list, resblks ?
1562 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1563 if (error) {
1564 ASSERT(error != ENOSPC);
1565 goto out_trans_abort;
1566 }
1567 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1568 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1569
1570 if (is_dir) {
1571 error = xfs_dir_init(tp, ip, dp);
1572 if (error)
1573 goto out_bmap_cancel;
1574
1575 error = xfs_bumplink(tp, dp);
1576 if (error)
1577 goto out_bmap_cancel;
1578 }
1579
1580 /*
1581 * If this is a synchronous mount, make sure that the
1582 * create transaction goes to disk before returning to
1583 * the user.
1584 */
1585 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1586 xfs_trans_set_sync(tp);
1587
1588 /*
1589 * Attach the dquot(s) to the inodes and modify them incore.
1590 * These ids of the inode couldn't have changed since the new
1591 * inode has been locked ever since it was created.
1592 */
1593 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1594
1595 /*
1596 * xfs_trans_commit normally decrements the vnode ref count
1597 * when it unlocks the inode. Since we want to return the
1598 * vnode to the caller, we bump the vnode ref count now.
1599 */
1600 IHOLD(ip);
1601
1602 error = xfs_bmap_finish(&tp, &free_list, &committed);
1603 if (error)
1604 goto out_abort_rele;
1605
1606 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1607 if (error) {
1608 IRELE(ip);
1609 goto out_dqrele;
1610 }
1611
1612 xfs_qm_dqrele(udqp);
1613 xfs_qm_dqrele(gdqp);
1614
1615 *ipp = ip;
1616
1617 /* Fallthrough to std_return with error = 0 */
1618 std_return:
1619 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1620 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1621 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1622 error, 0);
1623 }
1624
1625 return error;
1626
1627 out_bmap_cancel:
1628 xfs_bmap_cancel(&free_list);
1629 out_trans_abort:
1630 cancel_flags |= XFS_TRANS_ABORT;
1631 out_trans_cancel:
1632 xfs_trans_cancel(tp, cancel_flags);
1633 out_dqrele:
1634 xfs_qm_dqrele(udqp);
1635 xfs_qm_dqrele(gdqp);
1636
1637 if (unlock_dp_on_error)
1638 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1639
1640 goto std_return;
1641
1642 out_abort_rele:
1643 /*
1644 * Wait until after the current transaction is aborted to
1645 * release the inode. This prevents recursive transactions
1646 * and deadlocks from xfs_inactive.
1647 */
1648 xfs_bmap_cancel(&free_list);
1649 cancel_flags |= XFS_TRANS_ABORT;
1650 xfs_trans_cancel(tp, cancel_flags);
1651 IRELE(ip);
1652 unlock_dp_on_error = B_FALSE;
1653 goto out_dqrele;
1654 }
1655
1656 #ifdef DEBUG
1657 int xfs_locked_n;
1658 int xfs_small_retries;
1659 int xfs_middle_retries;
1660 int xfs_lots_retries;
1661 int xfs_lock_delays;
1662 #endif
1663
1664 /*
1665 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1666 * a different value
1667 */
1668 static inline int
1669 xfs_lock_inumorder(int lock_mode, int subclass)
1670 {
1671 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1672 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1673 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1674 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1675
1676 return lock_mode;
1677 }
1678
1679 /*
1680 * The following routine will lock n inodes in exclusive mode.
1681 * We assume the caller calls us with the inodes in i_ino order.
1682 *
1683 * We need to detect deadlock where an inode that we lock
1684 * is in the AIL and we start waiting for another inode that is locked
1685 * by a thread in a long running transaction (such as truncate). This can
1686 * result in deadlock since the long running trans might need to wait
1687 * for the inode we just locked in order to push the tail and free space
1688 * in the log.
1689 */
1690 void
1691 xfs_lock_inodes(
1692 xfs_inode_t **ips,
1693 int inodes,
1694 uint lock_mode)
1695 {
1696 int attempts = 0, i, j, try_lock;
1697 xfs_log_item_t *lp;
1698
1699 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1700
1701 try_lock = 0;
1702 i = 0;
1703
1704 again:
1705 for (; i < inodes; i++) {
1706 ASSERT(ips[i]);
1707
1708 if (i && (ips[i] == ips[i-1])) /* Already locked */
1709 continue;
1710
1711 /*
1712 * If try_lock is not set yet, make sure all locked inodes
1713 * are not in the AIL.
1714 * If any are, set try_lock to be used later.
1715 */
1716
1717 if (!try_lock) {
1718 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1719 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1720 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1721 try_lock++;
1722 }
1723 }
1724 }
1725
1726 /*
1727 * If any of the previous locks we have locked is in the AIL,
1728 * we must TRY to get the second and subsequent locks. If
1729 * we can't get any, we must release all we have
1730 * and try again.
1731 */
1732
1733 if (try_lock) {
1734 /* try_lock must be 0 if i is 0. */
1735 /*
1736 * try_lock means we have an inode locked
1737 * that is in the AIL.
1738 */
1739 ASSERT(i != 0);
1740 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1741 attempts++;
1742
1743 /*
1744 * Unlock all previous guys and try again.
1745 * xfs_iunlock will try to push the tail
1746 * if the inode is in the AIL.
1747 */
1748
1749 for(j = i - 1; j >= 0; j--) {
1750
1751 /*
1752 * Check to see if we've already
1753 * unlocked this one.
1754 * Not the first one going back,
1755 * and the inode ptr is the same.
1756 */
1757 if ((j != (i - 1)) && ips[j] ==
1758 ips[j+1])
1759 continue;
1760
1761 xfs_iunlock(ips[j], lock_mode);
1762 }
1763
1764 if ((attempts % 5) == 0) {
1765 delay(1); /* Don't just spin the CPU */
1766 #ifdef DEBUG
1767 xfs_lock_delays++;
1768 #endif
1769 }
1770 i = 0;
1771 try_lock = 0;
1772 goto again;
1773 }
1774 } else {
1775 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1776 }
1777 }
1778
1779 #ifdef DEBUG
1780 if (attempts) {
1781 if (attempts < 5) xfs_small_retries++;
1782 else if (attempts < 100) xfs_middle_retries++;
1783 else xfs_lots_retries++;
1784 } else {
1785 xfs_locked_n++;
1786 }
1787 #endif
1788 }
1789
1790 /*
1791 * xfs_lock_two_inodes() can only be used to lock one type of lock
1792 * at a time - the iolock or the ilock, but not both at once. If
1793 * we lock both at once, lockdep will report false positives saying
1794 * we have violated locking orders.
1795 */
1796 void
1797 xfs_lock_two_inodes(
1798 xfs_inode_t *ip0,
1799 xfs_inode_t *ip1,
1800 uint lock_mode)
1801 {
1802 xfs_inode_t *temp;
1803 int attempts = 0;
1804 xfs_log_item_t *lp;
1805
1806 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1807 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1808 ASSERT(ip0->i_ino != ip1->i_ino);
1809
1810 if (ip0->i_ino > ip1->i_ino) {
1811 temp = ip0;
1812 ip0 = ip1;
1813 ip1 = temp;
1814 }
1815
1816 again:
1817 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1818
1819 /*
1820 * If the first lock we have locked is in the AIL, we must TRY to get
1821 * the second lock. If we can't get it, we must release the first one
1822 * and try again.
1823 */
1824 lp = (xfs_log_item_t *)ip0->i_itemp;
1825 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1826 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1827 xfs_iunlock(ip0, lock_mode);
1828 if ((++attempts % 5) == 0)
1829 delay(1); /* Don't just spin the CPU */
1830 goto again;
1831 }
1832 } else {
1833 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1834 }
1835 }
1836
1837 int
1838 xfs_remove(
1839 xfs_inode_t *dp,
1840 struct xfs_name *name,
1841 xfs_inode_t *ip)
1842 {
1843 xfs_mount_t *mp = dp->i_mount;
1844 xfs_trans_t *tp = NULL;
1845 int is_dir = S_ISDIR(ip->i_d.di_mode);
1846 int error = 0;
1847 xfs_bmap_free_t free_list;
1848 xfs_fsblock_t first_block;
1849 int cancel_flags;
1850 int committed;
1851 int link_zero;
1852 uint resblks;
1853 uint log_count;
1854
1855 xfs_itrace_entry(dp);
1856 xfs_itrace_entry(ip);
1857
1858 if (XFS_FORCED_SHUTDOWN(mp))
1859 return XFS_ERROR(EIO);
1860
1861 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1862 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1863 NULL, DM_RIGHT_NULL, name->name, NULL,
1864 ip->i_d.di_mode, 0, 0);
1865 if (error)
1866 return error;
1867 }
1868
1869 error = xfs_qm_dqattach(dp, 0);
1870 if (error)
1871 goto std_return;
1872
1873 error = xfs_qm_dqattach(ip, 0);
1874 if (error)
1875 goto std_return;
1876
1877 if (is_dir) {
1878 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1879 log_count = XFS_DEFAULT_LOG_COUNT;
1880 } else {
1881 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1882 log_count = XFS_REMOVE_LOG_COUNT;
1883 }
1884 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1885
1886 /*
1887 * We try to get the real space reservation first,
1888 * allowing for directory btree deletion(s) implying
1889 * possible bmap insert(s). If we can't get the space
1890 * reservation then we use 0 instead, and avoid the bmap
1891 * btree insert(s) in the directory code by, if the bmap
1892 * insert tries to happen, instead trimming the LAST
1893 * block from the directory.
1894 */
1895 resblks = XFS_REMOVE_SPACE_RES(mp);
1896 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1897 XFS_TRANS_PERM_LOG_RES, log_count);
1898 if (error == ENOSPC) {
1899 resblks = 0;
1900 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1901 XFS_TRANS_PERM_LOG_RES, log_count);
1902 }
1903 if (error) {
1904 ASSERT(error != ENOSPC);
1905 cancel_flags = 0;
1906 goto out_trans_cancel;
1907 }
1908
1909 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1910
1911 /*
1912 * At this point, we've gotten both the directory and the entry
1913 * inodes locked.
1914 */
1915 IHOLD(ip);
1916 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1917
1918 IHOLD(dp);
1919 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1920
1921 /*
1922 * If we're removing a directory perform some additional validation.
1923 */
1924 if (is_dir) {
1925 ASSERT(ip->i_d.di_nlink >= 2);
1926 if (ip->i_d.di_nlink != 2) {
1927 error = XFS_ERROR(ENOTEMPTY);
1928 goto out_trans_cancel;
1929 }
1930 if (!xfs_dir_isempty(ip)) {
1931 error = XFS_ERROR(ENOTEMPTY);
1932 goto out_trans_cancel;
1933 }
1934 }
1935
1936 xfs_bmap_init(&free_list, &first_block);
1937 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1938 &first_block, &free_list, resblks);
1939 if (error) {
1940 ASSERT(error != ENOENT);
1941 goto out_bmap_cancel;
1942 }
1943 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1944
1945 if (is_dir) {
1946 /*
1947 * Drop the link from ip's "..".
1948 */
1949 error = xfs_droplink(tp, dp);
1950 if (error)
1951 goto out_bmap_cancel;
1952
1953 /*
1954 * Drop the "." link from ip to self.
1955 */
1956 error = xfs_droplink(tp, ip);
1957 if (error)
1958 goto out_bmap_cancel;
1959 } else {
1960 /*
1961 * When removing a non-directory we need to log the parent
1962 * inode here. For a directory this is done implicitly
1963 * by the xfs_droplink call for the ".." entry.
1964 */
1965 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1966 }
1967
1968 /*
1969 * Drop the link from dp to ip.
1970 */
1971 error = xfs_droplink(tp, ip);
1972 if (error)
1973 goto out_bmap_cancel;
1974
1975 /*
1976 * Determine if this is the last link while
1977 * we are in the transaction.
1978 */
1979 link_zero = (ip->i_d.di_nlink == 0);
1980
1981 /*
1982 * If this is a synchronous mount, make sure that the
1983 * remove transaction goes to disk before returning to
1984 * the user.
1985 */
1986 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1987 xfs_trans_set_sync(tp);
1988
1989 error = xfs_bmap_finish(&tp, &free_list, &committed);
1990 if (error)
1991 goto out_bmap_cancel;
1992
1993 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1994 if (error)
1995 goto std_return;
1996
1997 /*
1998 * If we are using filestreams, kill the stream association.
1999 * If the file is still open it may get a new one but that
2000 * will get killed on last close in xfs_close() so we don't
2001 * have to worry about that.
2002 */
2003 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
2004 xfs_filestream_deassociate(ip);
2005
2006 xfs_itrace_exit(ip);
2007 xfs_itrace_exit(dp);
2008
2009 std_return:
2010 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2011 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
2012 NULL, DM_RIGHT_NULL, name->name, NULL,
2013 ip->i_d.di_mode, error, 0);
2014 }
2015
2016 return error;
2017
2018 out_bmap_cancel:
2019 xfs_bmap_cancel(&free_list);
2020 cancel_flags |= XFS_TRANS_ABORT;
2021 out_trans_cancel:
2022 xfs_trans_cancel(tp, cancel_flags);
2023 goto std_return;
2024 }
2025
2026 int
2027 xfs_link(
2028 xfs_inode_t *tdp,
2029 xfs_inode_t *sip,
2030 struct xfs_name *target_name)
2031 {
2032 xfs_mount_t *mp = tdp->i_mount;
2033 xfs_trans_t *tp;
2034 int error;
2035 xfs_bmap_free_t free_list;
2036 xfs_fsblock_t first_block;
2037 int cancel_flags;
2038 int committed;
2039 int resblks;
2040
2041 xfs_itrace_entry(tdp);
2042 xfs_itrace_entry(sip);
2043
2044 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2045
2046 if (XFS_FORCED_SHUTDOWN(mp))
2047 return XFS_ERROR(EIO);
2048
2049 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2050 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2051 tdp, DM_RIGHT_NULL,
2052 sip, DM_RIGHT_NULL,
2053 target_name->name, NULL, 0, 0, 0);
2054 if (error)
2055 return error;
2056 }
2057
2058 /* Return through std_return after this point. */
2059
2060 error = xfs_qm_dqattach(sip, 0);
2061 if (error)
2062 goto std_return;
2063
2064 error = xfs_qm_dqattach(tdp, 0);
2065 if (error)
2066 goto std_return;
2067
2068 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2069 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2070 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2071 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2072 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2073 if (error == ENOSPC) {
2074 resblks = 0;
2075 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2076 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2077 }
2078 if (error) {
2079 cancel_flags = 0;
2080 goto error_return;
2081 }
2082
2083 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
2084
2085 /*
2086 * Increment vnode ref counts since xfs_trans_commit &
2087 * xfs_trans_cancel will both unlock the inodes and
2088 * decrement the associated ref counts.
2089 */
2090 IHOLD(sip);
2091 IHOLD(tdp);
2092 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2093 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2094
2095 /*
2096 * If the source has too many links, we can't make any more to it.
2097 */
2098 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2099 error = XFS_ERROR(EMLINK);
2100 goto error_return;
2101 }
2102
2103 /*
2104 * If we are using project inheritance, we only allow hard link
2105 * creation in our tree when the project IDs are the same; else
2106 * the tree quota mechanism could be circumvented.
2107 */
2108 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2109 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2110 error = XFS_ERROR(EXDEV);
2111 goto error_return;
2112 }
2113
2114 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2115 if (error)
2116 goto error_return;
2117
2118 xfs_bmap_init(&free_list, &first_block);
2119
2120 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2121 &first_block, &free_list, resblks);
2122 if (error)
2123 goto abort_return;
2124 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2125 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2126
2127 error = xfs_bumplink(tp, sip);
2128 if (error)
2129 goto abort_return;
2130
2131 /*
2132 * If this is a synchronous mount, make sure that the
2133 * link transaction goes to disk before returning to
2134 * the user.
2135 */
2136 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2137 xfs_trans_set_sync(tp);
2138 }
2139
2140 error = xfs_bmap_finish (&tp, &free_list, &committed);
2141 if (error) {
2142 xfs_bmap_cancel(&free_list);
2143 goto abort_return;
2144 }
2145
2146 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2147 if (error)
2148 goto std_return;
2149
2150 /* Fall through to std_return with error = 0. */
2151 std_return:
2152 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2153 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2154 tdp, DM_RIGHT_NULL,
2155 sip, DM_RIGHT_NULL,
2156 target_name->name, NULL, 0, error, 0);
2157 }
2158 return error;
2159
2160 abort_return:
2161 cancel_flags |= XFS_TRANS_ABORT;
2162 /* FALLTHROUGH */
2163
2164 error_return:
2165 xfs_trans_cancel(tp, cancel_flags);
2166 goto std_return;
2167 }
2168
2169 int
2170 xfs_symlink(
2171 xfs_inode_t *dp,
2172 struct xfs_name *link_name,
2173 const char *target_path,
2174 mode_t mode,
2175 xfs_inode_t **ipp,
2176 cred_t *credp)
2177 {
2178 xfs_mount_t *mp = dp->i_mount;
2179 xfs_trans_t *tp;
2180 xfs_inode_t *ip;
2181 int error;
2182 int pathlen;
2183 xfs_bmap_free_t free_list;
2184 xfs_fsblock_t first_block;
2185 boolean_t unlock_dp_on_error = B_FALSE;
2186 uint cancel_flags;
2187 int committed;
2188 xfs_fileoff_t first_fsb;
2189 xfs_filblks_t fs_blocks;
2190 int nmaps;
2191 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2192 xfs_daddr_t d;
2193 const char *cur_chunk;
2194 int byte_cnt;
2195 int n;
2196 xfs_buf_t *bp;
2197 xfs_prid_t prid;
2198 struct xfs_dquot *udqp, *gdqp;
2199 uint resblks;
2200
2201 *ipp = NULL;
2202 error = 0;
2203 ip = NULL;
2204 tp = NULL;
2205
2206 xfs_itrace_entry(dp);
2207
2208 if (XFS_FORCED_SHUTDOWN(mp))
2209 return XFS_ERROR(EIO);
2210
2211 /*
2212 * Check component lengths of the target path name.
2213 */
2214 pathlen = strlen(target_path);
2215 if (pathlen >= MAXPATHLEN) /* total string too long */
2216 return XFS_ERROR(ENAMETOOLONG);
2217
2218 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
2219 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
2220 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2221 link_name->name, target_path, 0, 0, 0);
2222 if (error)
2223 return error;
2224 }
2225
2226 /* Return through std_return after this point. */
2227
2228 udqp = gdqp = NULL;
2229 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2230 prid = dp->i_d.di_projid;
2231 else
2232 prid = (xfs_prid_t)dfltprid;
2233
2234 /*
2235 * Make sure that we have allocated dquot(s) on disk.
2236 */
2237 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
2238 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2239 if (error)
2240 goto std_return;
2241
2242 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2243 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2244 /*
2245 * The symlink will fit into the inode data fork?
2246 * There can't be any attributes so we get the whole variable part.
2247 */
2248 if (pathlen <= XFS_LITINO(mp))
2249 fs_blocks = 0;
2250 else
2251 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2252 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2253 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2254 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2255 if (error == ENOSPC && fs_blocks == 0) {
2256 resblks = 0;
2257 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2258 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2259 }
2260 if (error) {
2261 cancel_flags = 0;
2262 goto error_return;
2263 }
2264
2265 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2266 unlock_dp_on_error = B_TRUE;
2267
2268 /*
2269 * Check whether the directory allows new symlinks or not.
2270 */
2271 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2272 error = XFS_ERROR(EPERM);
2273 goto error_return;
2274 }
2275
2276 /*
2277 * Reserve disk quota : blocks and inode.
2278 */
2279 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
2280 if (error)
2281 goto error_return;
2282
2283 /*
2284 * Check for ability to enter directory entry, if no space reserved.
2285 */
2286 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2287 if (error)
2288 goto error_return;
2289 /*
2290 * Initialize the bmap freelist prior to calling either
2291 * bmapi or the directory create code.
2292 */
2293 xfs_bmap_init(&free_list, &first_block);
2294
2295 /*
2296 * Allocate an inode for the symlink.
2297 */
2298 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2299 1, 0, credp, prid, resblks > 0, &ip, NULL);
2300 if (error) {
2301 if (error == ENOSPC)
2302 goto error_return;
2303 goto error1;
2304 }
2305 xfs_itrace_ref(ip);
2306
2307 /*
2308 * An error after we've joined dp to the transaction will result in the
2309 * transaction cancel unlocking dp so don't do it explicitly in the
2310 * error path.
2311 */
2312 IHOLD(dp);
2313 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2314 unlock_dp_on_error = B_FALSE;
2315
2316 /*
2317 * Also attach the dquot(s) to it, if applicable.
2318 */
2319 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
2320
2321 if (resblks)
2322 resblks -= XFS_IALLOC_SPACE_RES(mp);
2323 /*
2324 * If the symlink will fit into the inode, write it inline.
2325 */
2326 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2327 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2328 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2329 ip->i_d.di_size = pathlen;
2330
2331 /*
2332 * The inode was initially created in extent format.
2333 */
2334 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2335 ip->i_df.if_flags |= XFS_IFINLINE;
2336
2337 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2338 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2339
2340 } else {
2341 first_fsb = 0;
2342 nmaps = SYMLINK_MAPS;
2343
2344 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2345 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2346 &first_block, resblks, mval, &nmaps,
2347 &free_list, NULL);
2348 if (error) {
2349 goto error1;
2350 }
2351
2352 if (resblks)
2353 resblks -= fs_blocks;
2354 ip->i_d.di_size = pathlen;
2355 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2356
2357 cur_chunk = target_path;
2358 for (n = 0; n < nmaps; n++) {
2359 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2360 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2361 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2362 BTOBB(byte_cnt), 0);
2363 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2364 if (pathlen < byte_cnt) {
2365 byte_cnt = pathlen;
2366 }
2367 pathlen -= byte_cnt;
2368
2369 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2370 cur_chunk += byte_cnt;
2371
2372 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2373 }
2374 }
2375
2376 /*
2377 * Create the directory entry for the symlink.
2378 */
2379 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2380 &first_block, &free_list, resblks);
2381 if (error)
2382 goto error1;
2383 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2384 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2385
2386 /*
2387 * If this is a synchronous mount, make sure that the
2388 * symlink transaction goes to disk before returning to
2389 * the user.
2390 */
2391 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2392 xfs_trans_set_sync(tp);
2393 }
2394
2395 /*
2396 * xfs_trans_commit normally decrements the vnode ref count
2397 * when it unlocks the inode. Since we want to return the
2398 * vnode to the caller, we bump the vnode ref count now.
2399 */
2400 IHOLD(ip);
2401
2402 error = xfs_bmap_finish(&tp, &free_list, &committed);
2403 if (error) {
2404 goto error2;
2405 }
2406 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2407 xfs_qm_dqrele(udqp);
2408 xfs_qm_dqrele(gdqp);
2409
2410 /* Fall through to std_return with error = 0 or errno from
2411 * xfs_trans_commit */
2412 std_return:
2413 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
2414 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
2415 dp, DM_RIGHT_NULL,
2416 error ? NULL : ip,
2417 DM_RIGHT_NULL, link_name->name,
2418 target_path, 0, error, 0);
2419 }
2420
2421 if (!error)
2422 *ipp = ip;
2423 return error;
2424
2425 error2:
2426 IRELE(ip);
2427 error1:
2428 xfs_bmap_cancel(&free_list);
2429 cancel_flags |= XFS_TRANS_ABORT;
2430 error_return:
2431 xfs_trans_cancel(tp, cancel_flags);
2432 xfs_qm_dqrele(udqp);
2433 xfs_qm_dqrele(gdqp);
2434
2435 if (unlock_dp_on_error)
2436 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2437
2438 goto std_return;
2439 }
2440
2441 int
2442 xfs_set_dmattrs(
2443 xfs_inode_t *ip,
2444 u_int evmask,
2445 u_int16_t state)
2446 {
2447 xfs_mount_t *mp = ip->i_mount;
2448 xfs_trans_t *tp;
2449 int error;
2450
2451 if (!capable(CAP_SYS_ADMIN))
2452 return XFS_ERROR(EPERM);
2453
2454 if (XFS_FORCED_SHUTDOWN(mp))
2455 return XFS_ERROR(EIO);
2456
2457 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2458 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2459 if (error) {
2460 xfs_trans_cancel(tp, 0);
2461 return error;
2462 }
2463 xfs_ilock(ip, XFS_ILOCK_EXCL);
2464 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2465
2466 ip->i_d.di_dmevmask = evmask;
2467 ip->i_d.di_dmstate = state;
2468
2469 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2470 IHOLD(ip);
2471 error = xfs_trans_commit(tp, 0);
2472
2473 return error;
2474 }
2475
2476 /*
2477 * xfs_alloc_file_space()
2478 * This routine allocates disk space for the given file.
2479 *
2480 * If alloc_type == 0, this request is for an ALLOCSP type
2481 * request which will change the file size. In this case, no
2482 * DMAPI event will be generated by the call. A TRUNCATE event
2483 * will be generated later by xfs_setattr.
2484 *
2485 * If alloc_type != 0, this request is for a RESVSP type
2486 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2487 * lower block boundary byte address is less than the file's
2488 * length.
2489 *
2490 * RETURNS:
2491 * 0 on success
2492 * errno on error
2493 *
2494 */
2495 STATIC int
2496 xfs_alloc_file_space(
2497 xfs_inode_t *ip,
2498 xfs_off_t offset,
2499 xfs_off_t len,
2500 int alloc_type,
2501 int attr_flags)
2502 {
2503 xfs_mount_t *mp = ip->i_mount;
2504 xfs_off_t count;
2505 xfs_filblks_t allocated_fsb;
2506 xfs_filblks_t allocatesize_fsb;
2507 xfs_extlen_t extsz, temp;
2508 xfs_fileoff_t startoffset_fsb;
2509 xfs_fsblock_t firstfsb;
2510 int nimaps;
2511 int bmapi_flag;
2512 int quota_flag;
2513 int rt;
2514 xfs_trans_t *tp;
2515 xfs_bmbt_irec_t imaps[1], *imapp;
2516 xfs_bmap_free_t free_list;
2517 uint qblocks, resblks, resrtextents;
2518 int committed;
2519 int error;
2520
2521 xfs_itrace_entry(ip);
2522
2523 if (XFS_FORCED_SHUTDOWN(mp))
2524 return XFS_ERROR(EIO);
2525
2526 error = xfs_qm_dqattach(ip, 0);
2527 if (error)
2528 return error;
2529
2530 if (len <= 0)
2531 return XFS_ERROR(EINVAL);
2532
2533 rt = XFS_IS_REALTIME_INODE(ip);
2534 extsz = xfs_get_extsz_hint(ip);
2535
2536 count = len;
2537 imapp = &imaps[0];
2538 nimaps = 1;
2539 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
2540 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2541 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2542
2543 /* Generate a DMAPI event if needed. */
2544 if (alloc_type != 0 && offset < ip->i_size &&
2545 (attr_flags & XFS_ATTR_DMI) == 0 &&
2546 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2547 xfs_off_t end_dmi_offset;
2548
2549 end_dmi_offset = offset+len;
2550 if (end_dmi_offset > ip->i_size)
2551 end_dmi_offset = ip->i_size;
2552 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2553 end_dmi_offset - offset, 0, NULL);
2554 if (error)
2555 return error;
2556 }
2557
2558 /*
2559 * Allocate file space until done or until there is an error
2560 */
2561 retry:
2562 while (allocatesize_fsb && !error) {
2563 xfs_fileoff_t s, e;
2564
2565 /*
2566 * Determine space reservations for data/realtime.
2567 */
2568 if (unlikely(extsz)) {
2569 s = startoffset_fsb;
2570 do_div(s, extsz);
2571 s *= extsz;
2572 e = startoffset_fsb + allocatesize_fsb;
2573 if ((temp = do_mod(startoffset_fsb, extsz)))
2574 e += temp;
2575 if ((temp = do_mod(e, extsz)))
2576 e += extsz - temp;
2577 } else {
2578 s = 0;
2579 e = allocatesize_fsb;
2580 }
2581
2582 if (unlikely(rt)) {
2583 resrtextents = qblocks = (uint)(e - s);
2584 resrtextents /= mp->m_sb.sb_rextsize;
2585 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2586 quota_flag = XFS_QMOPT_RES_RTBLKS;
2587 } else {
2588 resrtextents = 0;
2589 resblks = qblocks = \
2590 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2591 quota_flag = XFS_QMOPT_RES_REGBLKS;
2592 }
2593
2594 /*
2595 * Allocate and setup the transaction.
2596 */
2597 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2598 error = xfs_trans_reserve(tp, resblks,
2599 XFS_WRITE_LOG_RES(mp), resrtextents,
2600 XFS_TRANS_PERM_LOG_RES,
2601 XFS_WRITE_LOG_COUNT);
2602 /*
2603 * Check for running out of space
2604 */
2605 if (error) {
2606 /*
2607 * Free the transaction structure.
2608 */
2609 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2610 xfs_trans_cancel(tp, 0);
2611 break;
2612 }
2613 xfs_ilock(ip, XFS_ILOCK_EXCL);
2614 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2615 0, quota_flag);
2616 if (error)
2617 goto error1;
2618
2619 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2620 xfs_trans_ihold(tp, ip);
2621
2622 /*
2623 * Issue the xfs_bmapi() call to allocate the blocks
2624 */
2625 xfs_bmap_init(&free_list, &firstfsb);
2626 error = xfs_bmapi(tp, ip, startoffset_fsb,
2627 allocatesize_fsb, bmapi_flag,
2628 &firstfsb, 0, imapp, &nimaps,
2629 &free_list, NULL);
2630 if (error) {
2631 goto error0;
2632 }
2633
2634 /*
2635 * Complete the transaction
2636 */
2637 error = xfs_bmap_finish(&tp, &free_list, &committed);
2638 if (error) {
2639 goto error0;
2640 }
2641
2642 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2643 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2644 if (error) {
2645 break;
2646 }
2647
2648 allocated_fsb = imapp->br_blockcount;
2649
2650 if (nimaps == 0) {
2651 error = XFS_ERROR(ENOSPC);
2652 break;
2653 }
2654
2655 startoffset_fsb += allocated_fsb;
2656 allocatesize_fsb -= allocated_fsb;
2657 }
2658 dmapi_enospc_check:
2659 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
2660 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
2661 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
2662 ip, DM_RIGHT_NULL,
2663 ip, DM_RIGHT_NULL,
2664 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
2665 if (error == 0)
2666 goto retry; /* Maybe DMAPI app. has made space */
2667 /* else fall through with error from XFS_SEND_DATA */
2668 }
2669
2670 return error;
2671
2672 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
2673 xfs_bmap_cancel(&free_list);
2674 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
2675
2676 error1: /* Just cancel transaction */
2677 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2678 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2679 goto dmapi_enospc_check;
2680 }
2681
2682 /*
2683 * Zero file bytes between startoff and endoff inclusive.
2684 * The iolock is held exclusive and no blocks are buffered.
2685 *
2686 * This function is used by xfs_free_file_space() to zero
2687 * partial blocks when the range to free is not block aligned.
2688 * When unreserving space with boundaries that are not block
2689 * aligned we round up the start and round down the end
2690 * boundaries and then use this function to zero the parts of
2691 * the blocks that got dropped during the rounding.
2692 */
2693 STATIC int
2694 xfs_zero_remaining_bytes(
2695 xfs_inode_t *ip,
2696 xfs_off_t startoff,
2697 xfs_off_t endoff)
2698 {
2699 xfs_bmbt_irec_t imap;
2700 xfs_fileoff_t offset_fsb;
2701 xfs_off_t lastoffset;
2702 xfs_off_t offset;
2703 xfs_buf_t *bp;
2704 xfs_mount_t *mp = ip->i_mount;
2705 int nimap;
2706 int error = 0;
2707
2708 /*
2709 * Avoid doing I/O beyond eof - it's not necessary
2710 * since nothing can read beyond eof. The space will
2711 * be zeroed when the file is extended anyway.
2712 */
2713 if (startoff >= ip->i_size)
2714 return 0;
2715
2716 if (endoff > ip->i_size)
2717 endoff = ip->i_size;
2718
2719 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
2720 XFS_IS_REALTIME_INODE(ip) ?
2721 mp->m_rtdev_targp : mp->m_ddev_targp);
2722 if (!bp)
2723 return XFS_ERROR(ENOMEM);
2724
2725 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2726 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2727 nimap = 1;
2728 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
2729 NULL, 0, &imap, &nimap, NULL, NULL);
2730 if (error || nimap < 1)
2731 break;
2732 ASSERT(imap.br_blockcount >= 1);
2733 ASSERT(imap.br_startoff == offset_fsb);
2734 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2735 if (lastoffset > endoff)
2736 lastoffset = endoff;
2737 if (imap.br_startblock == HOLESTARTBLOCK)
2738 continue;
2739 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2740 if (imap.br_state == XFS_EXT_UNWRITTEN)
2741 continue;
2742 XFS_BUF_UNDONE(bp);
2743 XFS_BUF_UNWRITE(bp);
2744 XFS_BUF_READ(bp);
2745 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
2746 xfsbdstrat(mp, bp);
2747 error = xfs_iowait(bp);
2748 if (error) {
2749 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2750 mp, bp, XFS_BUF_ADDR(bp));
2751 break;
2752 }
2753 memset(XFS_BUF_PTR(bp) +
2754 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2755 0, lastoffset - offset + 1);
2756 XFS_BUF_UNDONE(bp);
2757 XFS_BUF_UNREAD(bp);
2758 XFS_BUF_WRITE(bp);
2759 xfsbdstrat(mp, bp);
2760 error = xfs_iowait(bp);
2761 if (error) {
2762 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2763 mp, bp, XFS_BUF_ADDR(bp));
2764 break;
2765 }
2766 }
2767 xfs_buf_free(bp);
2768 return error;
2769 }
2770
2771 /*
2772 * xfs_free_file_space()
2773 * This routine frees disk space for the given file.
2774 *
2775 * This routine is only called by xfs_change_file_space
2776 * for an UNRESVSP type call.
2777 *
2778 * RETURNS:
2779 * 0 on success
2780 * errno on error
2781 *
2782 */
2783 STATIC int
2784 xfs_free_file_space(
2785 xfs_inode_t *ip,
2786 xfs_off_t offset,
2787 xfs_off_t len,
2788 int attr_flags)
2789 {
2790 int committed;
2791 int done;
2792 xfs_off_t end_dmi_offset;
2793 xfs_fileoff_t endoffset_fsb;
2794 int error;
2795 xfs_fsblock_t firstfsb;
2796 xfs_bmap_free_t free_list;
2797 xfs_bmbt_irec_t imap;
2798 xfs_off_t ioffset;
2799 xfs_extlen_t mod=0;
2800 xfs_mount_t *mp;
2801 int nimap;
2802 uint resblks;
2803 uint rounding;
2804 int rt;
2805 xfs_fileoff_t startoffset_fsb;
2806 xfs_trans_t *tp;
2807 int need_iolock = 1;
2808
2809 mp = ip->i_mount;
2810
2811 xfs_itrace_entry(ip);
2812
2813 error = xfs_qm_dqattach(ip, 0);
2814 if (error)
2815 return error;
2816
2817 error = 0;
2818 if (len <= 0) /* if nothing being freed */
2819 return error;
2820 rt = XFS_IS_REALTIME_INODE(ip);
2821 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2822 end_dmi_offset = offset + len;
2823 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2824
2825 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
2826 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2827 if (end_dmi_offset > ip->i_size)
2828 end_dmi_offset = ip->i_size;
2829 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
2830 offset, end_dmi_offset - offset,
2831 AT_DELAY_FLAG(attr_flags), NULL);
2832 if (error)
2833 return error;
2834 }
2835
2836 if (attr_flags & XFS_ATTR_NOLOCK)
2837 need_iolock = 0;
2838 if (need_iolock) {
2839 xfs_ilock(ip, XFS_IOLOCK_EXCL);
2840 /* wait for the completion of any pending DIOs */
2841 xfs_ioend_wait(ip);
2842 }
2843
2844 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2845 ioffset = offset & ~(rounding - 1);
2846
2847 if (VN_CACHED(VFS_I(ip)) != 0) {
2848 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
2849 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
2850 if (error)
2851 goto out_unlock_iolock;
2852 }
2853
2854 /*
2855 * Need to zero the stuff we're not freeing, on disk.
2856 * If it's a realtime file & can't use unwritten extents then we
2857 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2858 * will take care of it for us.
2859 */
2860 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
2861 nimap = 1;
2862 error = xfs_bmapi(NULL, ip, startoffset_fsb,
2863 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
2864 if (error)
2865 goto out_unlock_iolock;
2866 ASSERT(nimap == 0 || nimap == 1);
2867 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2868 xfs_daddr_t block;
2869
2870 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2871 block = imap.br_startblock;
2872 mod = do_div(block, mp->m_sb.sb_rextsize);
2873 if (mod)
2874 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2875 }
2876 nimap = 1;
2877 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
2878 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
2879 if (error)
2880 goto out_unlock_iolock;
2881 ASSERT(nimap == 0 || nimap == 1);
2882 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2883 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2884 mod++;
2885 if (mod && (mod != mp->m_sb.sb_rextsize))
2886 endoffset_fsb -= mod;
2887 }
2888 }
2889 if ((done = (endoffset_fsb <= startoffset_fsb)))
2890 /*
2891 * One contiguous piece to clear
2892 */
2893 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2894 else {
2895 /*
2896 * Some full blocks, possibly two pieces to clear
2897 */
2898 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2899 error = xfs_zero_remaining_bytes(ip, offset,
2900 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2901 if (!error &&
2902 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2903 error = xfs_zero_remaining_bytes(ip,
2904 XFS_FSB_TO_B(mp, endoffset_fsb),
2905 offset + len - 1);
2906 }
2907
2908 /*
2909 * free file space until done or until there is an error
2910 */
2911 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2912 while (!error && !done) {
2913
2914 /*
2915 * allocate and setup the transaction. Allow this
2916 * transaction to dip into the reserve blocks to ensure
2917 * the freeing of the space succeeds at ENOSPC.
2918 */
2919 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2920 tp->t_flags |= XFS_TRANS_RESERVE;
2921 error = xfs_trans_reserve(tp,
2922 resblks,
2923 XFS_WRITE_LOG_RES(mp),
2924 0,
2925 XFS_TRANS_PERM_LOG_RES,
2926 XFS_WRITE_LOG_COUNT);
2927
2928 /*
2929 * check for running out of space
2930 */
2931 if (error) {
2932 /*
2933 * Free the transaction structure.
2934 */
2935 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2936 xfs_trans_cancel(tp, 0);
2937 break;
2938 }
2939 xfs_ilock(ip, XFS_ILOCK_EXCL);
2940 error = xfs_trans_reserve_quota(tp, mp,
2941 ip->i_udquot, ip->i_gdquot,
2942 resblks, 0, XFS_QMOPT_RES_REGBLKS);
2943 if (error)
2944 goto error1;
2945
2946 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2947 xfs_trans_ihold(tp, ip);
2948
2949 /*
2950 * issue the bunmapi() call to free the blocks
2951 */
2952 xfs_bmap_init(&free_list, &firstfsb);
2953 error = xfs_bunmapi(tp, ip, startoffset_fsb,
2954 endoffset_fsb - startoffset_fsb,
2955 0, 2, &firstfsb, &free_list, NULL, &done);
2956 if (error) {
2957 goto error0;
2958 }
2959
2960 /*
2961 * complete the transaction
2962 */
2963 error = xfs_bmap_finish(&tp, &free_list, &committed);
2964 if (error) {
2965 goto error0;
2966 }
2967
2968 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2969 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2970 }
2971
2972 out_unlock_iolock:
2973 if (need_iolock)
2974 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2975 return error;
2976
2977 error0:
2978 xfs_bmap_cancel(&free_list);
2979 error1:
2980 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2981 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2982 XFS_ILOCK_EXCL);
2983 return error;
2984 }
2985
2986 /*
2987 * xfs_change_file_space()
2988 * This routine allocates or frees disk space for the given file.
2989 * The user specified parameters are checked for alignment and size
2990 * limitations.
2991 *
2992 * RETURNS:
2993 * 0 on success
2994 * errno on error
2995 *
2996 */
2997 int
2998 xfs_change_file_space(
2999 xfs_inode_t *ip,
3000 int cmd,
3001 xfs_flock64_t *bf,
3002 xfs_off_t offset,
3003 int attr_flags)
3004 {
3005 xfs_mount_t *mp = ip->i_mount;
3006 int clrprealloc;
3007 int error;
3008 xfs_fsize_t fsize;
3009 int setprealloc;
3010 xfs_off_t startoffset;
3011 xfs_off_t llen;
3012 xfs_trans_t *tp;
3013 struct iattr iattr;
3014
3015 xfs_itrace_entry(ip);
3016
3017 if (!S_ISREG(ip->i_d.di_mode))
3018 return XFS_ERROR(EINVAL);
3019
3020 switch (bf->l_whence) {
3021 case 0: /*SEEK_SET*/
3022 break;
3023 case 1: /*SEEK_CUR*/
3024 bf->l_start += offset;
3025 break;
3026 case 2: /*SEEK_END*/
3027 bf->l_start += ip->i_size;
3028 break;
3029 default:
3030 return XFS_ERROR(EINVAL);
3031 }
3032
3033 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3034
3035 if ( (bf->l_start < 0)
3036 || (bf->l_start > XFS_MAXIOFFSET(mp))
3037 || (bf->l_start + llen < 0)
3038 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3039 return XFS_ERROR(EINVAL);
3040
3041 bf->l_whence = 0;
3042
3043 startoffset = bf->l_start;
3044 fsize = ip->i_size;
3045
3046 /*
3047 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3048 * file space.
3049 * These calls do NOT zero the data space allocated to the file,
3050 * nor do they change the file size.
3051 *
3052 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3053 * space.
3054 * These calls cause the new file data to be zeroed and the file
3055 * size to be changed.
3056 */
3057 setprealloc = clrprealloc = 0;
3058
3059 switch (cmd) {
3060 case XFS_IOC_RESVSP:
3061 case XFS_IOC_RESVSP64:
3062 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3063 1, attr_flags);
3064 if (error)
3065 return error;
3066 setprealloc = 1;
3067 break;
3068
3069 case XFS_IOC_UNRESVSP:
3070 case XFS_IOC_UNRESVSP64:
3071 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3072 attr_flags)))
3073 return error;
3074 break;
3075
3076 case XFS_IOC_ALLOCSP:
3077 case XFS_IOC_ALLOCSP64:
3078 case XFS_IOC_FREESP:
3079 case XFS_IOC_FREESP64:
3080 if (startoffset > fsize) {
3081 error = xfs_alloc_file_space(ip, fsize,
3082 startoffset - fsize, 0, attr_flags);
3083 if (error)
3084 break;
3085 }
3086
3087 iattr.ia_valid = ATTR_SIZE;
3088 iattr.ia_size = startoffset;
3089
3090 error = xfs_setattr(ip, &iattr, attr_flags);
3091
3092 if (error)
3093 return error;
3094
3095 clrprealloc = 1;
3096 break;
3097
3098 default:
3099 ASSERT(0);
3100 return XFS_ERROR(EINVAL);
3101 }
3102
3103 /*
3104 * update the inode timestamp, mode, and prealloc flag bits
3105 */
3106 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3107
3108 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3109 0, 0, 0))) {
3110 /* ASSERT(0); */
3111 xfs_trans_cancel(tp, 0);
3112 return error;
3113 }
3114
3115 xfs_ilock(ip, XFS_ILOCK_EXCL);
3116
3117 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3118 xfs_trans_ihold(tp, ip);
3119
3120 if ((attr_flags & XFS_ATTR_DMI) == 0) {
3121 ip->i_d.di_mode &= ~S_ISUID;
3122
3123 /*
3124 * Note that we don't have to worry about mandatory
3125 * file locking being disabled here because we only
3126 * clear the S_ISGID bit if the Group execute bit is
3127 * on, but if it was on then mandatory locking wouldn't
3128 * have been enabled.
3129 */
3130 if (ip->i_d.di_mode & S_IXGRP)
3131 ip->i_d.di_mode &= ~S_ISGID;
3132
3133 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3134 }
3135 if (setprealloc)
3136 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3137 else if (clrprealloc)
3138 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3139
3140 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3141 xfs_trans_set_sync(tp);
3142
3143 error = xfs_trans_commit(tp, 0);
3144
3145 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3146
3147 return error;
3148 }
This page took 0.10749 seconds and 6 git commands to generate.