Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-drm-fsl-dcu.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_attr.h"
46 #include "xfs_rw.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_utils.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_refcache.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_log_priv.h"
54 #include "xfs_mac.h"
55
56 STATIC int
57 xfs_open(
58         bhv_desc_t      *bdp,
59         cred_t          *credp)
60 {
61         int             mode;
62         bhv_vnode_t     *vp = BHV_TO_VNODE(bdp);
63         xfs_inode_t     *ip = XFS_BHVTOI(bdp);
64
65         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
66                 return XFS_ERROR(EIO);
67
68         /*
69          * If it's a directory with any blocks, read-ahead block 0
70          * as we're almost certain to have the next operation be a read there.
71          */
72         if (VN_ISDIR(vp) && ip->i_d.di_nextents > 0) {
73                 mode = xfs_ilock_map_shared(ip);
74                 if (ip->i_d.di_nextents > 0)
75                         (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
76                 xfs_iunlock(ip, mode);
77         }
78         return 0;
79 }
80
81 STATIC int
82 xfs_close(
83         bhv_desc_t      *bdp,
84         int             flags,
85         lastclose_t     lastclose,
86         cred_t          *credp)
87 {
88         bhv_vnode_t     *vp = BHV_TO_VNODE(bdp);
89         xfs_inode_t     *ip = XFS_BHVTOI(bdp);
90
91         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
92                 return XFS_ERROR(EIO);
93
94         if (lastclose != L_TRUE || !VN_ISREG(vp))
95                 return 0;
96
97         /*
98          * If we previously truncated this file and removed old data in
99          * the process, we want to initiate "early" writeout on the last
100          * close.  This is an attempt to combat the notorious NULL files
101          * problem which is particularly noticable from a truncate down,
102          * buffered (re-)write (delalloc), followed by a crash.  What we
103          * are effectively doing here is significantly reducing the time
104          * window where we'd otherwise be exposed to that problem.
105          */
106         if (VUNTRUNCATE(vp) && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
107                 return bhv_vop_flush_pages(vp, 0, -1, XFS_B_ASYNC, FI_NONE);
108         return 0;
109 }
110
111 /*
112  * xfs_getattr
113  */
114 STATIC int
115 xfs_getattr(
116         bhv_desc_t      *bdp,
117         bhv_vattr_t     *vap,
118         int             flags,
119         cred_t          *credp)
120 {
121         xfs_inode_t     *ip;
122         xfs_mount_t     *mp;
123         bhv_vnode_t     *vp;
124
125         vp  = BHV_TO_VNODE(bdp);
126         vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
127
128         ip = XFS_BHVTOI(bdp);
129         mp = ip->i_mount;
130
131         if (XFS_FORCED_SHUTDOWN(mp))
132                 return XFS_ERROR(EIO);
133
134         if (!(flags & ATTR_LAZY))
135                 xfs_ilock(ip, XFS_ILOCK_SHARED);
136
137         vap->va_size = ip->i_d.di_size;
138         if (vap->va_mask == XFS_AT_SIZE)
139                 goto all_done;
140
141         vap->va_nblocks =
142                 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
143         vap->va_nodeid = ip->i_ino;
144 #if XFS_BIG_INUMS
145         vap->va_nodeid += mp->m_inoadd;
146 #endif
147         vap->va_nlink = ip->i_d.di_nlink;
148
149         /*
150          * Quick exit for non-stat callers
151          */
152         if ((vap->va_mask &
153             ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
154               XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
155                 goto all_done;
156
157         /*
158          * Copy from in-core inode.
159          */
160         vap->va_mode = ip->i_d.di_mode;
161         vap->va_uid = ip->i_d.di_uid;
162         vap->va_gid = ip->i_d.di_gid;
163         vap->va_projid = ip->i_d.di_projid;
164
165         /*
166          * Check vnode type block/char vs. everything else.
167          */
168         switch (ip->i_d.di_mode & S_IFMT) {
169         case S_IFBLK:
170         case S_IFCHR:
171                 vap->va_rdev = ip->i_df.if_u2.if_rdev;
172                 vap->va_blocksize = BLKDEV_IOSIZE;
173                 break;
174         default:
175                 vap->va_rdev = 0;
176
177                 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
178                         vap->va_blocksize = xfs_preferred_iosize(mp);
179                 } else {
180
181                         /*
182                          * If the file blocks are being allocated from a
183                          * realtime partition, then return the inode's
184                          * realtime extent size or the realtime volume's
185                          * extent size.
186                          */
187                         vap->va_blocksize = ip->i_d.di_extsize ?
188                                 (ip->i_d.di_extsize << mp->m_sb.sb_blocklog) :
189                                 (mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog);
190                 }
191                 break;
192         }
193
194         vn_atime_to_timespec(vp, &vap->va_atime);
195         vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
196         vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
197         vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
198         vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
199
200         /*
201          * Exit for stat callers.  See if any of the rest of the fields
202          * to be filled in are needed.
203          */
204         if ((vap->va_mask &
205              (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
206               XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
207                 goto all_done;
208
209         /*
210          * Convert di_flags to xflags.
211          */
212         vap->va_xflags = xfs_ip2xflags(ip);
213
214         /*
215          * Exit for inode revalidate.  See if any of the rest of
216          * the fields to be filled in are needed.
217          */
218         if ((vap->va_mask &
219              (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
220               XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
221                 goto all_done;
222
223         vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
224         vap->va_nextents =
225                 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
226                         ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
227                         ip->i_d.di_nextents;
228         if (ip->i_afp)
229                 vap->va_anextents =
230                         (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
231                                 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
232                                  ip->i_d.di_anextents;
233         else
234                 vap->va_anextents = 0;
235         vap->va_gen = ip->i_d.di_gen;
236
237  all_done:
238         if (!(flags & ATTR_LAZY))
239                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
240         return 0;
241 }
242
243
244 /*
245  * xfs_setattr
246  */
247 int
248 xfs_setattr(
249         bhv_desc_t              *bdp,
250         bhv_vattr_t             *vap,
251         int                     flags,
252         cred_t                  *credp)
253 {
254         xfs_inode_t             *ip;
255         xfs_trans_t             *tp;
256         xfs_mount_t             *mp;
257         int                     mask;
258         int                     code;
259         uint                    lock_flags;
260         uint                    commit_flags=0;
261         uid_t                   uid=0, iuid=0;
262         gid_t                   gid=0, igid=0;
263         int                     timeflags = 0;
264         bhv_vnode_t             *vp;
265         xfs_prid_t              projid=0, iprojid=0;
266         int                     mandlock_before, mandlock_after;
267         struct xfs_dquot        *udqp, *gdqp, *olddquot1, *olddquot2;
268         int                     file_owner;
269         int                     need_iolock = 1;
270
271         vp = BHV_TO_VNODE(bdp);
272         vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
273
274         if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
275                 return XFS_ERROR(EROFS);
276
277         /*
278          * Cannot set certain attributes.
279          */
280         mask = vap->va_mask;
281         if (mask & XFS_AT_NOSET) {
282                 return XFS_ERROR(EINVAL);
283         }
284
285         ip = XFS_BHVTOI(bdp);
286         mp = ip->i_mount;
287
288         if (XFS_FORCED_SHUTDOWN(mp))
289                 return XFS_ERROR(EIO);
290
291         /*
292          * Timestamps do not need to be logged and hence do not
293          * need to be done within a transaction.
294          */
295         if (mask & XFS_AT_UPDTIMES) {
296                 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
297                 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
298                             ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
299                             ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
300                 xfs_ichgtime(ip, timeflags);
301                 return 0;
302         }
303
304         olddquot1 = olddquot2 = NULL;
305         udqp = gdqp = NULL;
306
307         /*
308          * If disk quotas is on, we make sure that the dquots do exist on disk,
309          * before we start any other transactions. Trying to do this later
310          * is messy. We don't care to take a readlock to look at the ids
311          * in inode here, because we can't hold it across the trans_reserve.
312          * If the IDs do change before we take the ilock, we're covered
313          * because the i_*dquot fields will get updated anyway.
314          */
315         if (XFS_IS_QUOTA_ON(mp) &&
316             (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
317                 uint    qflags = 0;
318
319                 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
320                         uid = vap->va_uid;
321                         qflags |= XFS_QMOPT_UQUOTA;
322                 } else {
323                         uid = ip->i_d.di_uid;
324                 }
325                 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
326                         gid = vap->va_gid;
327                         qflags |= XFS_QMOPT_GQUOTA;
328                 }  else {
329                         gid = ip->i_d.di_gid;
330                 }
331                 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
332                         projid = vap->va_projid;
333                         qflags |= XFS_QMOPT_PQUOTA;
334                 }  else {
335                         projid = ip->i_d.di_projid;
336                 }
337                 /*
338                  * We take a reference when we initialize udqp and gdqp,
339                  * so it is important that we never blindly double trip on
340                  * the same variable. See xfs_create() for an example.
341                  */
342                 ASSERT(udqp == NULL);
343                 ASSERT(gdqp == NULL);
344                 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
345                                          &udqp, &gdqp);
346                 if (code)
347                         return code;
348         }
349
350         /*
351          * For the other attributes, we acquire the inode lock and
352          * first do an error checking pass.
353          */
354         tp = NULL;
355         lock_flags = XFS_ILOCK_EXCL;
356         if (flags & ATTR_NOLOCK)
357                 need_iolock = 0;
358         if (!(mask & XFS_AT_SIZE)) {
359                 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
360                     (mp->m_flags & XFS_MOUNT_WSYNC)) {
361                         tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
362                         commit_flags = 0;
363                         if ((code = xfs_trans_reserve(tp, 0,
364                                                      XFS_ICHANGE_LOG_RES(mp), 0,
365                                                      0, 0))) {
366                                 lock_flags = 0;
367                                 goto error_return;
368                         }
369                 }
370         } else {
371                 if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
372                     !(flags & ATTR_DMI)) {
373                         int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
374                         code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
375                                 vap->va_size, 0, dmflags, NULL);
376                         if (code) {
377                                 lock_flags = 0;
378                                 goto error_return;
379                         }
380                 }
381                 if (need_iolock)
382                         lock_flags |= XFS_IOLOCK_EXCL;
383         }
384
385         xfs_ilock(ip, lock_flags);
386
387         /* boolean: are we the file owner? */
388         file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
389
390         /*
391          * Change various properties of a file.
392          * Only the owner or users with CAP_FOWNER
393          * capability may do these things.
394          */
395         if (mask &
396             (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
397              XFS_AT_GID|XFS_AT_PROJID)) {
398                 /*
399                  * CAP_FOWNER overrides the following restrictions:
400                  *
401                  * The user ID of the calling process must be equal
402                  * to the file owner ID, except in cases where the
403                  * CAP_FSETID capability is applicable.
404                  */
405                 if (!file_owner && !capable(CAP_FOWNER)) {
406                         code = XFS_ERROR(EPERM);
407                         goto error_return;
408                 }
409
410                 /*
411                  * CAP_FSETID overrides the following restrictions:
412                  *
413                  * The effective user ID of the calling process shall match
414                  * the file owner when setting the set-user-ID and
415                  * set-group-ID bits on that file.
416                  *
417                  * The effective group ID or one of the supplementary group
418                  * IDs of the calling process shall match the group owner of
419                  * the file when setting the set-group-ID bit on that file
420                  */
421                 if (mask & XFS_AT_MODE) {
422                         mode_t m = 0;
423
424                         if ((vap->va_mode & S_ISUID) && !file_owner)
425                                 m |= S_ISUID;
426                         if ((vap->va_mode & S_ISGID) &&
427                             !in_group_p((gid_t)ip->i_d.di_gid))
428                                 m |= S_ISGID;
429 #if 0
430                         /* Linux allows this, Irix doesn't. */
431                         if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
432                                 m |= S_ISVTX;
433 #endif
434                         if (m && !capable(CAP_FSETID))
435                                 vap->va_mode &= ~m;
436                 }
437         }
438
439         /*
440          * Change file ownership.  Must be the owner or privileged.
441          * If the system was configured with the "restricted_chown"
442          * option, the owner is not permitted to give away the file,
443          * and can change the group id only to a group of which he
444          * or she is a member.
445          */
446         if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
447                 /*
448                  * These IDs could have changed since we last looked at them.
449                  * But, we're assured that if the ownership did change
450                  * while we didn't have the inode locked, inode's dquot(s)
451                  * would have changed also.
452                  */
453                 iuid = ip->i_d.di_uid;
454                 iprojid = ip->i_d.di_projid;
455                 igid = ip->i_d.di_gid;
456                 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
457                 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
458                 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
459                          iprojid;
460
461                 /*
462                  * CAP_CHOWN overrides the following restrictions:
463                  *
464                  * If _POSIX_CHOWN_RESTRICTED is defined, this capability
465                  * shall override the restriction that a process cannot
466                  * change the user ID of a file it owns and the restriction
467                  * that the group ID supplied to the chown() function
468                  * shall be equal to either the group ID or one of the
469                  * supplementary group IDs of the calling process.
470                  */
471                 if (restricted_chown &&
472                     (iuid != uid || (igid != gid &&
473                                      !in_group_p((gid_t)gid))) &&
474                     !capable(CAP_CHOWN)) {
475                         code = XFS_ERROR(EPERM);
476                         goto error_return;
477                 }
478                 /*
479                  * Do a quota reservation only if uid/projid/gid is actually
480                  * going to change.
481                  */
482                 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
483                     (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
484                     (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
485                         ASSERT(tp);
486                         code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
487                                                 capable(CAP_FOWNER) ?
488                                                 XFS_QMOPT_FORCE_RES : 0);
489                         if (code)       /* out of quota */
490                                 goto error_return;
491                 }
492         }
493
494         /*
495          * Truncate file.  Must have write permission and not be a directory.
496          */
497         if (mask & XFS_AT_SIZE) {
498                 /* Short circuit the truncate case for zero length files */
499                 if ((vap->va_size == 0) &&
500                    (ip->i_d.di_size == 0) && (ip->i_d.di_nextents == 0)) {
501                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
502                         lock_flags &= ~XFS_ILOCK_EXCL;
503                         if (mask & XFS_AT_CTIME)
504                                 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
505                         code = 0;
506                         goto error_return;
507                 }
508
509                 if (VN_ISDIR(vp)) {
510                         code = XFS_ERROR(EISDIR);
511                         goto error_return;
512                 } else if (!VN_ISREG(vp)) {
513                         code = XFS_ERROR(EINVAL);
514                         goto error_return;
515                 }
516                 /*
517                  * Make sure that the dquots are attached to the inode.
518                  */
519                 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
520                         goto error_return;
521         }
522
523         /*
524          * Change file access or modified times.
525          */
526         if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
527                 if (!file_owner) {
528                         if ((flags & ATTR_UTIME) &&
529                             !capable(CAP_FOWNER)) {
530                                 code = XFS_ERROR(EPERM);
531                                 goto error_return;
532                         }
533                 }
534         }
535
536         /*
537          * Change extent size or realtime flag.
538          */
539         if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
540                 /*
541                  * Can't change extent size if any extents are allocated.
542                  */
543                 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
544                     ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
545                      vap->va_extsize) ) {
546                         code = XFS_ERROR(EINVAL);       /* EFBIG? */
547                         goto error_return;
548                 }
549
550                 /*
551                  * Can't change realtime flag if any extents are allocated.
552                  */
553                 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
554                     (mask & XFS_AT_XFLAGS) &&
555                     (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
556                     (vap->va_xflags & XFS_XFLAG_REALTIME)) {
557                         code = XFS_ERROR(EINVAL);       /* EFBIG? */
558                         goto error_return;
559                 }
560                 /*
561                  * Extent size must be a multiple of the appropriate block
562                  * size, if set at all.
563                  */
564                 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
565                         xfs_extlen_t    size;
566
567                         if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
568                             ((mask & XFS_AT_XFLAGS) &&
569                             (vap->va_xflags & XFS_XFLAG_REALTIME))) {
570                                 size = mp->m_sb.sb_rextsize <<
571                                        mp->m_sb.sb_blocklog;
572                         } else {
573                                 size = mp->m_sb.sb_blocksize;
574                         }
575                         if (vap->va_extsize % size) {
576                                 code = XFS_ERROR(EINVAL);
577                                 goto error_return;
578                         }
579                 }
580                 /*
581                  * If realtime flag is set then must have realtime data.
582                  */
583                 if ((mask & XFS_AT_XFLAGS) &&
584                     (vap->va_xflags & XFS_XFLAG_REALTIME)) {
585                         if ((mp->m_sb.sb_rblocks == 0) ||
586                             (mp->m_sb.sb_rextsize == 0) ||
587                             (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
588                                 code = XFS_ERROR(EINVAL);
589                                 goto error_return;
590                         }
591                 }
592
593                 /*
594                  * Can't modify an immutable/append-only file unless
595                  * we have appropriate permission.
596                  */
597                 if ((mask & XFS_AT_XFLAGS) &&
598                     (ip->i_d.di_flags &
599                                 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
600                      (vap->va_xflags &
601                                 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
602                     !capable(CAP_LINUX_IMMUTABLE)) {
603                         code = XFS_ERROR(EPERM);
604                         goto error_return;
605                 }
606         }
607
608         /*
609          * Now we can make the changes.  Before we join the inode
610          * to the transaction, if XFS_AT_SIZE is set then take care of
611          * the part of the truncation that must be done without the
612          * inode lock.  This needs to be done before joining the inode
613          * to the transaction, because the inode cannot be unlocked
614          * once it is a part of the transaction.
615          */
616         if (mask & XFS_AT_SIZE) {
617                 code = 0;
618                 if ((vap->va_size > ip->i_d.di_size) && 
619                     (flags & ATTR_NOSIZETOK) == 0) {
620                         code = xfs_igrow_start(ip, vap->va_size, credp);
621                 }
622                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
623                 vn_iowait(vp); /* wait for the completion of any pending DIOs */
624                 if (!code)
625                         code = xfs_itruncate_data(ip, vap->va_size);
626                 if (code) {
627                         ASSERT(tp == NULL);
628                         lock_flags &= ~XFS_ILOCK_EXCL;
629                         ASSERT(lock_flags == XFS_IOLOCK_EXCL);
630                         goto error_return;
631                 }
632                 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
633                 if ((code = xfs_trans_reserve(tp, 0,
634                                              XFS_ITRUNCATE_LOG_RES(mp), 0,
635                                              XFS_TRANS_PERM_LOG_RES,
636                                              XFS_ITRUNCATE_LOG_COUNT))) {
637                         xfs_trans_cancel(tp, 0);
638                         if (need_iolock)
639                                 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
640                         return code;
641                 }
642                 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
643                 xfs_ilock(ip, XFS_ILOCK_EXCL);
644         }
645
646         if (tp) {
647                 xfs_trans_ijoin(tp, ip, lock_flags);
648                 xfs_trans_ihold(tp, ip);
649         }
650
651         /* determine whether mandatory locking mode changes */
652         mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
653
654         /*
655          * Truncate file.  Must have write permission and not be a directory.
656          */
657         if (mask & XFS_AT_SIZE) {
658                 if (vap->va_size > ip->i_d.di_size) {
659                         xfs_igrow_finish(tp, ip, vap->va_size,
660                             !(flags & ATTR_DMI));
661                 } else if ((vap->va_size <= ip->i_d.di_size) ||
662                            ((vap->va_size == 0) && ip->i_d.di_nextents)) {
663                         /*
664                          * signal a sync transaction unless
665                          * we're truncating an already unlinked
666                          * file on a wsync filesystem
667                          */
668                         code = xfs_itruncate_finish(&tp, ip,
669                                             (xfs_fsize_t)vap->va_size,
670                                             XFS_DATA_FORK,
671                                             ((ip->i_d.di_nlink != 0 ||
672                                               !(mp->m_flags & XFS_MOUNT_WSYNC))
673                                              ? 1 : 0));
674                         if (code)
675                                 goto abort_return;
676                         /*
677                          * Truncated "down", so we're removing references
678                          * to old data here - if we now delay flushing for
679                          * a long time, we expose ourselves unduly to the
680                          * notorious NULL files problem.  So, we mark this
681                          * vnode and flush it when the file is closed, and
682                          * do not wait the usual (long) time for writeout.
683                          */
684                         VTRUNCATE(vp);
685                 }
686                 /*
687                  * Have to do this even if the file's size doesn't change.
688                  */
689                 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
690         }
691
692         /*
693          * Change file access modes.
694          */
695         if (mask & XFS_AT_MODE) {
696                 ip->i_d.di_mode &= S_IFMT;
697                 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
698
699                 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
700                 timeflags |= XFS_ICHGTIME_CHG;
701         }
702
703         /*
704          * Change file ownership.  Must be the owner or privileged.
705          * If the system was configured with the "restricted_chown"
706          * option, the owner is not permitted to give away the file,
707          * and can change the group id only to a group of which he
708          * or she is a member.
709          */
710         if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
711                 /*
712                  * CAP_FSETID overrides the following restrictions:
713                  *
714                  * The set-user-ID and set-group-ID bits of a file will be
715                  * cleared upon successful return from chown()
716                  */
717                 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
718                     !capable(CAP_FSETID)) {
719                         ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
720                 }
721
722                 /*
723                  * Change the ownerships and register quota modifications
724                  * in the transaction.
725                  */
726                 if (iuid != uid) {
727                         if (XFS_IS_UQUOTA_ON(mp)) {
728                                 ASSERT(mask & XFS_AT_UID);
729                                 ASSERT(udqp);
730                                 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
731                                                         &ip->i_udquot, udqp);
732                         }
733                         ip->i_d.di_uid = uid;
734                 }
735                 if (igid != gid) {
736                         if (XFS_IS_GQUOTA_ON(mp)) {
737                                 ASSERT(!XFS_IS_PQUOTA_ON(mp));
738                                 ASSERT(mask & XFS_AT_GID);
739                                 ASSERT(gdqp);
740                                 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
741                                                         &ip->i_gdquot, gdqp);
742                         }
743                         ip->i_d.di_gid = gid;
744                 }
745                 if (iprojid != projid) {
746                         if (XFS_IS_PQUOTA_ON(mp)) {
747                                 ASSERT(!XFS_IS_GQUOTA_ON(mp));
748                                 ASSERT(mask & XFS_AT_PROJID);
749                                 ASSERT(gdqp);
750                                 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
751                                                         &ip->i_gdquot, gdqp);
752                         }
753                         ip->i_d.di_projid = projid;
754                         /*
755                          * We may have to rev the inode as well as
756                          * the superblock version number since projids didn't
757                          * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
758                          */
759                         if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
760                                 xfs_bump_ino_vers2(tp, ip);
761                 }
762
763                 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
764                 timeflags |= XFS_ICHGTIME_CHG;
765         }
766
767
768         /*
769          * Change file access or modified times.
770          */
771         if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
772                 if (mask & XFS_AT_ATIME) {
773                         ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
774                         ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
775                         ip->i_update_core = 1;
776                         timeflags &= ~XFS_ICHGTIME_ACC;
777                 }
778                 if (mask & XFS_AT_MTIME) {
779                         ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
780                         ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
781                         timeflags &= ~XFS_ICHGTIME_MOD;
782                         timeflags |= XFS_ICHGTIME_CHG;
783                 }
784                 if (tp && (flags & ATTR_UTIME))
785                         xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
786         }
787
788         /*
789          * Change XFS-added attributes.
790          */
791         if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
792                 if (mask & XFS_AT_EXTSIZE) {
793                         /*
794                          * Converting bytes to fs blocks.
795                          */
796                         ip->i_d.di_extsize = vap->va_extsize >>
797                                 mp->m_sb.sb_blocklog;
798                 }
799                 if (mask & XFS_AT_XFLAGS) {
800                         uint    di_flags;
801
802                         /* can't set PREALLOC this way, just preserve it */
803                         di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
804                         if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
805                                 di_flags |= XFS_DIFLAG_IMMUTABLE;
806                         if (vap->va_xflags & XFS_XFLAG_APPEND)
807                                 di_flags |= XFS_DIFLAG_APPEND;
808                         if (vap->va_xflags & XFS_XFLAG_SYNC)
809                                 di_flags |= XFS_DIFLAG_SYNC;
810                         if (vap->va_xflags & XFS_XFLAG_NOATIME)
811                                 di_flags |= XFS_DIFLAG_NOATIME;
812                         if (vap->va_xflags & XFS_XFLAG_NODUMP)
813                                 di_flags |= XFS_DIFLAG_NODUMP;
814                         if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
815                                 di_flags |= XFS_DIFLAG_PROJINHERIT;
816                         if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
817                                 di_flags |= XFS_DIFLAG_NODEFRAG;
818                         if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
819                                 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
820                                         di_flags |= XFS_DIFLAG_RTINHERIT;
821                                 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
822                                         di_flags |= XFS_DIFLAG_NOSYMLINKS;
823                                 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
824                                         di_flags |= XFS_DIFLAG_EXTSZINHERIT;
825                         } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
826                                 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
827                                         di_flags |= XFS_DIFLAG_REALTIME;
828                                         ip->i_iocore.io_flags |= XFS_IOCORE_RT;
829                                 } else {
830                                         ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
831                                 }
832                                 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
833                                         di_flags |= XFS_DIFLAG_EXTSIZE;
834                         }
835                         ip->i_d.di_flags = di_flags;
836                 }
837                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
838                 timeflags |= XFS_ICHGTIME_CHG;
839         }
840
841         /*
842          * Change file inode change time only if XFS_AT_CTIME set
843          * AND we have been called by a DMI function.
844          */
845
846         if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
847                 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
848                 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
849                 ip->i_update_core = 1;
850                 timeflags &= ~XFS_ICHGTIME_CHG;
851         }
852
853         /*
854          * Send out timestamp changes that need to be set to the
855          * current time.  Not done when called by a DMI function.
856          */
857         if (timeflags && !(flags & ATTR_DMI))
858                 xfs_ichgtime(ip, timeflags);
859
860         XFS_STATS_INC(xs_ig_attrchg);
861
862         /*
863          * If this is a synchronous mount, make sure that the
864          * transaction goes to disk before returning to the user.
865          * This is slightly sub-optimal in that truncates require
866          * two sync transactions instead of one for wsync filesystems.
867          * One for the truncate and one for the timestamps since we
868          * don't want to change the timestamps unless we're sure the
869          * truncate worked.  Truncates are less than 1% of the laddis
870          * mix so this probably isn't worth the trouble to optimize.
871          */
872         code = 0;
873         if (tp) {
874                 if (mp->m_flags & XFS_MOUNT_WSYNC)
875                         xfs_trans_set_sync(tp);
876
877                 code = xfs_trans_commit(tp, commit_flags, NULL);
878         }
879
880         /*
881          * If the (regular) file's mandatory locking mode changed, then
882          * notify the vnode.  We do this under the inode lock to prevent
883          * racing calls to vop_vnode_change.
884          */
885         mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
886         if (mandlock_before != mandlock_after) {
887                 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_ENF_LOCKING,
888                                  mandlock_after);
889         }
890
891         xfs_iunlock(ip, lock_flags);
892
893         /*
894          * Release any dquot(s) the inode had kept before chown.
895          */
896         XFS_QM_DQRELE(mp, olddquot1);
897         XFS_QM_DQRELE(mp, olddquot2);
898         XFS_QM_DQRELE(mp, udqp);
899         XFS_QM_DQRELE(mp, gdqp);
900
901         if (code) {
902                 return code;
903         }
904
905         if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
906             !(flags & ATTR_DMI)) {
907                 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
908                                         NULL, DM_RIGHT_NULL, NULL, NULL,
909                                         0, 0, AT_DELAY_FLAG(flags));
910         }
911         return 0;
912
913  abort_return:
914         commit_flags |= XFS_TRANS_ABORT;
915         /* FALLTHROUGH */
916  error_return:
917         XFS_QM_DQRELE(mp, udqp);
918         XFS_QM_DQRELE(mp, gdqp);
919         if (tp) {
920                 xfs_trans_cancel(tp, commit_flags);
921         }
922         if (lock_flags != 0) {
923                 xfs_iunlock(ip, lock_flags);
924         }
925         return code;
926 }
927
928
929 /*
930  * xfs_access
931  * Null conversion from vnode mode bits to inode mode bits, as in efs.
932  */
933 STATIC int
934 xfs_access(
935         bhv_desc_t      *bdp,
936         int             mode,
937         cred_t          *credp)
938 {
939         xfs_inode_t     *ip;
940         int             error;
941
942         vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
943                                                (inst_t *)__return_address);
944
945         ip = XFS_BHVTOI(bdp);
946         xfs_ilock(ip, XFS_ILOCK_SHARED);
947         error = xfs_iaccess(ip, mode, credp);
948         xfs_iunlock(ip, XFS_ILOCK_SHARED);
949         return error;
950 }
951
952
953 /*
954  * The maximum pathlen is 1024 bytes. Since the minimum file system
955  * blocksize is 512 bytes, we can get a max of 2 extents back from
956  * bmapi.
957  */
958 #define SYMLINK_MAPS 2
959
960 /*
961  * xfs_readlink
962  *
963  */
964 STATIC int
965 xfs_readlink(
966         bhv_desc_t      *bdp,
967         uio_t           *uiop,
968         int             ioflags,
969         cred_t          *credp)
970 {
971         xfs_inode_t     *ip;
972         int             count;
973         xfs_off_t       offset;
974         int             pathlen;
975         bhv_vnode_t     *vp;
976         int             error = 0;
977         xfs_mount_t     *mp;
978         int             nmaps;
979         xfs_bmbt_irec_t mval[SYMLINK_MAPS];
980         xfs_daddr_t     d;
981         int             byte_cnt;
982         int             n;
983         xfs_buf_t       *bp;
984
985         vp = BHV_TO_VNODE(bdp);
986         vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
987
988         ip = XFS_BHVTOI(bdp);
989         mp = ip->i_mount;
990
991         if (XFS_FORCED_SHUTDOWN(mp))
992                 return XFS_ERROR(EIO);
993
994         xfs_ilock(ip, XFS_ILOCK_SHARED);
995
996         ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
997
998         offset = uiop->uio_offset;
999         count = uiop->uio_resid;
1000
1001         if (offset < 0) {
1002                 error = XFS_ERROR(EINVAL);
1003                 goto error_return;
1004         }
1005         if (count <= 0) {
1006                 error = 0;
1007                 goto error_return;
1008         }
1009
1010         /*
1011          * See if the symlink is stored inline.
1012          */
1013         pathlen = (int)ip->i_d.di_size;
1014
1015         if (ip->i_df.if_flags & XFS_IFINLINE) {
1016                 error = uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
1017         }
1018         else {
1019                 /*
1020                  * Symlink not inline.  Call bmap to get it in.
1021                  */
1022                 nmaps = SYMLINK_MAPS;
1023
1024                 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
1025                                   0, NULL, 0, mval, &nmaps, NULL, NULL);
1026
1027                 if (error) {
1028                         goto error_return;
1029                 }
1030
1031                 for (n = 0; n < nmaps; n++) {
1032                         d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1033                         byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1034                         bp = xfs_buf_read(mp->m_ddev_targp, d,
1035                                       BTOBB(byte_cnt), 0);
1036                         error = XFS_BUF_GETERROR(bp);
1037                         if (error) {
1038                                 xfs_ioerror_alert("xfs_readlink",
1039                                           ip->i_mount, bp, XFS_BUF_ADDR(bp));
1040                                 xfs_buf_relse(bp);
1041                                 goto error_return;
1042                         }
1043                         if (pathlen < byte_cnt)
1044                                 byte_cnt = pathlen;
1045                         pathlen -= byte_cnt;
1046
1047                         error = uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1048                         xfs_buf_relse (bp);
1049                 }
1050
1051         }
1052
1053 error_return:
1054         xfs_iunlock(ip, XFS_ILOCK_SHARED);
1055         return error;
1056 }
1057
1058
1059 /*
1060  * xfs_fsync
1061  *
1062  * This is called to sync the inode and its data out to disk.
1063  * We need to hold the I/O lock while flushing the data, and
1064  * the inode lock while flushing the inode.  The inode lock CANNOT
1065  * be held while flushing the data, so acquire after we're done
1066  * with that.
1067  */
1068 STATIC int
1069 xfs_fsync(
1070         bhv_desc_t      *bdp,
1071         int             flag,
1072         cred_t          *credp,
1073         xfs_off_t       start,
1074         xfs_off_t       stop)
1075 {
1076         xfs_inode_t     *ip;
1077         xfs_trans_t     *tp;
1078         int             error;
1079         int             log_flushed = 0, changed = 1;
1080
1081         vn_trace_entry(BHV_TO_VNODE(bdp),
1082                         __FUNCTION__, (inst_t *)__return_address);
1083
1084         ip = XFS_BHVTOI(bdp);
1085
1086         ASSERT(start >= 0 && stop >= -1);
1087
1088         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1089                 return XFS_ERROR(EIO);
1090
1091         /*
1092          * We always need to make sure that the required inode state
1093          * is safe on disk.  The vnode might be clean but because
1094          * of committed transactions that haven't hit the disk yet.
1095          * Likewise, there could be unflushed non-transactional
1096          * changes to the inode core that have to go to disk.
1097          *
1098          * The following code depends on one assumption:  that
1099          * any transaction that changes an inode logs the core
1100          * because it has to change some field in the inode core
1101          * (typically nextents or nblocks).  That assumption
1102          * implies that any transactions against an inode will
1103          * catch any non-transactional updates.  If inode-altering
1104          * transactions exist that violate this assumption, the
1105          * code breaks.  Right now, it figures that if the involved
1106          * update_* field is clear and the inode is unpinned, the
1107          * inode is clean.  Either it's been flushed or it's been
1108          * committed and the commit has hit the disk unpinning the inode.
1109          * (Note that xfs_inode_item_format() called at commit clears
1110          * the update_* fields.)
1111          */
1112         xfs_ilock(ip, XFS_ILOCK_SHARED);
1113
1114         /* If we are flushing data then we care about update_size
1115          * being set, otherwise we care about update_core
1116          */
1117         if ((flag & FSYNC_DATA) ?
1118                         (ip->i_update_size == 0) :
1119                         (ip->i_update_core == 0)) {
1120                 /*
1121                  * Timestamps/size haven't changed since last inode
1122                  * flush or inode transaction commit.  That means
1123                  * either nothing got written or a transaction
1124                  * committed which caught the updates.  If the
1125                  * latter happened and the transaction hasn't
1126                  * hit the disk yet, the inode will be still
1127                  * be pinned.  If it is, force the log.
1128                  */
1129
1130                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1131
1132                 if (xfs_ipincount(ip)) {
1133                         _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1134                                       XFS_LOG_FORCE |
1135                                       ((flag & FSYNC_WAIT)
1136                                        ? XFS_LOG_SYNC : 0),
1137                                       &log_flushed);
1138                 } else {
1139                         /*
1140                          * If the inode is not pinned and nothing
1141                          * has changed we don't need to flush the
1142                          * cache.
1143                          */
1144                         changed = 0;
1145                 }
1146                 error = 0;
1147         } else  {
1148                 /*
1149                  * Kick off a transaction to log the inode
1150                  * core to get the updates.  Make it
1151                  * sync if FSYNC_WAIT is passed in (which
1152                  * is done by everybody but specfs).  The
1153                  * sync transaction will also force the log.
1154                  */
1155                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1156                 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1157                 if ((error = xfs_trans_reserve(tp, 0,
1158                                 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1159                                 0, 0, 0)))  {
1160                         xfs_trans_cancel(tp, 0);
1161                         return error;
1162                 }
1163                 xfs_ilock(ip, XFS_ILOCK_EXCL);
1164
1165                 /*
1166                  * Note - it's possible that we might have pushed
1167                  * ourselves out of the way during trans_reserve
1168                  * which would flush the inode.  But there's no
1169                  * guarantee that the inode buffer has actually
1170                  * gone out yet (it's delwri).  Plus the buffer
1171                  * could be pinned anyway if it's part of an
1172                  * inode in another recent transaction.  So we
1173                  * play it safe and fire off the transaction anyway.
1174                  */
1175                 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1176                 xfs_trans_ihold(tp, ip);
1177                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1178                 if (flag & FSYNC_WAIT)
1179                         xfs_trans_set_sync(tp);
1180                 error = _xfs_trans_commit(tp, 0, NULL, &log_flushed);
1181
1182                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1183         }
1184
1185         if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1186                 /*
1187                  * If the log write didn't issue an ordered tag we need
1188                  * to flush the disk cache for the data device now.
1189                  */
1190                 if (!log_flushed)
1191                         xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1192
1193                 /*
1194                  * If this inode is on the RT dev we need to flush that
1195                  * cache as well.
1196                  */
1197                 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1198                         xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1199         }
1200
1201         return error;
1202 }
1203
1204 /*
1205  * This is called by xfs_inactive to free any blocks beyond eof,
1206  * when the link count isn't zero.
1207  */
1208 STATIC int
1209 xfs_inactive_free_eofblocks(
1210         xfs_mount_t     *mp,
1211         xfs_inode_t     *ip)
1212 {
1213         xfs_trans_t     *tp;
1214         int             error;
1215         xfs_fileoff_t   end_fsb;
1216         xfs_fileoff_t   last_fsb;
1217         xfs_filblks_t   map_len;
1218         int             nimaps;
1219         xfs_bmbt_irec_t imap;
1220
1221         /*
1222          * Figure out if there are any blocks beyond the end
1223          * of the file.  If not, then there is nothing to do.
1224          */
1225         end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_d.di_size));
1226         last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1227         map_len = last_fsb - end_fsb;
1228         if (map_len <= 0)
1229                 return 0;
1230
1231         nimaps = 1;
1232         xfs_ilock(ip, XFS_ILOCK_SHARED);
1233         error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1234                           NULL, 0, &imap, &nimaps, NULL, NULL);
1235         xfs_iunlock(ip, XFS_ILOCK_SHARED);
1236
1237         if (!error && (nimaps != 0) &&
1238             (imap.br_startblock != HOLESTARTBLOCK ||
1239              ip->i_delayed_blks)) {
1240                 /*
1241                  * Attach the dquots to the inode up front.
1242                  */
1243                 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1244                         return error;
1245
1246                 /*
1247                  * There are blocks after the end of file.
1248                  * Free them up now by truncating the file to
1249                  * its current size.
1250                  */
1251                 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1252
1253                 /*
1254                  * Do the xfs_itruncate_start() call before
1255                  * reserving any log space because
1256                  * itruncate_start will call into the buffer
1257                  * cache and we can't
1258                  * do that within a transaction.
1259                  */
1260                 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1261                 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1262                                     ip->i_d.di_size);
1263
1264                 error = xfs_trans_reserve(tp, 0,
1265                                           XFS_ITRUNCATE_LOG_RES(mp),
1266                                           0, XFS_TRANS_PERM_LOG_RES,
1267                                           XFS_ITRUNCATE_LOG_COUNT);
1268                 if (error) {
1269                         ASSERT(XFS_FORCED_SHUTDOWN(mp));
1270                         xfs_trans_cancel(tp, 0);
1271                         xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1272                         return error;
1273                 }
1274
1275                 xfs_ilock(ip, XFS_ILOCK_EXCL);
1276                 xfs_trans_ijoin(tp, ip,
1277                                 XFS_IOLOCK_EXCL |
1278                                 XFS_ILOCK_EXCL);
1279                 xfs_trans_ihold(tp, ip);
1280
1281                 error = xfs_itruncate_finish(&tp, ip,
1282                                              ip->i_d.di_size,
1283                                              XFS_DATA_FORK,
1284                                              0);
1285                 /*
1286                  * If we get an error at this point we
1287                  * simply don't bother truncating the file.
1288                  */
1289                 if (error) {
1290                         xfs_trans_cancel(tp,
1291                                          (XFS_TRANS_RELEASE_LOG_RES |
1292                                           XFS_TRANS_ABORT));
1293                 } else {
1294                         error = xfs_trans_commit(tp,
1295                                                 XFS_TRANS_RELEASE_LOG_RES,
1296                                                 NULL);
1297                 }
1298                 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1299         }
1300         return error;
1301 }
1302
1303 /*
1304  * Free a symlink that has blocks associated with it.
1305  */
1306 STATIC int
1307 xfs_inactive_symlink_rmt(
1308         xfs_inode_t     *ip,
1309         xfs_trans_t     **tpp)
1310 {
1311         xfs_buf_t       *bp;
1312         int             committed;
1313         int             done;
1314         int             error;
1315         xfs_fsblock_t   first_block;
1316         xfs_bmap_free_t free_list;
1317         int             i;
1318         xfs_mount_t     *mp;
1319         xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1320         int             nmaps;
1321         xfs_trans_t     *ntp;
1322         int             size;
1323         xfs_trans_t     *tp;
1324
1325         tp = *tpp;
1326         mp = ip->i_mount;
1327         ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1328         /*
1329          * We're freeing a symlink that has some
1330          * blocks allocated to it.  Free the
1331          * blocks here.  We know that we've got
1332          * either 1 or 2 extents and that we can
1333          * free them all in one bunmapi call.
1334          */
1335         ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1336         if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1337                         XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1338                 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1339                 xfs_trans_cancel(tp, 0);
1340                 *tpp = NULL;
1341                 return error;
1342         }
1343         /*
1344          * Lock the inode, fix the size, and join it to the transaction.
1345          * Hold it so in the normal path, we still have it locked for
1346          * the second transaction.  In the error paths we need it
1347          * held so the cancel won't rele it, see below.
1348          */
1349         xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1350         size = (int)ip->i_d.di_size;
1351         ip->i_d.di_size = 0;
1352         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1353         xfs_trans_ihold(tp, ip);
1354         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1355         /*
1356          * Find the block(s) so we can inval and unmap them.
1357          */
1358         done = 0;
1359         XFS_BMAP_INIT(&free_list, &first_block);
1360         nmaps = ARRAY_SIZE(mval);
1361         if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1362                         XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1363                         &free_list, NULL)))
1364                 goto error0;
1365         /*
1366          * Invalidate the block(s).
1367          */
1368         for (i = 0; i < nmaps; i++) {
1369                 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1370                         XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1371                         XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1372                 xfs_trans_binval(tp, bp);
1373         }
1374         /*
1375          * Unmap the dead block(s) to the free_list.
1376          */
1377         if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1378                         &first_block, &free_list, NULL, &done)))
1379                 goto error1;
1380         ASSERT(done);
1381         /*
1382          * Commit the first transaction.  This logs the EFI and the inode.
1383          */
1384         if ((error = xfs_bmap_finish(&tp, &free_list, first_block, &committed)))
1385                 goto error1;
1386         /*
1387          * The transaction must have been committed, since there were
1388          * actually extents freed by xfs_bunmapi.  See xfs_bmap_finish.
1389          * The new tp has the extent freeing and EFDs.
1390          */
1391         ASSERT(committed);
1392         /*
1393          * The first xact was committed, so add the inode to the new one.
1394          * Mark it dirty so it will be logged and moved forward in the log as
1395          * part of every commit.
1396          */
1397         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1398         xfs_trans_ihold(tp, ip);
1399         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1400         /*
1401          * Get a new, empty transaction to return to our caller.
1402          */
1403         ntp = xfs_trans_dup(tp);
1404         /*
1405          * Commit the transaction containing extent freeing and EFDs.
1406          * If we get an error on the commit here or on the reserve below,
1407          * we need to unlock the inode since the new transaction doesn't
1408          * have the inode attached.
1409          */
1410         error = xfs_trans_commit(tp, 0, NULL);
1411         tp = ntp;
1412         if (error) {
1413                 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1414                 goto error0;
1415         }
1416         /*
1417          * Remove the memory for extent descriptions (just bookkeeping).
1418          */
1419         if (ip->i_df.if_bytes)
1420                 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1421         ASSERT(ip->i_df.if_bytes == 0);
1422         /*
1423          * Put an itruncate log reservation in the new transaction
1424          * for our caller.
1425          */
1426         if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1427                         XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1428                 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1429                 goto error0;
1430         }
1431         /*
1432          * Return with the inode locked but not joined to the transaction.
1433          */
1434         *tpp = tp;
1435         return 0;
1436
1437  error1:
1438         xfs_bmap_cancel(&free_list);
1439  error0:
1440         /*
1441          * Have to come here with the inode locked and either
1442          * (held and in the transaction) or (not in the transaction).
1443          * If the inode isn't held then cancel would iput it, but
1444          * that's wrong since this is inactive and the vnode ref
1445          * count is 0 already.
1446          * Cancel won't do anything to the inode if held, but it still
1447          * needs to be locked until the cancel is done, if it was
1448          * joined to the transaction.
1449          */
1450         xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1451         xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1452         *tpp = NULL;
1453         return error;
1454
1455 }
1456
1457 STATIC int
1458 xfs_inactive_symlink_local(
1459         xfs_inode_t     *ip,
1460         xfs_trans_t     **tpp)
1461 {
1462         int             error;
1463
1464         ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1465         /*
1466          * We're freeing a symlink which fit into
1467          * the inode.  Just free the memory used
1468          * to hold the old symlink.
1469          */
1470         error = xfs_trans_reserve(*tpp, 0,
1471                                   XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1472                                   0, XFS_TRANS_PERM_LOG_RES,
1473                                   XFS_ITRUNCATE_LOG_COUNT);
1474
1475         if (error) {
1476                 xfs_trans_cancel(*tpp, 0);
1477                 *tpp = NULL;
1478                 return error;
1479         }
1480         xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1481
1482         /*
1483          * Zero length symlinks _can_ exist.
1484          */
1485         if (ip->i_df.if_bytes > 0) {
1486                 xfs_idata_realloc(ip,
1487                                   -(ip->i_df.if_bytes),
1488                                   XFS_DATA_FORK);
1489                 ASSERT(ip->i_df.if_bytes == 0);
1490         }
1491         return 0;
1492 }
1493
1494 STATIC int
1495 xfs_inactive_attrs(
1496         xfs_inode_t     *ip,
1497         xfs_trans_t     **tpp)
1498 {
1499         xfs_trans_t     *tp;
1500         int             error;
1501         xfs_mount_t     *mp;
1502
1503         ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1504         tp = *tpp;
1505         mp = ip->i_mount;
1506         ASSERT(ip->i_d.di_forkoff != 0);
1507         xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1508         xfs_iunlock(ip, XFS_ILOCK_EXCL);
1509
1510         error = xfs_attr_inactive(ip);
1511         if (error) {
1512                 *tpp = NULL;
1513                 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1514                 return error; /* goto out */
1515         }
1516
1517         tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1518         error = xfs_trans_reserve(tp, 0,
1519                                   XFS_IFREE_LOG_RES(mp),
1520                                   0, XFS_TRANS_PERM_LOG_RES,
1521                                   XFS_INACTIVE_LOG_COUNT);
1522         if (error) {
1523                 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1524                 xfs_trans_cancel(tp, 0);
1525                 *tpp = NULL;
1526                 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1527                 return error;
1528         }
1529
1530         xfs_ilock(ip, XFS_ILOCK_EXCL);
1531         xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1532         xfs_trans_ihold(tp, ip);
1533         xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1534
1535         ASSERT(ip->i_d.di_anextents == 0);
1536
1537         *tpp = tp;
1538         return 0;
1539 }
1540
1541 STATIC int
1542 xfs_release(
1543         bhv_desc_t      *bdp)
1544 {
1545         xfs_inode_t     *ip;
1546         bhv_vnode_t     *vp;
1547         xfs_mount_t     *mp;
1548         int             error;
1549
1550         vp = BHV_TO_VNODE(bdp);
1551         ip = XFS_BHVTOI(bdp);
1552         mp = ip->i_mount;
1553
1554         if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1555                 return 0;
1556
1557         /* If this is a read-only mount, don't do this (would generate I/O) */
1558         if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1559                 return 0;
1560
1561 #ifdef HAVE_REFCACHE
1562         /* If we are in the NFS reference cache then don't do this now */
1563         if (ip->i_refcache)
1564                 return 0;
1565 #endif
1566
1567         if (ip->i_d.di_nlink != 0) {
1568                 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1569                      ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0 ||
1570                        ip->i_delayed_blks > 0)) &&
1571                      (ip->i_df.if_flags & XFS_IFEXTENTS))  &&
1572                     (!(ip->i_d.di_flags &
1573                                 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1574                         if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1575                                 return error;
1576                         /* Update linux inode block count after free above */
1577                         vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1578                                 ip->i_d.di_nblocks + ip->i_delayed_blks);
1579                 }
1580         }
1581
1582         return 0;
1583 }
1584
1585 /*
1586  * xfs_inactive
1587  *
1588  * This is called when the vnode reference count for the vnode
1589  * goes to zero.  If the file has been unlinked, then it must
1590  * now be truncated.  Also, we clear all of the read-ahead state
1591  * kept for the inode here since the file is now closed.
1592  */
1593 STATIC int
1594 xfs_inactive(
1595         bhv_desc_t      *bdp,
1596         cred_t          *credp)
1597 {
1598         xfs_inode_t     *ip;
1599         bhv_vnode_t     *vp;
1600         xfs_bmap_free_t free_list;
1601         xfs_fsblock_t   first_block;
1602         int             committed;
1603         xfs_trans_t     *tp;
1604         xfs_mount_t     *mp;
1605         int             error;
1606         int             truncate;
1607
1608         vp = BHV_TO_VNODE(bdp);
1609         vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1610
1611         ip = XFS_BHVTOI(bdp);
1612
1613         /*
1614          * If the inode is already free, then there can be nothing
1615          * to clean up here.
1616          */
1617         if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1618                 ASSERT(ip->i_df.if_real_bytes == 0);
1619                 ASSERT(ip->i_df.if_broot_bytes == 0);
1620                 return VN_INACTIVE_CACHE;
1621         }
1622
1623         /*
1624          * Only do a truncate if it's a regular file with
1625          * some actual space in it.  It's OK to look at the
1626          * inode's fields without the lock because we're the
1627          * only one with a reference to the inode.
1628          */
1629         truncate = ((ip->i_d.di_nlink == 0) &&
1630             ((ip->i_d.di_size != 0) || (ip->i_d.di_nextents > 0) ||
1631              (ip->i_delayed_blks > 0)) &&
1632             ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1633
1634         mp = ip->i_mount;
1635
1636         if (ip->i_d.di_nlink == 0 &&
1637             DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1638                 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1639         }
1640
1641         error = 0;
1642
1643         /* If this is a read-only mount, don't do this (would generate I/O) */
1644         if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1645                 goto out;
1646
1647         if (ip->i_d.di_nlink != 0) {
1648                 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1649                      ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0 ||
1650                        ip->i_delayed_blks > 0)) &&
1651                       (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1652                      (!(ip->i_d.di_flags &
1653                                 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1654                       (ip->i_delayed_blks != 0)))) {
1655                         if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1656                                 return VN_INACTIVE_CACHE;
1657                         /* Update linux inode block count after free above */
1658                         vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1659                                 ip->i_d.di_nblocks + ip->i_delayed_blks);
1660                 }
1661                 goto out;
1662         }
1663
1664         ASSERT(ip->i_d.di_nlink == 0);
1665
1666         if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1667                 return VN_INACTIVE_CACHE;
1668
1669         tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1670         if (truncate) {
1671                 /*
1672                  * Do the xfs_itruncate_start() call before
1673                  * reserving any log space because itruncate_start
1674                  * will call into the buffer cache and we can't
1675                  * do that within a transaction.
1676                  */
1677                 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1678
1679                 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1680
1681                 error = xfs_trans_reserve(tp, 0,
1682                                           XFS_ITRUNCATE_LOG_RES(mp),
1683                                           0, XFS_TRANS_PERM_LOG_RES,
1684                                           XFS_ITRUNCATE_LOG_COUNT);
1685                 if (error) {
1686                         /* Don't call itruncate_cleanup */
1687                         ASSERT(XFS_FORCED_SHUTDOWN(mp));
1688                         xfs_trans_cancel(tp, 0);
1689                         xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1690                         return VN_INACTIVE_CACHE;
1691                 }
1692
1693                 xfs_ilock(ip, XFS_ILOCK_EXCL);
1694                 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1695                 xfs_trans_ihold(tp, ip);
1696
1697                 /*
1698                  * normally, we have to run xfs_itruncate_finish sync.
1699                  * But if filesystem is wsync and we're in the inactive
1700                  * path, then we know that nlink == 0, and that the
1701                  * xaction that made nlink == 0 is permanently committed
1702                  * since xfs_remove runs as a synchronous transaction.
1703                  */
1704                 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1705                                 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1706
1707                 if (error) {
1708                         xfs_trans_cancel(tp,
1709                                 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1710                         xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1711                         return VN_INACTIVE_CACHE;
1712                 }
1713         } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1714
1715                 /*
1716                  * If we get an error while cleaning up a
1717                  * symlink we bail out.
1718                  */
1719                 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1720                         xfs_inactive_symlink_rmt(ip, &tp) :
1721                         xfs_inactive_symlink_local(ip, &tp);
1722
1723                 if (error) {
1724                         ASSERT(tp == NULL);
1725                         return VN_INACTIVE_CACHE;
1726                 }
1727
1728                 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1729                 xfs_trans_ihold(tp, ip);
1730         } else {
1731                 error = xfs_trans_reserve(tp, 0,
1732                                           XFS_IFREE_LOG_RES(mp),
1733                                           0, XFS_TRANS_PERM_LOG_RES,
1734                                           XFS_INACTIVE_LOG_COUNT);
1735                 if (error) {
1736                         ASSERT(XFS_FORCED_SHUTDOWN(mp));
1737                         xfs_trans_cancel(tp, 0);
1738                         return VN_INACTIVE_CACHE;
1739                 }
1740
1741                 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1742                 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1743                 xfs_trans_ihold(tp, ip);
1744         }
1745
1746         /*
1747          * If there are attributes associated with the file
1748          * then blow them away now.  The code calls a routine
1749          * that recursively deconstructs the attribute fork.
1750          * We need to just commit the current transaction
1751          * because we can't use it for xfs_attr_inactive().
1752          */
1753         if (ip->i_d.di_anextents > 0) {
1754                 error = xfs_inactive_attrs(ip, &tp);
1755                 /*
1756                  * If we got an error, the transaction is already
1757                  * cancelled, and the inode is unlocked. Just get out.
1758                  */
1759                  if (error)
1760                          return VN_INACTIVE_CACHE;
1761         } else if (ip->i_afp) {
1762                 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1763         }
1764
1765         /*
1766          * Free the inode.
1767          */
1768         XFS_BMAP_INIT(&free_list, &first_block);
1769         error = xfs_ifree(tp, ip, &free_list);
1770         if (error) {
1771                 /*
1772                  * If we fail to free the inode, shut down.  The cancel
1773                  * might do that, we need to make sure.  Otherwise the
1774                  * inode might be lost for a long time or forever.
1775                  */
1776                 if (!XFS_FORCED_SHUTDOWN(mp)) {
1777                         cmn_err(CE_NOTE,
1778                 "xfs_inactive:  xfs_ifree() returned an error = %d on %s",
1779                                 error, mp->m_fsname);
1780                         xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1781                 }
1782                 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1783         } else {
1784                 /*
1785                  * Credit the quota account(s). The inode is gone.
1786                  */
1787                 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1788
1789                 /*
1790                  * Just ignore errors at this point.  There is
1791                  * nothing we can do except to try to keep going.
1792                  */
1793                 (void) xfs_bmap_finish(&tp,  &free_list, first_block,
1794                                        &committed);
1795                 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1796         }
1797         /*
1798          * Release the dquots held by inode, if any.
1799          */
1800         XFS_QM_DQDETACH(mp, ip);
1801
1802         xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1803
1804  out:
1805         return VN_INACTIVE_CACHE;
1806 }
1807
1808
1809 /*
1810  * xfs_lookup
1811  */
1812 STATIC int
1813 xfs_lookup(
1814         bhv_desc_t              *dir_bdp,
1815         bhv_vname_t             *dentry,
1816         bhv_vnode_t             **vpp,
1817         int                     flags,
1818         bhv_vnode_t             *rdir,
1819         cred_t                  *credp)
1820 {
1821         xfs_inode_t             *dp, *ip;
1822         xfs_ino_t               e_inum;
1823         int                     error;
1824         uint                    lock_mode;
1825         bhv_vnode_t             *dir_vp;
1826
1827         dir_vp = BHV_TO_VNODE(dir_bdp);
1828         vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1829
1830         dp = XFS_BHVTOI(dir_bdp);
1831
1832         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1833                 return XFS_ERROR(EIO);
1834
1835         lock_mode = xfs_ilock_map_shared(dp);
1836         error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1837         if (!error) {
1838                 *vpp = XFS_ITOV(ip);
1839                 ITRACE(ip);
1840         }
1841         xfs_iunlock_map_shared(dp, lock_mode);
1842         return error;
1843 }
1844
1845
1846 /*
1847  * xfs_create (create a new file).
1848  */
1849 STATIC int
1850 xfs_create(
1851         bhv_desc_t              *dir_bdp,
1852         bhv_vname_t             *dentry,
1853         bhv_vattr_t             *vap,
1854         bhv_vnode_t             **vpp,
1855         cred_t                  *credp)
1856 {
1857         char                    *name = VNAME(dentry);
1858         bhv_vnode_t             *dir_vp;
1859         xfs_inode_t             *dp, *ip;
1860         bhv_vnode_t             *vp = NULL;
1861         xfs_trans_t             *tp;
1862         xfs_mount_t             *mp;
1863         xfs_dev_t               rdev;
1864         int                     error;
1865         xfs_bmap_free_t         free_list;
1866         xfs_fsblock_t           first_block;
1867         boolean_t               dp_joined_to_trans;
1868         int                     dm_event_sent = 0;
1869         uint                    cancel_flags;
1870         int                     committed;
1871         xfs_prid_t              prid;
1872         struct xfs_dquot        *udqp, *gdqp;
1873         uint                    resblks;
1874         int                     dm_di_mode;
1875         int                     namelen;
1876
1877         ASSERT(!*vpp);
1878         dir_vp = BHV_TO_VNODE(dir_bdp);
1879         vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1880
1881         dp = XFS_BHVTOI(dir_bdp);
1882         mp = dp->i_mount;
1883
1884         dm_di_mode = vap->va_mode;
1885         namelen = VNAMELEN(dentry);
1886
1887         if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1888                 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1889                                 dir_vp, DM_RIGHT_NULL, NULL,
1890                                 DM_RIGHT_NULL, name, NULL,
1891                                 dm_di_mode, 0, 0);
1892
1893                 if (error)
1894                         return error;
1895                 dm_event_sent = 1;
1896         }
1897
1898         if (XFS_FORCED_SHUTDOWN(mp))
1899                 return XFS_ERROR(EIO);
1900
1901         /* Return through std_return after this point. */
1902
1903         udqp = gdqp = NULL;
1904         if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1905                 prid = dp->i_d.di_projid;
1906         else if (vap->va_mask & XFS_AT_PROJID)
1907                 prid = (xfs_prid_t)vap->va_projid;
1908         else
1909                 prid = (xfs_prid_t)dfltprid;
1910
1911         /*
1912          * Make sure that we have allocated dquot(s) on disk.
1913          */
1914         error = XFS_QM_DQVOPALLOC(mp, dp,
1915                         current_fsuid(credp), current_fsgid(credp), prid,
1916                         XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1917         if (error)
1918                 goto std_return;
1919
1920         ip = NULL;
1921         dp_joined_to_trans = B_FALSE;
1922
1923         tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1924         cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1925         resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1926         /*
1927          * Initially assume that the file does not exist and
1928          * reserve the resources for that case.  If that is not
1929          * the case we'll drop the one we have and get a more
1930          * appropriate transaction later.
1931          */
1932         error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1933                         XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1934         if (error == ENOSPC) {
1935                 resblks = 0;
1936                 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1937                                 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1938         }
1939         if (error) {
1940                 cancel_flags = 0;
1941                 dp = NULL;
1942                 goto error_return;
1943         }
1944
1945         xfs_ilock(dp, XFS_ILOCK_EXCL);
1946
1947         XFS_BMAP_INIT(&free_list, &first_block);
1948
1949         ASSERT(ip == NULL);
1950
1951         /*
1952          * Reserve disk quota and the inode.
1953          */
1954         error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1955         if (error)
1956                 goto error_return;
1957
1958         if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1959                 goto error_return;
1960         rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1961         error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1962                         rdev, credp, prid, resblks > 0,
1963                         &ip, &committed);
1964         if (error) {
1965                 if (error == ENOSPC)
1966                         goto error_return;
1967                 goto abort_return;
1968         }
1969         ITRACE(ip);
1970
1971         /*
1972          * At this point, we've gotten a newly allocated inode.
1973          * It is locked (and joined to the transaction).
1974          */
1975
1976         ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1977
1978         /*
1979          * Now we join the directory inode to the transaction.
1980          * We do not do it earlier because xfs_dir_ialloc
1981          * might commit the previous transaction (and release
1982          * all the locks).
1983          */
1984
1985         VN_HOLD(dir_vp);
1986         xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1987         dp_joined_to_trans = B_TRUE;
1988
1989         error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1990                                         &first_block, &free_list, resblks ?
1991                                         resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1992         if (error) {
1993                 ASSERT(error != ENOSPC);
1994                 goto abort_return;
1995         }
1996         xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1997         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1998
1999         /*
2000          * If this is a synchronous mount, make sure that the
2001          * create transaction goes to disk before returning to
2002          * the user.
2003          */
2004         if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2005                 xfs_trans_set_sync(tp);
2006         }
2007
2008         dp->i_gen++;
2009
2010         /*
2011          * Attach the dquot(s) to the inodes and modify them incore.
2012          * These ids of the inode couldn't have changed since the new
2013          * inode has been locked ever since it was created.
2014          */
2015         XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2016
2017         /*
2018          * xfs_trans_commit normally decrements the vnode ref count
2019          * when it unlocks the inode. Since we want to return the
2020          * vnode to the caller, we bump the vnode ref count now.
2021          */
2022         IHOLD(ip);
2023         vp = XFS_ITOV(ip);
2024
2025         error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2026         if (error) {
2027                 xfs_bmap_cancel(&free_list);
2028                 goto abort_rele;
2029         }
2030
2031         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2032         if (error) {
2033                 IRELE(ip);
2034                 tp = NULL;
2035                 goto error_return;
2036         }
2037
2038         XFS_QM_DQRELE(mp, udqp);
2039         XFS_QM_DQRELE(mp, gdqp);
2040
2041         /*
2042          * Propagate the fact that the vnode changed after the
2043          * xfs_inode locks have been released.
2044          */
2045         bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2046
2047         *vpp = vp;
2048
2049         /* Fallthrough to std_return with error = 0  */
2050
2051 std_return:
2052         if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2053                         DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2054                                                         DM_EVENT_POSTCREATE)) {
2055                 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2056                         dir_vp, DM_RIGHT_NULL,
2057                         *vpp ? vp:NULL,
2058                         DM_RIGHT_NULL, name, NULL,
2059                         dm_di_mode, error, 0);
2060         }
2061         return error;
2062
2063  abort_return:
2064         cancel_flags |= XFS_TRANS_ABORT;
2065         /* FALLTHROUGH */
2066
2067  error_return:
2068         if (tp != NULL)
2069                 xfs_trans_cancel(tp, cancel_flags);
2070
2071         if (!dp_joined_to_trans && (dp != NULL))
2072                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2073         XFS_QM_DQRELE(mp, udqp);
2074         XFS_QM_DQRELE(mp, gdqp);
2075
2076         goto std_return;
2077
2078  abort_rele:
2079         /*
2080          * Wait until after the current transaction is aborted to
2081          * release the inode.  This prevents recursive transactions
2082          * and deadlocks from xfs_inactive.
2083          */
2084         cancel_flags |= XFS_TRANS_ABORT;
2085         xfs_trans_cancel(tp, cancel_flags);
2086         IRELE(ip);
2087
2088         XFS_QM_DQRELE(mp, udqp);
2089         XFS_QM_DQRELE(mp, gdqp);
2090
2091         goto std_return;
2092 }
2093
2094 #ifdef DEBUG
2095 /*
2096  * Some counters to see if (and how often) we are hitting some deadlock
2097  * prevention code paths.
2098  */
2099
2100 int xfs_rm_locks;
2101 int xfs_rm_lock_delays;
2102 int xfs_rm_attempts;
2103 #endif
2104
2105 /*
2106  * The following routine will lock the inodes associated with the
2107  * directory and the named entry in the directory. The locks are
2108  * acquired in increasing inode number.
2109  *
2110  * If the entry is "..", then only the directory is locked. The
2111  * vnode ref count will still include that from the .. entry in
2112  * this case.
2113  *
2114  * There is a deadlock we need to worry about. If the locked directory is
2115  * in the AIL, it might be blocking up the log. The next inode we lock
2116  * could be already locked by another thread waiting for log space (e.g
2117  * a permanent log reservation with a long running transaction (see
2118  * xfs_itruncate_finish)). To solve this, we must check if the directory
2119  * is in the ail and use lock_nowait. If we can't lock, we need to
2120  * drop the inode lock on the directory and try again. xfs_iunlock will
2121  * potentially push the tail if we were holding up the log.
2122  */
2123 STATIC int
2124 xfs_lock_dir_and_entry(
2125         xfs_inode_t     *dp,
2126         bhv_vname_t     *dentry,
2127         xfs_inode_t     *ip)    /* inode of entry 'name' */
2128 {
2129         int             attempts;
2130         xfs_ino_t       e_inum;
2131         xfs_inode_t     *ips[2];
2132         xfs_log_item_t  *lp;
2133
2134 #ifdef DEBUG
2135         xfs_rm_locks++;
2136 #endif
2137         attempts = 0;
2138
2139 again:
2140         xfs_ilock(dp, XFS_ILOCK_EXCL);
2141
2142         e_inum = ip->i_ino;
2143
2144         ITRACE(ip);
2145
2146         /*
2147          * We want to lock in increasing inum. Since we've already
2148          * acquired the lock on the directory, we may need to release
2149          * if if the inum of the entry turns out to be less.
2150          */
2151         if (e_inum > dp->i_ino) {
2152                 /*
2153                  * We are already in the right order, so just
2154                  * lock on the inode of the entry.
2155                  * We need to use nowait if dp is in the AIL.
2156                  */
2157
2158                 lp = (xfs_log_item_t *)dp->i_itemp;
2159                 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2160                         if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2161                                 attempts++;
2162 #ifdef DEBUG
2163                                 xfs_rm_attempts++;
2164 #endif
2165
2166                                 /*
2167                                  * Unlock dp and try again.
2168                                  * xfs_iunlock will try to push the tail
2169                                  * if the inode is in the AIL.
2170                                  */
2171
2172                                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2173
2174                                 if ((attempts % 5) == 0) {
2175                                         delay(1); /* Don't just spin the CPU */
2176 #ifdef DEBUG
2177                                         xfs_rm_lock_delays++;
2178 #endif
2179                                 }
2180                                 goto again;
2181                         }
2182                 } else {
2183                         xfs_ilock(ip, XFS_ILOCK_EXCL);
2184                 }
2185         } else if (e_inum < dp->i_ino) {
2186                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2187
2188                 ips[0] = ip;
2189                 ips[1] = dp;
2190                 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2191         }
2192         /* else  e_inum == dp->i_ino */
2193         /*     This can happen if we're asked to lock /x/..
2194          *     the entry is "..", which is also the parent directory.
2195          */
2196
2197         return 0;
2198 }
2199
2200 #ifdef DEBUG
2201 int xfs_locked_n;
2202 int xfs_small_retries;
2203 int xfs_middle_retries;
2204 int xfs_lots_retries;
2205 int xfs_lock_delays;
2206 #endif
2207
2208 /*
2209  * The following routine will lock n inodes in exclusive mode.
2210  * We assume the caller calls us with the inodes in i_ino order.
2211  *
2212  * We need to detect deadlock where an inode that we lock
2213  * is in the AIL and we start waiting for another inode that is locked
2214  * by a thread in a long running transaction (such as truncate). This can
2215  * result in deadlock since the long running trans might need to wait
2216  * for the inode we just locked in order to push the tail and free space
2217  * in the log.
2218  */
2219 void
2220 xfs_lock_inodes(
2221         xfs_inode_t     **ips,
2222         int             inodes,
2223         int             first_locked,
2224         uint            lock_mode)
2225 {
2226         int             attempts = 0, i, j, try_lock;
2227         xfs_log_item_t  *lp;
2228
2229         ASSERT(ips && (inodes >= 2)); /* we need at least two */
2230
2231         if (first_locked) {
2232                 try_lock = 1;
2233                 i = 1;
2234         } else {
2235                 try_lock = 0;
2236                 i = 0;
2237         }
2238
2239 again:
2240         for (; i < inodes; i++) {
2241                 ASSERT(ips[i]);
2242
2243                 if (i && (ips[i] == ips[i-1]))  /* Already locked */
2244                         continue;
2245
2246                 /*
2247                  * If try_lock is not set yet, make sure all locked inodes
2248                  * are not in the AIL.
2249                  * If any are, set try_lock to be used later.
2250                  */
2251
2252                 if (!try_lock) {
2253                         for (j = (i - 1); j >= 0 && !try_lock; j--) {
2254                                 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2255                                 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2256                                         try_lock++;
2257                                 }
2258                         }
2259                 }
2260
2261                 /*
2262                  * If any of the previous locks we have locked is in the AIL,
2263                  * we must TRY to get the second and subsequent locks. If
2264                  * we can't get any, we must release all we have
2265                  * and try again.
2266                  */
2267
2268                 if (try_lock) {
2269                         /* try_lock must be 0 if i is 0. */
2270                         /*
2271                          * try_lock means we have an inode locked
2272                          * that is in the AIL.
2273                          */
2274                         ASSERT(i != 0);
2275                         if (!xfs_ilock_nowait(ips[i], lock_mode)) {
2276                                 attempts++;
2277
2278                                 /*
2279                                  * Unlock all previous guys and try again.
2280                                  * xfs_iunlock will try to push the tail
2281                                  * if the inode is in the AIL.
2282                                  */
2283
2284                                 for(j = i - 1; j >= 0; j--) {
2285
2286                                         /*
2287                                          * Check to see if we've already
2288                                          * unlocked this one.
2289                                          * Not the first one going back,
2290                                          * and the inode ptr is the same.
2291                                          */
2292                                         if ((j != (i - 1)) && ips[j] ==
2293                                                                 ips[j+1])
2294                                                 continue;
2295
2296                                         xfs_iunlock(ips[j], lock_mode);
2297                                 }
2298
2299                                 if ((attempts % 5) == 0) {
2300                                         delay(1); /* Don't just spin the CPU */
2301 #ifdef DEBUG
2302                                         xfs_lock_delays++;
2303 #endif
2304                                 }
2305                                 i = 0;
2306                                 try_lock = 0;
2307                                 goto again;
2308                         }
2309                 } else {
2310                         xfs_ilock(ips[i], lock_mode);
2311                 }
2312         }
2313
2314 #ifdef DEBUG
2315         if (attempts) {
2316                 if (attempts < 5) xfs_small_retries++;
2317                 else if (attempts < 100) xfs_middle_retries++;
2318                 else xfs_lots_retries++;
2319         } else {
2320                 xfs_locked_n++;
2321         }
2322 #endif
2323 }
2324
2325 #ifdef  DEBUG
2326 #define REMOVE_DEBUG_TRACE(x)   {remove_which_error_return = (x);}
2327 int remove_which_error_return = 0;
2328 #else /* ! DEBUG */
2329 #define REMOVE_DEBUG_TRACE(x)
2330 #endif  /* ! DEBUG */
2331
2332
2333 /*
2334  * xfs_remove
2335  *
2336  */
2337 STATIC int
2338 xfs_remove(
2339         bhv_desc_t              *dir_bdp,
2340         bhv_vname_t             *dentry,
2341         cred_t                  *credp)
2342 {
2343         bhv_vnode_t             *dir_vp;
2344         char                    *name = VNAME(dentry);
2345         xfs_inode_t             *dp, *ip;
2346         xfs_trans_t             *tp = NULL;
2347         xfs_mount_t             *mp;
2348         int                     error = 0;
2349         xfs_bmap_free_t         free_list;
2350         xfs_fsblock_t           first_block;
2351         int                     cancel_flags;
2352         int                     committed;
2353         int                     dm_di_mode = 0;
2354         int                     link_zero;
2355         uint                    resblks;
2356         int                     namelen;
2357
2358         dir_vp = BHV_TO_VNODE(dir_bdp);
2359         vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2360
2361         dp = XFS_BHVTOI(dir_bdp);
2362         mp = dp->i_mount;
2363
2364         if (XFS_FORCED_SHUTDOWN(mp))
2365                 return XFS_ERROR(EIO);
2366
2367         namelen = VNAMELEN(dentry);
2368
2369         if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2370                 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2371                                         DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2372                                         name, NULL, 0, 0, 0);
2373                 if (error)
2374                         return error;
2375         }
2376
2377         /* From this point on, return through std_return */
2378         ip = NULL;
2379
2380         /*
2381          * We need to get a reference to ip before we get our log
2382          * reservation. The reason for this is that we cannot call
2383          * xfs_iget for an inode for which we do not have a reference
2384          * once we've acquired a log reservation. This is because the
2385          * inode we are trying to get might be in xfs_inactive going
2386          * for a log reservation. Since we'll have to wait for the
2387          * inactive code to complete before returning from xfs_iget,
2388          * we need to make sure that we don't have log space reserved
2389          * when we call xfs_iget.  Instead we get an unlocked reference
2390          * to the inode before getting our log reservation.
2391          */
2392         error = xfs_get_dir_entry(dentry, &ip);
2393         if (error) {
2394                 REMOVE_DEBUG_TRACE(__LINE__);
2395                 goto std_return;
2396         }
2397
2398         dm_di_mode = ip->i_d.di_mode;
2399
2400         vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2401
2402         ITRACE(ip);
2403
2404         error = XFS_QM_DQATTACH(mp, dp, 0);
2405         if (!error && dp != ip)
2406                 error = XFS_QM_DQATTACH(mp, ip, 0);
2407         if (error) {
2408                 REMOVE_DEBUG_TRACE(__LINE__);
2409                 IRELE(ip);
2410                 goto std_return;
2411         }
2412
2413         tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2414         cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2415         /*
2416          * We try to get the real space reservation first,
2417          * allowing for directory btree deletion(s) implying
2418          * possible bmap insert(s).  If we can't get the space
2419          * reservation then we use 0 instead, and avoid the bmap
2420          * btree insert(s) in the directory code by, if the bmap
2421          * insert tries to happen, instead trimming the LAST
2422          * block from the directory.
2423          */
2424         resblks = XFS_REMOVE_SPACE_RES(mp);
2425         error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2426                         XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2427         if (error == ENOSPC) {
2428                 resblks = 0;
2429                 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2430                                 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2431         }
2432         if (error) {
2433                 ASSERT(error != ENOSPC);
2434                 REMOVE_DEBUG_TRACE(__LINE__);
2435                 xfs_trans_cancel(tp, 0);
2436                 IRELE(ip);
2437                 return error;
2438         }
2439
2440         error = xfs_lock_dir_and_entry(dp, dentry, ip);
2441         if (error) {
2442                 REMOVE_DEBUG_TRACE(__LINE__);
2443                 xfs_trans_cancel(tp, cancel_flags);
2444                 IRELE(ip);
2445                 goto std_return;
2446         }
2447
2448         /*
2449          * At this point, we've gotten both the directory and the entry
2450          * inodes locked.
2451          */
2452         xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2453         if (dp != ip) {
2454                 /*
2455                  * Increment vnode ref count only in this case since
2456                  * there's an extra vnode reference in the case where
2457                  * dp == ip.
2458                  */
2459                 IHOLD(dp);
2460                 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2461         }
2462
2463         /*
2464          * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2465          */
2466         XFS_BMAP_INIT(&free_list, &first_block);
2467         error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2468                                         &first_block, &free_list, 0);
2469         if (error) {
2470                 ASSERT(error != ENOENT);
2471                 REMOVE_DEBUG_TRACE(__LINE__);
2472                 goto error1;
2473         }
2474         xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2475
2476         dp->i_gen++;
2477         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2478
2479         error = xfs_droplink(tp, ip);
2480         if (error) {
2481                 REMOVE_DEBUG_TRACE(__LINE__);
2482                 goto error1;
2483         }
2484
2485         /* Determine if this is the last link while
2486          * we are in the transaction.
2487          */
2488         link_zero = (ip)->i_d.di_nlink==0;
2489
2490         /*
2491          * Take an extra ref on the inode so that it doesn't
2492          * go to xfs_inactive() from within the commit.
2493          */
2494         IHOLD(ip);
2495
2496         /*
2497          * If this is a synchronous mount, make sure that the
2498          * remove transaction goes to disk before returning to
2499          * the user.
2500          */
2501         if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2502                 xfs_trans_set_sync(tp);
2503         }
2504
2505         error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2506         if (error) {
2507                 REMOVE_DEBUG_TRACE(__LINE__);
2508                 goto error_rele;
2509         }
2510
2511         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2512         if (error) {
2513                 IRELE(ip);
2514                 goto std_return;
2515         }
2516
2517         /*
2518          * Before we drop our extra reference to the inode, purge it
2519          * from the refcache if it is there.  By waiting until afterwards
2520          * to do the IRELE, we ensure that we won't go inactive in the
2521          * xfs_refcache_purge_ip routine (although that would be OK).
2522          */
2523         xfs_refcache_purge_ip(ip);
2524
2525         vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2526
2527         /*
2528          * Let interposed file systems know about removed links.
2529          */
2530         bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
2531
2532         IRELE(ip);
2533
2534 /*      Fall through to std_return with error = 0 */
2535  std_return:
2536         if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2537                                                 DM_EVENT_POSTREMOVE)) {
2538                 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2539                                 dir_vp, DM_RIGHT_NULL,
2540                                 NULL, DM_RIGHT_NULL,
2541                                 name, NULL, dm_di_mode, error, 0);
2542         }
2543         return error;
2544
2545  error1:
2546         xfs_bmap_cancel(&free_list);
2547         cancel_flags |= XFS_TRANS_ABORT;
2548         xfs_trans_cancel(tp, cancel_flags);
2549         goto std_return;
2550
2551  error_rele:
2552         /*
2553          * In this case make sure to not release the inode until after
2554          * the current transaction is aborted.  Releasing it beforehand
2555          * can cause us to go to xfs_inactive and start a recursive
2556          * transaction which can easily deadlock with the current one.
2557          */
2558         xfs_bmap_cancel(&free_list);
2559         cancel_flags |= XFS_TRANS_ABORT;
2560         xfs_trans_cancel(tp, cancel_flags);
2561
2562         /*
2563          * Before we drop our extra reference to the inode, purge it
2564          * from the refcache if it is there.  By waiting until afterwards
2565          * to do the IRELE, we ensure that we won't go inactive in the
2566          * xfs_refcache_purge_ip routine (although that would be OK).
2567          */
2568         xfs_refcache_purge_ip(ip);
2569
2570         IRELE(ip);
2571
2572         goto std_return;
2573 }
2574
2575
2576 /*
2577  * xfs_link
2578  *
2579  */
2580 STATIC int
2581 xfs_link(
2582         bhv_desc_t              *target_dir_bdp,
2583         bhv_vnode_t             *src_vp,
2584         bhv_vname_t             *dentry,
2585         cred_t                  *credp)
2586 {
2587         xfs_inode_t             *tdp, *sip;
2588         xfs_trans_t             *tp;
2589         xfs_mount_t             *mp;
2590         xfs_inode_t             *ips[2];
2591         int                     error;
2592         xfs_bmap_free_t         free_list;
2593         xfs_fsblock_t           first_block;
2594         int                     cancel_flags;
2595         int                     committed;
2596         bhv_vnode_t             *target_dir_vp;
2597         int                     resblks;
2598         char                    *target_name = VNAME(dentry);
2599         int                     target_namelen;
2600
2601         target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2602         vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2603         vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2604
2605         target_namelen = VNAMELEN(dentry);
2606         if (VN_ISDIR(src_vp))
2607                 return XFS_ERROR(EPERM);
2608
2609         sip = xfs_vtoi(src_vp);
2610         tdp = XFS_BHVTOI(target_dir_bdp);
2611         mp = tdp->i_mount;
2612         if (XFS_FORCED_SHUTDOWN(mp))
2613                 return XFS_ERROR(EIO);
2614
2615         if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2616                 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2617                                         target_dir_vp, DM_RIGHT_NULL,
2618                                         src_vp, DM_RIGHT_NULL,
2619                                         target_name, NULL, 0, 0, 0);
2620                 if (error)
2621                         return error;
2622         }
2623
2624         /* Return through std_return after this point. */
2625
2626         error = XFS_QM_DQATTACH(mp, sip, 0);
2627         if (!error && sip != tdp)
2628                 error = XFS_QM_DQATTACH(mp, tdp, 0);
2629         if (error)
2630                 goto std_return;
2631
2632         tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2633         cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2634         resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2635         error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2636                         XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2637         if (error == ENOSPC) {
2638                 resblks = 0;
2639                 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2640                                 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2641         }
2642         if (error) {
2643                 cancel_flags = 0;
2644                 goto error_return;
2645         }
2646
2647         if (sip->i_ino < tdp->i_ino) {
2648                 ips[0] = sip;
2649                 ips[1] = tdp;
2650         } else {
2651                 ips[0] = tdp;
2652                 ips[1] = sip;
2653         }
2654
2655         xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2656
2657         /*
2658          * Increment vnode ref counts since xfs_trans_commit &
2659          * xfs_trans_cancel will both unlock the inodes and
2660          * decrement the associated ref counts.
2661          */
2662         VN_HOLD(src_vp);
2663         VN_HOLD(target_dir_vp);
2664         xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2665         xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2666
2667         /*
2668          * If the source has too many links, we can't make any more to it.
2669          */
2670         if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2671                 error = XFS_ERROR(EMLINK);
2672                 goto error_return;
2673         }
2674
2675         /*
2676          * If we are using project inheritance, we only allow hard link
2677          * creation in our tree when the project IDs are the same; else
2678          * the tree quota mechanism could be circumvented.
2679          */
2680         if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2681                      (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2682                 error = XFS_ERROR(EXDEV);
2683                 goto error_return;
2684         }
2685
2686         if (resblks == 0 &&
2687             (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2688                 goto error_return;
2689
2690         XFS_BMAP_INIT(&free_list, &first_block);
2691
2692         error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2693                                    sip->i_ino, &first_block, &free_list,
2694                                    resblks);
2695         if (error)
2696                 goto abort_return;
2697         xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2698         tdp->i_gen++;
2699         xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2700
2701         error = xfs_bumplink(tp, sip);
2702         if (error) {
2703                 goto abort_return;
2704         }
2705
2706         /*
2707          * If this is a synchronous mount, make sure that the
2708          * link transaction goes to disk before returning to
2709          * the user.
2710          */
2711         if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2712                 xfs_trans_set_sync(tp);
2713         }
2714
2715         error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
2716         if (error) {
2717                 xfs_bmap_cancel(&free_list);
2718                 goto abort_return;
2719         }
2720
2721         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2722         if (error) {
2723                 goto std_return;
2724         }
2725
2726         /* Fall through to std_return with error = 0. */
2727 std_return:
2728         if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2729                                                 DM_EVENT_POSTLINK)) {
2730                 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2731                                 target_dir_vp, DM_RIGHT_NULL,
2732                                 src_vp, DM_RIGHT_NULL,
2733                                 target_name, NULL, 0, error, 0);
2734         }
2735         return error;
2736
2737  abort_return:
2738         cancel_flags |= XFS_TRANS_ABORT;
2739         /* FALLTHROUGH */
2740
2741  error_return:
2742         xfs_trans_cancel(tp, cancel_flags);
2743         goto std_return;
2744 }
2745 /*
2746  * xfs_mkdir
2747  *
2748  */
2749 STATIC int
2750 xfs_mkdir(
2751         bhv_desc_t              *dir_bdp,
2752         bhv_vname_t             *dentry,
2753         bhv_vattr_t             *vap,
2754         bhv_vnode_t             **vpp,
2755         cred_t                  *credp)
2756 {
2757         char                    *dir_name = VNAME(dentry);
2758         xfs_inode_t             *dp;
2759         xfs_inode_t             *cdp;   /* inode of created dir */
2760         bhv_vnode_t             *cvp;   /* vnode of created dir */
2761         xfs_trans_t             *tp;
2762         xfs_mount_t             *mp;
2763         int                     cancel_flags;
2764         int                     error;
2765         int                     committed;
2766         xfs_bmap_free_t         free_list;
2767         xfs_fsblock_t           first_block;
2768         bhv_vnode_t             *dir_vp;
2769         boolean_t               dp_joined_to_trans;
2770         boolean_t               created = B_FALSE;
2771         int                     dm_event_sent = 0;
2772         xfs_prid_t              prid;
2773         struct xfs_dquot        *udqp, *gdqp;
2774         uint                    resblks;
2775         int                     dm_di_mode;
2776         int                     dir_namelen;
2777
2778         dir_vp = BHV_TO_VNODE(dir_bdp);
2779         dp = XFS_BHVTOI(dir_bdp);
2780         mp = dp->i_mount;
2781
2782         if (XFS_FORCED_SHUTDOWN(mp))
2783                 return XFS_ERROR(EIO);
2784
2785         dir_namelen = VNAMELEN(dentry);
2786
2787         tp = NULL;
2788         dp_joined_to_trans = B_FALSE;
2789         dm_di_mode = vap->va_mode;
2790
2791         if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2792                 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2793                                         dir_vp, DM_RIGHT_NULL, NULL,
2794                                         DM_RIGHT_NULL, dir_name, NULL,
2795                                         dm_di_mode, 0, 0);
2796                 if (error)
2797                         return error;
2798                 dm_event_sent = 1;
2799         }
2800
2801         /* Return through std_return after this point. */
2802
2803         vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2804
2805         mp = dp->i_mount;
2806         udqp = gdqp = NULL;
2807         if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2808                 prid = dp->i_d.di_projid;
2809         else if (vap->va_mask & XFS_AT_PROJID)
2810                 prid = (xfs_prid_t)vap->va_projid;
2811         else
2812                 prid = (xfs_prid_t)dfltprid;
2813
2814         /*
2815          * Make sure that we have allocated dquot(s) on disk.
2816          */
2817         error = XFS_QM_DQVOPALLOC(mp, dp,
2818                         current_fsuid(credp), current_fsgid(credp), prid,
2819                         XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2820         if (error)
2821                 goto std_return;
2822
2823         tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2824         cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2825         resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2826         error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2827                                   XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2828         if (error == ENOSPC) {
2829                 resblks = 0;
2830                 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2831                                           XFS_TRANS_PERM_LOG_RES,
2832                                           XFS_MKDIR_LOG_COUNT);
2833         }
2834         if (error) {
2835                 cancel_flags = 0;
2836                 dp = NULL;
2837                 goto error_return;
2838         }
2839
2840         xfs_ilock(dp, XFS_ILOCK_EXCL);
2841
2842         /*
2843          * Check for directory link count overflow.
2844          */
2845         if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2846                 error = XFS_ERROR(EMLINK);
2847                 goto error_return;
2848         }
2849
2850         /*
2851          * Reserve disk quota and the inode.
2852          */
2853         error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2854         if (error)
2855                 goto error_return;
2856
2857         if (resblks == 0 &&
2858             (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2859                 goto error_return;
2860         /*
2861          * create the directory inode.
2862          */
2863         error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2864                         0, credp, prid, resblks > 0,
2865                 &cdp, NULL);
2866         if (error) {
2867                 if (error == ENOSPC)
2868                         goto error_return;
2869                 goto abort_return;
2870         }
2871         ITRACE(cdp);
2872
2873         /*
2874          * Now we add the directory inode to the transaction.
2875          * We waited until now since xfs_dir_ialloc might start
2876          * a new transaction.  Had we joined the transaction
2877          * earlier, the locks might have gotten released.
2878          */
2879         VN_HOLD(dir_vp);
2880         xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2881         dp_joined_to_trans = B_TRUE;
2882
2883         XFS_BMAP_INIT(&free_list, &first_block);
2884
2885         error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2886                                    &first_block, &free_list, resblks ?
2887                                    resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2888         if (error) {
2889                 ASSERT(error != ENOSPC);
2890                 goto error1;
2891         }
2892         xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2893
2894         /*
2895          * Bump the in memory version number of the parent directory
2896          * so that other processes accessing it will recognize that
2897          * the directory has changed.
2898          */
2899         dp->i_gen++;
2900
2901         error = xfs_dir_init(tp, cdp, dp);
2902         if (error)
2903                 goto error2;
2904
2905         cdp->i_gen = 1;
2906         error = xfs_bumplink(tp, dp);
2907         if (error)
2908                 goto error2;
2909
2910         cvp = XFS_ITOV(cdp);
2911
2912         created = B_TRUE;
2913
2914         *vpp = cvp;
2915         IHOLD(cdp);
2916
2917         /*
2918          * Attach the dquots to the new inode and modify the icount incore.
2919          */
2920         XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2921
2922         /*
2923          * If this is a synchronous mount, make sure that the
2924          * mkdir transaction goes to disk before returning to
2925          * the user.
2926          */
2927         if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2928                 xfs_trans_set_sync(tp);
2929         }
2930
2931         error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2932         if (error) {
2933                 IRELE(cdp);
2934                 goto error2;
2935         }
2936
2937         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2938         XFS_QM_DQRELE(mp, udqp);
2939         XFS_QM_DQRELE(mp, gdqp);
2940         if (error) {
2941                 IRELE(cdp);
2942         }
2943
2944         /* Fall through to std_return with error = 0 or errno from
2945          * xfs_trans_commit. */
2946
2947 std_return:
2948         if ( (created || (error != 0 && dm_event_sent != 0)) &&
2949                         DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2950                                                 DM_EVENT_POSTCREATE)) {
2951                 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2952                                         dir_vp, DM_RIGHT_NULL,
2953                                         created ? XFS_ITOV(cdp):NULL,
2954                                         DM_RIGHT_NULL,
2955                                         dir_name, NULL,
2956                                         dm_di_mode, error, 0);
2957         }
2958         return error;
2959
2960  error2:
2961  error1:
2962         xfs_bmap_cancel(&free_list);
2963  abort_return:
2964         cancel_flags |= XFS_TRANS_ABORT;
2965  error_return:
2966         xfs_trans_cancel(tp, cancel_flags);
2967         XFS_QM_DQRELE(mp, udqp);
2968         XFS_QM_DQRELE(mp, gdqp);
2969
2970         if (!dp_joined_to_trans && (dp != NULL)) {
2971                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2972         }
2973
2974         goto std_return;
2975 }
2976
2977
2978 /*
2979  * xfs_rmdir
2980  *
2981  */
2982 STATIC int
2983 xfs_rmdir(
2984         bhv_desc_t              *dir_bdp,
2985         bhv_vname_t             *dentry,
2986         cred_t                  *credp)
2987 {
2988         char                    *name = VNAME(dentry);
2989         xfs_inode_t             *dp;
2990         xfs_inode_t             *cdp;   /* child directory */
2991         xfs_trans_t             *tp;
2992         xfs_mount_t             *mp;
2993         int                     error;
2994         xfs_bmap_free_t         free_list;
2995         xfs_fsblock_t           first_block;
2996         int                     cancel_flags;
2997         int                     committed;
2998         bhv_vnode_t             *dir_vp;
2999         int                     dm_di_mode = 0;
3000         int                     last_cdp_link;
3001         int                     namelen;
3002         uint                    resblks;
3003
3004         dir_vp = BHV_TO_VNODE(dir_bdp);
3005         dp = XFS_BHVTOI(dir_bdp);
3006         mp = dp->i_mount;
3007
3008         vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3009
3010         if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3011                 return XFS_ERROR(EIO);
3012         namelen = VNAMELEN(dentry);
3013
3014         if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3015                 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3016                                         dir_vp, DM_RIGHT_NULL,
3017                                         NULL, DM_RIGHT_NULL,
3018                                         name, NULL, 0, 0, 0);
3019                 if (error)
3020                         return XFS_ERROR(error);
3021         }
3022
3023         /* Return through std_return after this point. */
3024
3025         cdp = NULL;
3026
3027         /*
3028          * We need to get a reference to cdp before we get our log
3029          * reservation.  The reason for this is that we cannot call
3030          * xfs_iget for an inode for which we do not have a reference
3031          * once we've acquired a log reservation.  This is because the
3032          * inode we are trying to get might be in xfs_inactive going
3033          * for a log reservation.  Since we'll have to wait for the
3034          * inactive code to complete before returning from xfs_iget,
3035          * we need to make sure that we don't have log space reserved
3036          * when we call xfs_iget.  Instead we get an unlocked reference
3037          * to the inode before getting our log reservation.
3038          */
3039         error = xfs_get_dir_entry(dentry, &cdp);
3040         if (error) {
3041                 REMOVE_DEBUG_TRACE(__LINE__);
3042                 goto std_return;
3043         }
3044         mp = dp->i_mount;
3045         dm_di_mode = cdp->i_d.di_mode;
3046
3047         /*
3048          * Get the dquots for the inodes.
3049          */
3050         error = XFS_QM_DQATTACH(mp, dp, 0);
3051         if (!error && dp != cdp)
3052                 error = XFS_QM_DQATTACH(mp, cdp, 0);
3053         if (error) {
3054                 IRELE(cdp);
3055                 REMOVE_DEBUG_TRACE(__LINE__);
3056                 goto std_return;
3057         }
3058
3059         tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3060         cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3061         /*
3062          * We try to get the real space reservation first,
3063          * allowing for directory btree deletion(s) implying
3064          * possible bmap insert(s).  If we can't get the space
3065          * reservation then we use 0 instead, and avoid the bmap
3066          * btree insert(s) in the directory code by, if the bmap
3067          * insert tries to happen, instead trimming the LAST
3068          * block from the directory.
3069          */
3070         resblks = XFS_REMOVE_SPACE_RES(mp);
3071         error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3072                         XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3073         if (error == ENOSPC) {
3074                 resblks = 0;
3075                 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3076                                 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3077         }
3078         if (error) {
3079                 ASSERT(error != ENOSPC);
3080                 cancel_flags = 0;
3081                 IRELE(cdp);
3082                 goto error_return;
3083         }
3084         XFS_BMAP_INIT(&free_list, &first_block);
3085
3086         /*
3087          * Now lock the child directory inode and the parent directory
3088          * inode in the proper order.  This will take care of validating
3089          * that the directory entry for the child directory inode has
3090          * not changed while we were obtaining a log reservation.
3091          */
3092         error = xfs_lock_dir_and_entry(dp, dentry, cdp);
3093         if (error) {
3094                 xfs_trans_cancel(tp, cancel_flags);
3095                 IRELE(cdp);
3096                 goto std_return;
3097         }
3098
3099         xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3100         if (dp != cdp) {
3101                 /*
3102                  * Only increment the parent directory vnode count if
3103                  * we didn't bump it in looking up cdp.  The only time
3104                  * we don't bump it is when we're looking up ".".
3105                  */
3106                 VN_HOLD(dir_vp);
3107         }
3108
3109         ITRACE(cdp);
3110         xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3111
3112         ASSERT(cdp->i_d.di_nlink >= 2);
3113         if (cdp->i_d.di_nlink != 2) {
3114                 error = XFS_ERROR(ENOTEMPTY);
3115                 goto error_return;
3116         }
3117         if (!xfs_dir_isempty(cdp)) {
3118                 error = XFS_ERROR(ENOTEMPTY);
3119                 goto error_return;
3120         }
3121
3122         error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3123                                         &first_block, &free_list, resblks);
3124         if (error)
3125                 goto error1;
3126
3127         xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3128
3129         /*
3130          * Bump the in memory generation count on the parent
3131          * directory so that other can know that it has changed.
3132          */
3133         dp->i_gen++;
3134
3135         /*
3136          * Drop the link from cdp's "..".
3137          */
3138         error = xfs_droplink(tp, dp);
3139         if (error) {
3140                 goto error1;
3141         }
3142
3143         /*
3144          * Drop the link from dp to cdp.
3145          */
3146         error = xfs_droplink(tp, cdp);
3147         if (error) {
3148                 goto error1;
3149         }
3150
3151         /*
3152          * Drop the "." link from cdp to self.
3153          */
3154         error = xfs_droplink(tp, cdp);
3155         if (error) {
3156                 goto error1;
3157         }
3158
3159         /* Determine these before committing transaction */
3160         last_cdp_link = (cdp)->i_d.di_nlink==0;
3161
3162         /*
3163          * Take an extra ref on the child vnode so that it
3164          * does not go to xfs_inactive() from within the commit.
3165          */
3166         IHOLD(cdp);
3167
3168         /*
3169          * If this is a synchronous mount, make sure that the
3170          * rmdir transaction goes to disk before returning to
3171          * the user.
3172          */
3173         if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3174                 xfs_trans_set_sync(tp);
3175         }
3176
3177         error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
3178         if (error) {
3179                 xfs_bmap_cancel(&free_list);
3180                 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3181                                  XFS_TRANS_ABORT));
3182                 IRELE(cdp);
3183                 goto std_return;
3184         }
3185
3186         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3187         if (error) {
3188                 IRELE(cdp);
3189                 goto std_return;
3190         }
3191
3192
3193         /*
3194          * Let interposed file systems know about removed links.
3195          */
3196         bhv_vop_link_removed(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3197
3198         IRELE(cdp);
3199
3200         /* Fall through to std_return with error = 0 or the errno
3201          * from xfs_trans_commit. */
3202  std_return:
3203         if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3204                 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3205                                         dir_vp, DM_RIGHT_NULL,
3206                                         NULL, DM_RIGHT_NULL,
3207                                         name, NULL, dm_di_mode,
3208                                         error, 0);
3209         }
3210         return error;
3211
3212  error1:
3213         xfs_bmap_cancel(&free_list);
3214         cancel_flags |= XFS_TRANS_ABORT;
3215         /* FALLTHROUGH */
3216
3217  error_return:
3218         xfs_trans_cancel(tp, cancel_flags);
3219         goto std_return;
3220 }
3221
3222
3223 /*
3224  * Read dp's entries starting at uiop->uio_offset and translate them into
3225  * bufsize bytes worth of struct dirents starting at bufbase.
3226  */
3227 STATIC int
3228 xfs_readdir(
3229         bhv_desc_t      *dir_bdp,
3230         uio_t           *uiop,
3231         cred_t          *credp,
3232         int             *eofp)
3233 {
3234         xfs_inode_t     *dp;
3235         xfs_trans_t     *tp = NULL;
3236         int             error = 0;
3237         uint            lock_mode;
3238
3239         vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3240                                                (inst_t *)__return_address);
3241         dp = XFS_BHVTOI(dir_bdp);
3242
3243         if (XFS_FORCED_SHUTDOWN(dp->i_mount))
3244                 return XFS_ERROR(EIO);
3245
3246         lock_mode = xfs_ilock_map_shared(dp);
3247         error = xfs_dir_getdents(tp, dp, uiop, eofp);
3248         xfs_iunlock_map_shared(dp, lock_mode);
3249         return error;
3250 }
3251
3252
3253 STATIC int
3254 xfs_symlink(
3255         bhv_desc_t              *dir_bdp,
3256         bhv_vname_t             *dentry,
3257         bhv_vattr_t             *vap,
3258         char                    *target_path,
3259         bhv_vnode_t             **vpp,
3260         cred_t                  *credp)
3261 {
3262         xfs_trans_t             *tp;
3263         xfs_mount_t             *mp;
3264         xfs_inode_t             *dp;
3265         xfs_inode_t             *ip;
3266         int                     error;
3267         int                     pathlen;
3268         xfs_bmap_free_t         free_list;
3269         xfs_fsblock_t           first_block;
3270         boolean_t               dp_joined_to_trans;
3271         bhv_vnode_t             *dir_vp;
3272         uint                    cancel_flags;
3273         int                     committed;
3274         xfs_fileoff_t           first_fsb;
3275         xfs_filblks_t           fs_blocks;
3276         int                     nmaps;
3277         xfs_bmbt_irec_t         mval[SYMLINK_MAPS];
3278         xfs_daddr_t             d;
3279         char                    *cur_chunk;
3280         int                     byte_cnt;
3281         int                     n;
3282         xfs_buf_t               *bp;
3283         xfs_prid_t              prid;
3284         struct xfs_dquot        *udqp, *gdqp;
3285         uint                    resblks;
3286         char                    *link_name = VNAME(dentry);
3287         int                     link_namelen;
3288
3289         *vpp = NULL;
3290         dir_vp = BHV_TO_VNODE(dir_bdp);
3291         dp = XFS_BHVTOI(dir_bdp);
3292         dp_joined_to_trans = B_FALSE;
3293         error = 0;
3294         ip = NULL;
3295         tp = NULL;
3296
3297         vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3298
3299         mp = dp->i_mount;
3300
3301         if (XFS_FORCED_SHUTDOWN(mp))
3302                 return XFS_ERROR(EIO);
3303
3304         link_namelen = VNAMELEN(dentry);
3305
3306         /*
3307          * Check component lengths of the target path name.
3308          */
3309         pathlen = strlen(target_path);
3310         if (pathlen >= MAXPATHLEN)      /* total string too long */
3311                 return XFS_ERROR(ENAMETOOLONG);
3312         if (pathlen >= MAXNAMELEN) {    /* is any component too long? */
3313                 int len, total;
3314                 char *path;
3315
3316                 for (total = 0, path = target_path; total < pathlen;) {
3317                         /*
3318                          * Skip any slashes.
3319                          */
3320                         while(*path == '/') {
3321                                 total++;
3322                                 path++;
3323                         }
3324
3325                         /*
3326                          * Count up to the next slash or end of path.
3327                          * Error out if the component is bigger than MAXNAMELEN.
3328                          */
3329                         for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3330                                 if (++len >= MAXNAMELEN) {
3331                                         error = ENAMETOOLONG;
3332                                         return error;
3333                                 }
3334                         }
3335                 }
3336         }
3337
3338         if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3339                 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3340                                         DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3341                                         link_name, target_path, 0, 0, 0);
3342                 if (error)
3343                         return error;
3344         }
3345
3346         /* Return through std_return after this point. */
3347
3348         udqp = gdqp = NULL;
3349         if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3350                 prid = dp->i_d.di_projid;
3351         else if (vap->va_mask & XFS_AT_PROJID)
3352                 prid = (xfs_prid_t)vap->va_projid;
3353         else
3354                 prid = (xfs_prid_t)dfltprid;
3355
3356         /*
3357          * Make sure that we have allocated dquot(s) on disk.
3358          */
3359         error = XFS_QM_DQVOPALLOC(mp, dp,
3360                         current_fsuid(credp), current_fsgid(credp), prid,
3361                         XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3362         if (error)
3363                 goto std_return;
3364
3365         tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3366         cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3367         /*
3368          * The symlink will fit into the inode data fork?
3369          * There can't be any attributes so we get the whole variable part.
3370          */
3371         if (pathlen <= XFS_LITINO(mp))
3372                 fs_blocks = 0;
3373         else
3374                 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3375         resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3376         error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3377                         XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3378         if (error == ENOSPC && fs_blocks == 0) {
3379                 resblks = 0;
3380                 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3381                                 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3382         }
3383         if (error) {
3384                 cancel_flags = 0;
3385                 dp = NULL;
3386                 goto error_return;
3387         }
3388
3389         xfs_ilock(dp, XFS_ILOCK_EXCL);
3390
3391         /*
3392          * Check whether the directory allows new symlinks or not.
3393          */
3394         if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3395                 error = XFS_ERROR(EPERM);
3396                 goto error_return;
3397         }
3398
3399         /*
3400          * Reserve disk quota : blocks and inode.
3401          */
3402         error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3403         if (error)
3404                 goto error_return;
3405
3406         /*
3407          * Check for ability to enter directory entry, if no space reserved.
3408          */
3409         if (resblks == 0 &&
3410             (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3411                 goto error_return;
3412         /*
3413          * Initialize the bmap freelist prior to calling either
3414          * bmapi or the directory create code.
3415          */
3416         XFS_BMAP_INIT(&free_list, &first_block);
3417
3418         /*
3419          * Allocate an inode for the symlink.
3420          */
3421         error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3422                                1, 0, credp, prid, resblks > 0, &ip, NULL);
3423         if (error) {
3424                 if (error == ENOSPC)
3425                         goto error_return;
3426                 goto error1;
3427         }
3428         ITRACE(ip);
3429
3430         VN_HOLD(dir_vp);
3431         xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3432         dp_joined_to_trans = B_TRUE;
3433
3434         /*
3435          * Also attach the dquot(s) to it, if applicable.
3436          */
3437         XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3438
3439         if (resblks)
3440                 resblks -= XFS_IALLOC_SPACE_RES(mp);
3441         /*
3442          * If the symlink will fit into the inode, write it inline.
3443          */
3444         if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3445                 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3446                 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3447                 ip->i_d.di_size = pathlen;
3448
3449                 /*
3450                  * The inode was initially created in extent format.
3451                  */
3452                 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3453                 ip->i_df.if_flags |= XFS_IFINLINE;
3454
3455                 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3456                 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3457
3458         } else {
3459                 first_fsb = 0;
3460                 nmaps = SYMLINK_MAPS;
3461
3462                 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3463                                   XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3464                                   &first_block, resblks, mval, &nmaps,
3465                                   &free_list, NULL);
3466                 if (error) {
3467                         goto error1;
3468                 }
3469
3470                 if (resblks)
3471                         resblks -= fs_blocks;
3472                 ip->i_d.di_size = pathlen;
3473                 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3474
3475                 cur_chunk = target_path;
3476                 for (n = 0; n < nmaps; n++) {
3477                         d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3478                         byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3479                         bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3480                                                BTOBB(byte_cnt), 0);
3481                         ASSERT(bp && !XFS_BUF_GETERROR(bp));
3482                         if (pathlen < byte_cnt) {
3483                                 byte_cnt = pathlen;
3484                         }
3485                         pathlen -= byte_cnt;
3486
3487                         memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3488                         cur_chunk += byte_cnt;
3489
3490                         xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3491                 }
3492         }
3493
3494         /*
3495          * Create the directory entry for the symlink.
3496          */
3497         error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3498                                    &first_block, &free_list, resblks);
3499         if (error)
3500                 goto error1;
3501         xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3502         xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3503
3504         /*
3505          * Bump the in memory version number of the parent directory
3506          * so that other processes accessing it will recognize that
3507          * the directory has changed.
3508          */
3509         dp->i_gen++;
3510
3511         /*
3512          * If this is a synchronous mount, make sure that the
3513          * symlink transaction goes to disk before returning to
3514          * the user.
3515          */
3516         if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3517                 xfs_trans_set_sync(tp);
3518         }
3519
3520         /*
3521          * xfs_trans_commit normally decrements the vnode ref count
3522          * when it unlocks the inode. Since we want to return the
3523          * vnode to the caller, we bump the vnode ref count now.
3524          */
3525         IHOLD(ip);
3526
3527         error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
3528         if (error) {
3529                 goto error2;
3530         }
3531         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3532         XFS_QM_DQRELE(mp, udqp);
3533         XFS_QM_DQRELE(mp, gdqp);
3534
3535         /* Fall through to std_return with error = 0 or errno from
3536          * xfs_trans_commit     */
3537 std_return:
3538         if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3539                              DM_EVENT_POSTSYMLINK)) {
3540                 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3541                                         dir_vp, DM_RIGHT_NULL,
3542                                         error ? NULL : XFS_ITOV(ip),
3543                                         DM_RIGHT_NULL, link_name, target_path,
3544                                         0, error, 0);
3545         }
3546
3547         if (!error) {
3548                 bhv_vnode_t *vp;
3549
3550                 ASSERT(ip);
3551                 vp = XFS_ITOV(ip);
3552                 *vpp = vp;
3553         }
3554         return error;
3555
3556  error2:
3557         IRELE(ip);
3558  error1:
3559         xfs_bmap_cancel(&free_list);
3560         cancel_flags |= XFS_TRANS_ABORT;
3561  error_return:
3562         xfs_trans_cancel(tp, cancel_flags);
3563         XFS_QM_DQRELE(mp, udqp);
3564         XFS_QM_DQRELE(mp, gdqp);
3565
3566         if (!dp_joined_to_trans && (dp != NULL)) {
3567                 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3568         }
3569
3570         goto std_return;
3571 }
3572
3573
3574 /*
3575  * xfs_fid2
3576  *
3577  * A fid routine that takes a pointer to a previously allocated
3578  * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3579  */
3580 STATIC int
3581 xfs_fid2(
3582         bhv_desc_t      *bdp,
3583         fid_t           *fidp)
3584 {
3585         xfs_inode_t     *ip;
3586         xfs_fid2_t      *xfid;
3587
3588         vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3589                                        (inst_t *)__return_address);
3590         ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3591
3592         xfid = (xfs_fid2_t *)fidp;
3593         ip = XFS_BHVTOI(bdp);
3594         xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3595         xfid->fid_pad = 0;
3596         /*
3597          * use memcpy because the inode is a long long and there's no
3598          * assurance that xfid->fid_ino is properly aligned.
3599          */
3600         memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3601         xfid->fid_gen = ip->i_d.di_gen;
3602
3603         return 0;
3604 }
3605
3606
3607 /*
3608  * xfs_rwlock
3609  */
3610 int
3611 xfs_rwlock(
3612         bhv_desc_t      *bdp,
3613         bhv_vrwlock_t   locktype)
3614 {
3615         xfs_inode_t     *ip;
3616         bhv_vnode_t     *vp;
3617
3618         vp = BHV_TO_VNODE(bdp);
3619         if (VN_ISDIR(vp))
3620                 return 1;
3621         ip = XFS_BHVTOI(bdp);
3622         if (locktype == VRWLOCK_WRITE) {
3623                 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3624         } else if (locktype == VRWLOCK_TRY_READ) {
3625                 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3626         } else if (locktype == VRWLOCK_TRY_WRITE) {
3627                 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3628         } else {
3629                 ASSERT((locktype == VRWLOCK_READ) ||
3630                        (locktype == VRWLOCK_WRITE_DIRECT));
3631                 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3632         }
3633
3634         return 1;
3635 }
3636
3637
3638 /*
3639  * xfs_rwunlock
3640  */
3641 void
3642 xfs_rwunlock(
3643         bhv_desc_t      *bdp,
3644         bhv_vrwlock_t   locktype)
3645 {
3646         xfs_inode_t     *ip;
3647         bhv_vnode_t     *vp;
3648
3649         vp = BHV_TO_VNODE(bdp);
3650         if (VN_ISDIR(vp))
3651                 return;
3652         ip = XFS_BHVTOI(bdp);
3653         if (locktype == VRWLOCK_WRITE) {
3654                 /*
3655                  * In the write case, we may have added a new entry to
3656                  * the reference cache.  This might store a pointer to
3657                  * an inode to be released in this inode.  If it is there,
3658                  * clear the pointer and release the inode after unlocking
3659                  * this one.
3660                  */
3661                 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3662         } else {
3663                 ASSERT((locktype == VRWLOCK_READ) ||
3664                        (locktype == VRWLOCK_WRITE_DIRECT));
3665                 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3666         }
3667         return;
3668 }
3669
3670 STATIC int
3671 xfs_inode_flush(
3672         bhv_desc_t      *bdp,
3673         int             flags)
3674 {
3675         xfs_inode_t     *ip;
3676         xfs_mount_t     *mp;
3677         xfs_inode_log_item_t *iip;
3678         int             error = 0;
3679
3680         ip = XFS_BHVTOI(bdp);
3681         mp = ip->i_mount;
3682         iip = ip->i_itemp;
3683
3684         if (XFS_FORCED_SHUTDOWN(mp))
3685                 return XFS_ERROR(EIO);
3686
3687         /*
3688          * Bypass inodes which have already been cleaned by
3689          * the inode flush clustering code inside xfs_iflush
3690          */
3691         if ((ip->i_update_core == 0) &&
3692             ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3693                 return 0;
3694
3695         if (flags & FLUSH_LOG) {
3696                 if (iip && iip->ili_last_lsn) {
3697                         xlog_t          *log = mp->m_log;
3698                         xfs_lsn_t       sync_lsn;
3699                         int             s, log_flags = XFS_LOG_FORCE;
3700
3701                         s = GRANT_LOCK(log);
3702                         sync_lsn = log->l_last_sync_lsn;
3703                         GRANT_UNLOCK(log, s);
3704
3705                         if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3706                                 return 0;
3707
3708                         if (flags & FLUSH_SYNC)
3709                                 log_flags |= XFS_LOG_SYNC;
3710                         return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3711                 }
3712         }
3713
3714         /*
3715          * We make this non-blocking if the inode is contended,
3716          * return EAGAIN to indicate to the caller that they
3717          * did not succeed. This prevents the flush path from
3718          * blocking on inodes inside another operation right
3719          * now, they get caught later by xfs_sync.
3720          */
3721         if (flags & FLUSH_INODE) {
3722                 int     flush_flags;
3723
3724                 if (xfs_ipincount(ip))
3725                         return EAGAIN;
3726
3727                 if (flags & FLUSH_SYNC) {
3728                         xfs_ilock(ip, XFS_ILOCK_SHARED);
3729                         xfs_iflock(ip);
3730                 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3731                         if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3732                                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3733                                 return EAGAIN;
3734                         }
3735                 } else {
3736                         return EAGAIN;
3737                 }
3738
3739                 if (flags & FLUSH_SYNC)
3740                         flush_flags = XFS_IFLUSH_SYNC;
3741                 else
3742                         flush_flags = XFS_IFLUSH_ASYNC;
3743
3744                 error = xfs_iflush(ip, flush_flags);
3745                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3746         }
3747
3748         return error;
3749 }
3750
3751 int
3752 xfs_set_dmattrs (
3753         bhv_desc_t      *bdp,
3754         u_int           evmask,
3755         u_int16_t       state,
3756         cred_t          *credp)
3757 {
3758         xfs_inode_t     *ip;
3759         xfs_trans_t     *tp;
3760         xfs_mount_t     *mp;
3761         int             error;
3762
3763         if (!capable(CAP_SYS_ADMIN))
3764                 return XFS_ERROR(EPERM);
3765
3766         ip = XFS_BHVTOI(bdp);
3767         mp = ip->i_mount;
3768
3769         if (XFS_FORCED_SHUTDOWN(mp))
3770                 return XFS_ERROR(EIO);
3771
3772         tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3773         error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3774         if (error) {
3775                 xfs_trans_cancel(tp, 0);
3776                 return error;
3777         }
3778         xfs_ilock(ip, XFS_ILOCK_EXCL);
3779         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3780
3781         ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3782         ip->i_iocore.io_dmstate  = ip->i_d.di_dmstate  = state;
3783
3784         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3785         IHOLD(ip);
3786         error = xfs_trans_commit(tp, 0, NULL);
3787
3788         return error;
3789 }
3790
3791 STATIC int
3792 xfs_reclaim(
3793         bhv_desc_t      *bdp)
3794 {
3795         xfs_inode_t     *ip;
3796         bhv_vnode_t     *vp;
3797
3798         vp = BHV_TO_VNODE(bdp);
3799         ip = XFS_BHVTOI(bdp);
3800
3801         vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3802
3803         ASSERT(!VN_MAPPED(vp));
3804
3805         /* bad inode, get out here ASAP */
3806         if (VN_BAD(vp)) {
3807                 xfs_ireclaim(ip);
3808                 return 0;
3809         }
3810
3811         vn_iowait(vp);
3812
3813         ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3814
3815         /*
3816          * Make sure the atime in the XFS inode is correct before freeing the
3817          * Linux inode.
3818          */
3819         xfs_synchronize_atime(ip);
3820
3821         /* If we have nothing to flush with this inode then complete the
3822          * teardown now, otherwise break the link between the xfs inode
3823          * and the linux inode and clean up the xfs inode later. This
3824          * avoids flushing the inode to disk during the delete operation
3825          * itself.
3826          */
3827         if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3828                 xfs_ilock(ip, XFS_ILOCK_EXCL);
3829                 xfs_iflock(ip);
3830                 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3831         } else {
3832                 xfs_mount_t     *mp = ip->i_mount;
3833
3834                 /* Protect sync from us */
3835                 XFS_MOUNT_ILOCK(mp);
3836                 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3837                 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3838                 ip->i_flags |= XFS_IRECLAIMABLE;
3839                 XFS_MOUNT_IUNLOCK(mp);
3840         }
3841         return 0;
3842 }
3843
3844 int
3845 xfs_finish_reclaim(
3846         xfs_inode_t     *ip,
3847         int             locked,
3848         int             sync_mode)
3849 {
3850         xfs_ihash_t     *ih = ip->i_hash;
3851         bhv_vnode_t     *vp = XFS_ITOV_NULL(ip);
3852         int             error;
3853
3854         if (vp && VN_BAD(vp))
3855                 goto reclaim;
3856
3857         /* The hash lock here protects a thread in xfs_iget_core from
3858          * racing with us on linking the inode back with a vnode.
3859          * Once we have the XFS_IRECLAIM flag set it will not touch
3860          * us.
3861          */
3862         write_lock(&ih->ih_lock);
3863         if ((ip->i_flags & XFS_IRECLAIM) ||
3864             (!(ip->i_flags & XFS_IRECLAIMABLE) && vp == NULL)) {
3865                 write_unlock(&ih->ih_lock);
3866                 if (locked) {
3867                         xfs_ifunlock(ip);
3868                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
3869                 }
3870                 return 1;
3871         }
3872         ip->i_flags |= XFS_IRECLAIM;
3873         write_unlock(&ih->ih_lock);
3874
3875         /*
3876          * If the inode is still dirty, then flush it out.  If the inode
3877          * is not in the AIL, then it will be OK to flush it delwri as
3878          * long as xfs_iflush() does not keep any references to the inode.
3879          * We leave that decision up to xfs_iflush() since it has the
3880          * knowledge of whether it's OK to simply do a delwri flush of
3881          * the inode or whether we need to wait until the inode is
3882          * pulled from the AIL.
3883          * We get the flush lock regardless, though, just to make sure
3884          * we don't free it while it is being flushed.
3885          */
3886         if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3887                 if (!locked) {
3888                         xfs_ilock(ip, XFS_ILOCK_EXCL);
3889                         xfs_iflock(ip);
3890                 }
3891
3892                 if (ip->i_update_core ||
3893                     ((ip->i_itemp != NULL) &&
3894                      (ip->i_itemp->ili_format.ilf_fields != 0))) {
3895                         error = xfs_iflush(ip, sync_mode);
3896                         /*
3897                          * If we hit an error, typically because of filesystem
3898                          * shutdown, we don't need to let vn_reclaim to know
3899                          * because we're gonna reclaim the inode anyway.
3900                          */
3901                         if (error) {
3902                                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3903                                 goto reclaim;
3904                         }
3905                         xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3906                 }
3907
3908                 ASSERT(ip->i_update_core == 0);
3909                 ASSERT(ip->i_itemp == NULL ||
3910                        ip->i_itemp->ili_format.ilf_fields == 0);
3911                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3912         } else if (locked) {
3913                 /*
3914                  * We are not interested in doing an iflush if we're
3915                  * in the process of shutting down the filesystem forcibly.
3916                  * So, just reclaim the inode.
3917                  */
3918                 xfs_ifunlock(ip);
3919                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3920         }
3921
3922  reclaim:
3923         xfs_ireclaim(ip);
3924         return 0;
3925 }
3926
3927 int
3928 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3929 {
3930         int             purged;
3931         xfs_inode_t     *ip, *n;
3932         int             done = 0;
3933
3934         while (!done) {
3935                 purged = 0;
3936                 XFS_MOUNT_ILOCK(mp);
3937                 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3938                         if (noblock) {
3939                                 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3940                                         continue;
3941                                 if (xfs_ipincount(ip) ||
3942                                     !xfs_iflock_nowait(ip)) {
3943                                         xfs_iunlock(ip, XFS_ILOCK_EXCL);
3944                                         continue;
3945                                 }
3946                         }
3947                         XFS_MOUNT_IUNLOCK(mp);
3948                         if (xfs_finish_reclaim(ip, noblock,
3949                                         XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3950                                 delay(1);
3951                         purged = 1;
3952                         break;
3953                 }
3954
3955                 done = !purged;
3956         }
3957
3958         XFS_MOUNT_IUNLOCK(mp);
3959         return 0;
3960 }
3961
3962 /*
3963  * xfs_alloc_file_space()
3964  *      This routine allocates disk space for the given file.
3965  *
3966  *      If alloc_type == 0, this request is for an ALLOCSP type
3967  *      request which will change the file size.  In this case, no
3968  *      DMAPI event will be generated by the call.  A TRUNCATE event
3969  *      will be generated later by xfs_setattr.
3970  *
3971  *      If alloc_type != 0, this request is for a RESVSP type
3972  *      request, and a DMAPI DM_EVENT_WRITE will be generated if the
3973  *      lower block boundary byte address is less than the file's
3974  *      length.
3975  *
3976  * RETURNS:
3977  *       0 on success
3978  *      errno on error
3979  *
3980  */
3981 STATIC int
3982 xfs_alloc_file_space(
3983         xfs_inode_t             *ip,
3984         xfs_off_t               offset,
3985         xfs_off_t               len,
3986         int                     alloc_type,
3987         int                     attr_flags)
3988 {
3989         xfs_mount_t             *mp = ip->i_mount;
3990         xfs_off_t               count;
3991         xfs_filblks_t           allocated_fsb;
3992         xfs_filblks_t           allocatesize_fsb;
3993         xfs_extlen_t            extsz, temp;
3994         xfs_fileoff_t           startoffset_fsb;
3995         xfs_fsblock_t           firstfsb;
3996         int                     nimaps;
3997         int                     bmapi_flag;
3998         int                     quota_flag;
3999         int                     rt;
4000         xfs_trans_t             *tp;
4001         xfs_bmbt_irec_t         imaps[1], *imapp;
4002         xfs_bmap_free_t         free_list;
4003         uint                    qblocks, resblks, resrtextents;
4004         int                     committed;
4005         int                     error;
4006
4007         vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4008
4009         if (XFS_FORCED_SHUTDOWN(mp))
4010                 return XFS_ERROR(EIO);
4011
4012         rt = XFS_IS_REALTIME_INODE(ip);
4013         if (unlikely(rt)) {
4014                 if (!(extsz = ip->i_d.di_extsize))
4015                         extsz = mp->m_sb.sb_rextsize;
4016         } else {
4017                 extsz = ip->i_d.di_extsize;
4018         }
4019
4020         if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4021                 return error;
4022
4023         if (len <= 0)
4024                 return XFS_ERROR(EINVAL);
4025
4026         count = len;
4027         error = 0;
4028         imapp = &imaps[0];
4029         nimaps = 1;
4030         bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4031         startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4032         allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4033
4034         /*      Generate a DMAPI event if needed.       */
4035         if (alloc_type != 0 && offset < ip->i_d.di_size &&
4036                         (attr_flags&ATTR_DMI) == 0  &&
4037                         DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4038                 xfs_off_t           end_dmi_offset;
4039
4040                 end_dmi_offset = offset+len;
4041                 if (end_dmi_offset > ip->i_d.di_size)
4042                         end_dmi_offset = ip->i_d.di_size;
4043                 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4044                         offset, end_dmi_offset - offset,
4045                         0, NULL);
4046                 if (error)
4047                         return error;
4048         }
4049
4050         /*
4051          * Allocate file space until done or until there is an error
4052          */
4053 retry:
4054         while (allocatesize_fsb && !error) {
4055                 xfs_fileoff_t   s, e;
4056
4057                 /*
4058                  * Determine space reservations for data/realtime.
4059                  */
4060                 if (unlikely(extsz)) {
4061                         s = startoffset_fsb;
4062                         do_div(s, extsz);
4063                         s *= extsz;
4064                         e = startoffset_fsb + allocatesize_fsb;
4065                         if ((temp = do_mod(startoffset_fsb, extsz)))
4066                                 e += temp;
4067                         if ((temp = do_mod(e, extsz)))
4068                                 e += extsz - temp;
4069                 } else {
4070                         s = 0;
4071                         e = allocatesize_fsb;
4072                 }
4073
4074                 if (unlikely(rt)) {
4075                         resrtextents = qblocks = (uint)(e - s);
4076                         resrtextents /= mp->m_sb.sb_rextsize;
4077                         resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4078                         quota_flag = XFS_QMOPT_RES_RTBLKS;
4079                 } else {
4080                         resrtextents = 0;
4081                         resblks = qblocks = \
4082                                 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
4083                         quota_flag = XFS_QMOPT_RES_REGBLKS;
4084                 }
4085
4086                 /*
4087                  * Allocate and setup the transaction.
4088                  */
4089                 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4090                 error = xfs_trans_reserve(tp, resblks,
4091                                           XFS_WRITE_LOG_RES(mp), resrtextents,
4092                                           XFS_TRANS_PERM_LOG_RES,
4093                                           XFS_WRITE_LOG_COUNT);
4094                 /*
4095                  * Check for running out of space
4096                  */
4097                 if (error) {
4098                         /*
4099                          * Free the transaction structure.
4100                          */
4101                         ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4102                         xfs_trans_cancel(tp, 0);
4103                         break;
4104                 }
4105                 xfs_ilock(ip, XFS_ILOCK_EXCL);
4106                 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
4107                                                       qblocks, 0, quota_flag);
4108                 if (error)
4109                         goto error1;
4110
4111                 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4112                 xfs_trans_ihold(tp, ip);
4113
4114                 /*
4115                  * Issue the xfs_bmapi() call to allocate the blocks
4116                  */
4117                 XFS_BMAP_INIT(&free_list, &firstfsb);
4118                 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4119                                   allocatesize_fsb, bmapi_flag,
4120                                   &firstfsb, 0, imapp, &nimaps,
4121                                   &free_list, NULL);
4122                 if (error) {
4123                         goto error0;
4124                 }
4125
4126                 /*
4127                  * Complete the transaction
4128                  */
4129                 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4130                 if (error) {
4131                         goto error0;
4132                 }
4133
4134                 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4135                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4136                 if (error) {
4137                         break;
4138                 }
4139
4140                 allocated_fsb = imapp->br_blockcount;
4141
4142                 if (nimaps == 0) {
4143                         error = XFS_ERROR(ENOSPC);
4144                         break;
4145                 }
4146
4147                 startoffset_fsb += allocated_fsb;
4148                 allocatesize_fsb -= allocated_fsb;
4149         }
4150 dmapi_enospc_check:
4151         if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4152             DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4153
4154                 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4155                                 XFS_ITOV(ip), DM_RIGHT_NULL,
4156                                 XFS_ITOV(ip), DM_RIGHT_NULL,
4157                                 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4158                 if (error == 0)
4159                         goto retry;     /* Maybe DMAPI app. has made space */
4160                 /* else fall through with error from XFS_SEND_DATA */
4161         }
4162
4163         return error;
4164
4165 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4166         xfs_bmap_cancel(&free_list);
4167         XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4168
4169 error1: /* Just cancel transaction */
4170         xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4171         xfs_iunlock(ip, XFS_ILOCK_EXCL);
4172         goto dmapi_enospc_check;
4173 }
4174
4175 /*
4176  * Zero file bytes between startoff and endoff inclusive.
4177  * The iolock is held exclusive and no blocks are buffered.
4178  */
4179 STATIC int
4180 xfs_zero_remaining_bytes(
4181         xfs_inode_t             *ip,
4182         xfs_off_t               startoff,
4183         xfs_off_t               endoff)
4184 {
4185         xfs_bmbt_irec_t         imap;
4186         xfs_fileoff_t           offset_fsb;
4187         xfs_off_t               lastoffset;
4188         xfs_off_t               offset;
4189         xfs_buf_t               *bp;
4190         xfs_mount_t             *mp = ip->i_mount;
4191         int                     nimap;
4192         int                     error = 0;
4193
4194         bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4195                                 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4196                                 mp->m_rtdev_targp : mp->m_ddev_targp);
4197
4198         for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4199                 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4200                 nimap = 1;
4201                 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4202                         NULL, 0, &imap, &nimap, NULL, NULL);
4203                 if (error || nimap < 1)
4204                         break;
4205                 ASSERT(imap.br_blockcount >= 1);
4206                 ASSERT(imap.br_startoff == offset_fsb);
4207                 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4208                 if (lastoffset > endoff)
4209                         lastoffset = endoff;
4210                 if (imap.br_startblock == HOLESTARTBLOCK)
4211                         continue;
4212                 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4213                 if (imap.br_state == XFS_EXT_UNWRITTEN)
4214                         continue;
4215                 XFS_BUF_UNDONE(bp);
4216                 XFS_BUF_UNWRITE(bp);
4217                 XFS_BUF_READ(bp);
4218                 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4219                 xfsbdstrat(mp, bp);
4220                 if ((error = xfs_iowait(bp))) {
4221                         xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4222                                           mp, bp, XFS_BUF_ADDR(bp));
4223                         break;
4224                 }
4225                 memset(XFS_BUF_PTR(bp) +
4226                         (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4227                       0, lastoffset - offset + 1);
4228                 XFS_BUF_UNDONE(bp);
4229                 XFS_BUF_UNREAD(bp);
4230                 XFS_BUF_WRITE(bp);
4231                 xfsbdstrat(mp, bp);
4232                 if ((error = xfs_iowait(bp))) {
4233                         xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4234                                           mp, bp, XFS_BUF_ADDR(bp));
4235                         break;
4236                 }
4237         }
4238         xfs_buf_free(bp);
4239         return error;
4240 }
4241
4242 /*
4243  * xfs_free_file_space()
4244  *      This routine frees disk space for the given file.
4245  *
4246  *      This routine is only called by xfs_change_file_space
4247  *      for an UNRESVSP type call.
4248  *
4249  * RETURNS:
4250  *       0 on success
4251  *      errno on error
4252  *
4253  */
4254 STATIC int
4255 xfs_free_file_space(
4256         xfs_inode_t             *ip,
4257         xfs_off_t               offset,
4258         xfs_off_t               len,
4259         int                     attr_flags)
4260 {
4261         bhv_vnode_t             *vp;
4262         int                     committed;
4263         int                     done;
4264         xfs_off_t               end_dmi_offset;
4265         xfs_fileoff_t           endoffset_fsb;
4266         int                     error;
4267         xfs_fsblock_t           firstfsb;
4268         xfs_bmap_free_t         free_list;
4269         xfs_off_t               ilen;
4270         xfs_bmbt_irec_t         imap;
4271         xfs_off_t               ioffset;
4272         xfs_extlen_t            mod=0;
4273         xfs_mount_t             *mp;
4274         int                     nimap;
4275         uint                    resblks;
4276         int                     rounding;
4277         int                     rt;
4278         xfs_fileoff_t           startoffset_fsb;
4279         xfs_trans_t             *tp;
4280         int                     need_iolock = 1;
4281
4282         vp = XFS_ITOV(ip);
4283         mp = ip->i_mount;
4284
4285         vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4286
4287         if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4288                 return error;
4289
4290         error = 0;
4291         if (len <= 0)   /* if nothing being freed */
4292                 return error;
4293         rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4294         startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4295         end_dmi_offset = offset + len;
4296         endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4297
4298         if (offset < ip->i_d.di_size &&
4299             (attr_flags & ATTR_DMI) == 0 &&
4300             DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4301                 if (end_dmi_offset > ip->i_d.di_size)
4302                         end_dmi_offset = ip->i_d.di_size;
4303                 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4304                                 offset, end_dmi_offset - offset,
4305                                 AT_DELAY_FLAG(attr_flags), NULL);
4306                 if (error)
4307                         return error;
4308         }
4309
4310         if (attr_flags & ATTR_NOLOCK)
4311                 need_iolock = 0;
4312         if (need_iolock) {
4313                 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4314                 vn_iowait(vp);  /* wait for the completion of any pending DIOs */
4315         }
4316
4317         rounding = MAX((__uint8_t)(1 << mp->m_sb.sb_blocklog),
4318                         (__uint8_t)NBPP);
4319         ilen = len + (offset & (rounding - 1));
4320         ioffset = offset & ~(rounding - 1);
4321         if (ilen & (rounding - 1))
4322                 ilen = (ilen + rounding) & ~(rounding - 1);
4323
4324         if (VN_CACHED(vp) != 0) {
4325                 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4326                                 ctooff(offtoct(ioffset)), -1);
4327                 bhv_vop_flushinval_pages(vp, ctooff(offtoct(ioffset)),
4328                                 -1, FI_REMAPF_LOCKED);
4329         }
4330
4331         /*
4332          * Need to zero the stuff we're not freeing, on disk.
4333          * If its a realtime file & can't use unwritten extents then we
4334          * actually need to zero the extent edges.  Otherwise xfs_bunmapi
4335          * will take care of it for us.
4336          */
4337         if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4338                 nimap = 1;
4339                 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4340                         1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4341                 if (error)
4342                         goto out_unlock_iolock;
4343                 ASSERT(nimap == 0 || nimap == 1);
4344                 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4345                         xfs_daddr_t     block;
4346
4347                         ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4348                         block = imap.br_startblock;
4349                         mod = do_div(block, mp->m_sb.sb_rextsize);
4350                         if (mod)
4351                                 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4352                 }
4353                 nimap = 1;
4354                 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4355                         1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4356                 if (error)
4357                         goto out_unlock_iolock;
4358                 ASSERT(nimap == 0 || nimap == 1);
4359                 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4360                         ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4361                         mod++;
4362                         if (mod && (mod != mp->m_sb.sb_rextsize))
4363                                 endoffset_fsb -= mod;
4364                 }
4365         }
4366         if ((done = (endoffset_fsb <= startoffset_fsb)))
4367                 /*
4368                  * One contiguous piece to clear
4369                  */
4370                 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4371         else {
4372                 /*
4373                  * Some full blocks, possibly two pieces to clear
4374                  */
4375                 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4376                         error = xfs_zero_remaining_bytes(ip, offset,
4377                                 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4378                 if (!error &&
4379                     XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4380                         error = xfs_zero_remaining_bytes(ip,
4381                                 XFS_FSB_TO_B(mp, endoffset_fsb),
4382                                 offset + len - 1);
4383         }
4384
4385         /*
4386          * free file space until done or until there is an error
4387          */
4388         resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4389         while (!error && !done) {
4390
4391                 /*
4392                  * allocate and setup the transaction
4393                  */
4394                 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4395                 error = xfs_trans_reserve(tp,
4396                                           resblks,
4397                                           XFS_WRITE_LOG_RES(mp),
4398                                           0,
4399                                           XFS_TRANS_PERM_LOG_RES,
4400                                           XFS_WRITE_LOG_COUNT);
4401
4402                 /*
4403                  * check for running out of space
4404                  */
4405                 if (error) {
4406                         /*
4407                          * Free the transaction structure.
4408                          */
4409                         ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4410                         xfs_trans_cancel(tp, 0);
4411                         break;
4412                 }
4413                 xfs_ilock(ip, XFS_ILOCK_EXCL);
4414                 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4415                                 ip->i_udquot, ip->i_gdquot, resblks, 0,
4416                                 XFS_QMOPT_RES_REGBLKS);
4417                 if (error)
4418                         goto error1;
4419
4420                 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4421                 xfs_trans_ihold(tp, ip);
4422
4423                 /*
4424                  * issue the bunmapi() call to free the blocks
4425                  */
4426                 XFS_BMAP_INIT(&free_list, &firstfsb);
4427                 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4428                                   endoffset_fsb - startoffset_fsb,
4429                                   0, 2, &firstfsb, &free_list, NULL, &done);
4430                 if (error) {
4431                         goto error0;
4432                 }
4433
4434                 /*
4435                  * complete the transaction
4436                  */
4437                 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4438                 if (error) {
4439                         goto error0;
4440                 }
4441
4442                 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4443                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4444         }
4445
4446  out_unlock_iolock:
4447         if (need_iolock)
4448                 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4449         return error;
4450
4451  error0:
4452         xfs_bmap_cancel(&free_list);
4453  error1:
4454         xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4455         xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4456                     XFS_ILOCK_EXCL);
4457         return error;
4458 }
4459
4460 /*
4461  * xfs_change_file_space()
4462  *      This routine allocates or frees disk space for the given file.
4463  *      The user specified parameters are checked for alignment and size
4464  *      limitations.
4465  *
4466  * RETURNS:
4467  *       0 on success
4468  *      errno on error
4469  *
4470  */
4471 int
4472 xfs_change_file_space(
4473         bhv_desc_t      *bdp,
4474         int             cmd,
4475         xfs_flock64_t   *bf,
4476         xfs_off_t       offset,
4477         cred_t          *credp,
4478         int             attr_flags)
4479 {
4480         int             clrprealloc;
4481         int             error;
4482         xfs_fsize_t     fsize;
4483         xfs_inode_t     *ip;
4484         xfs_mount_t     *mp;
4485         int             setprealloc;
4486         xfs_off_t       startoffset;
4487         xfs_off_t       llen;
4488         xfs_trans_t     *tp;
4489         bhv_vattr_t     va;
4490         bhv_vnode_t     *vp;
4491
4492         vp = BHV_TO_VNODE(bdp);
4493         vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4494
4495         ip = XFS_BHVTOI(bdp);
4496         mp = ip->i_mount;
4497
4498         /*
4499          * must be a regular file and have write permission
4500          */
4501         if (!VN_ISREG(vp))
4502                 return XFS_ERROR(EINVAL);
4503
4504         xfs_ilock(ip, XFS_ILOCK_SHARED);
4505
4506         if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4507                 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4508                 return error;
4509         }
4510
4511         xfs_iunlock(ip, XFS_ILOCK_SHARED);
4512
4513         switch (bf->l_whence) {
4514         case 0: /*SEEK_SET*/
4515                 break;
4516         case 1: /*SEEK_CUR*/
4517                 bf->l_start += offset;
4518                 break;
4519         case 2: /*SEEK_END*/
4520                 bf->l_start += ip->i_d.di_size;
4521                 break;
4522         default:
4523                 return XFS_ERROR(EINVAL);
4524         }
4525
4526         llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4527
4528         if (   (bf->l_start < 0)
4529             || (bf->l_start > XFS_MAXIOFFSET(mp))
4530             || (bf->l_start + llen < 0)
4531             || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4532                 return XFS_ERROR(EINVAL);
4533
4534         bf->l_whence = 0;
4535
4536         startoffset = bf->l_start;
4537         fsize = ip->i_d.di_size;
4538
4539         /*
4540          * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4541          * file space.
4542          * These calls do NOT zero the data space allocated to the file,
4543          * nor do they change the file size.
4544          *
4545          * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4546          * space.
4547          * These calls cause the new file data to be zeroed and the file
4548          * size to be changed.
4549          */
4550         setprealloc = clrprealloc = 0;
4551
4552         switch (cmd) {
4553         case XFS_IOC_RESVSP:
4554         case XFS_IOC_RESVSP64:
4555                 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4556                                                                 1, attr_flags);
4557                 if (error)
4558                         return error;
4559                 setprealloc = 1;
4560                 break;
4561
4562         case XFS_IOC_UNRESVSP:
4563         case XFS_IOC_UNRESVSP64:
4564                 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4565                                                                 attr_flags)))
4566                         return error;
4567                 break;
4568
4569         case XFS_IOC_ALLOCSP:
4570         case XFS_IOC_ALLOCSP64:
4571         case XFS_IOC_FREESP:
4572         case XFS_IOC_FREESP64:
4573                 if (startoffset > fsize) {
4574                         error = xfs_alloc_file_space(ip, fsize,
4575                                         startoffset - fsize, 0, attr_flags);
4576                         if (error)
4577                                 break;
4578                 }
4579
4580                 va.va_mask = XFS_AT_SIZE;
4581                 va.va_size = startoffset;
4582
4583                 error = xfs_setattr(bdp, &va, attr_flags, credp);
4584
4585                 if (error)
4586                         return error;
4587
4588                 clrprealloc = 1;
4589                 break;
4590
4591         default:
4592                 ASSERT(0);
4593                 return XFS_ERROR(EINVAL);
4594         }
4595
4596         /*
4597          * update the inode timestamp, mode, and prealloc flag bits
4598          */
4599         tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4600
4601         if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4602                                       0, 0, 0))) {
4603                 /* ASSERT(0); */
4604                 xfs_trans_cancel(tp, 0);
4605                 return error;
4606         }
4607
4608         xfs_ilock(ip, XFS_ILOCK_EXCL);
4609
4610         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4611         xfs_trans_ihold(tp, ip);
4612
4613         if ((attr_flags & ATTR_DMI) == 0) {
4614                 ip->i_d.di_mode &= ~S_ISUID;
4615
4616                 /*
4617                  * Note that we don't have to worry about mandatory
4618                  * file locking being disabled here because we only
4619                  * clear the S_ISGID bit if the Group execute bit is
4620                  * on, but if it was on then mandatory locking wouldn't
4621                  * have been enabled.
4622                  */
4623                 if (ip->i_d.di_mode & S_IXGRP)
4624                         ip->i_d.di_mode &= ~S_ISGID;
4625
4626                 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4627         }
4628         if (setprealloc)
4629                 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4630         else if (clrprealloc)
4631                 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4632
4633         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4634         xfs_trans_set_sync(tp);
4635
4636         error = xfs_trans_commit(tp, 0, NULL);
4637
4638         xfs_iunlock(ip, XFS_ILOCK_EXCL);
4639
4640         return error;
4641 }
4642
4643 bhv_vnodeops_t xfs_vnodeops = {
4644         BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4645         .vop_open               = xfs_open,
4646         .vop_close              = xfs_close,
4647         .vop_read               = xfs_read,
4648 #ifdef HAVE_SENDFILE
4649         .vop_sendfile           = xfs_sendfile,
4650 #endif
4651 #ifdef HAVE_SPLICE
4652         .vop_splice_read        = xfs_splice_read,
4653         .vop_splice_write       = xfs_splice_write,
4654 #endif
4655         .vop_write              = xfs_write,
4656         .vop_ioctl              = xfs_ioctl,
4657         .vop_getattr            = xfs_getattr,
4658         .vop_setattr            = xfs_setattr,
4659         .vop_access             = xfs_access,
4660         .vop_lookup             = xfs_lookup,
4661         .vop_create             = xfs_create,
4662         .vop_remove             = xfs_remove,
4663         .vop_link               = xfs_link,
4664         .vop_rename             = xfs_rename,
4665         .vop_mkdir              = xfs_mkdir,
4666         .vop_rmdir              = xfs_rmdir,
4667         .vop_readdir            = xfs_readdir,
4668         .vop_symlink            = xfs_symlink,
4669         .vop_readlink           = xfs_readlink,
4670         .vop_fsync              = xfs_fsync,
4671         .vop_inactive           = xfs_inactive,
4672         .vop_fid2               = xfs_fid2,
4673         .vop_rwlock             = xfs_rwlock,
4674         .vop_rwunlock           = xfs_rwunlock,
4675         .vop_bmap               = xfs_bmap,
4676         .vop_reclaim            = xfs_reclaim,
4677         .vop_attr_get           = xfs_attr_get,
4678         .vop_attr_set           = xfs_attr_set,
4679         .vop_attr_remove        = xfs_attr_remove,
4680         .vop_attr_list          = xfs_attr_list,
4681         .vop_link_removed       = (vop_link_removed_t)fs_noval,
4682         .vop_vnode_change       = (vop_vnode_change_t)fs_noval,
4683         .vop_tosspages          = fs_tosspages,
4684         .vop_flushinval_pages   = fs_flushinval_pages,
4685         .vop_flush_pages        = fs_flush_pages,
4686         .vop_release            = xfs_release,
4687         .vop_iflush             = xfs_inode_flush,
4688 };