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