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