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