]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_iops.c
xfs: allow linkat() on O_TMPFILE files
[karo-tx-linux.git] / fs / xfs / xfs_iops.c
1 /*
2  * Copyright (c) 2000-2005 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 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_sb.h"
25 #include "xfs_ag.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_inode.h"
29 #include "xfs_bmap.h"
30 #include "xfs_bmap_util.h"
31 #include "xfs_acl.h"
32 #include "xfs_quota.h"
33 #include "xfs_error.h"
34 #include "xfs_attr.h"
35 #include "xfs_trans.h"
36 #include "xfs_trace.h"
37 #include "xfs_icache.h"
38 #include "xfs_symlink.h"
39 #include "xfs_da_btree.h"
40 #include "xfs_dir2_priv.h"
41 #include "xfs_dinode.h"
42 #include "xfs_trans_space.h"
43
44 #include <linux/capability.h>
45 #include <linux/xattr.h>
46 #include <linux/namei.h>
47 #include <linux/posix_acl.h>
48 #include <linux/security.h>
49 #include <linux/fiemap.h>
50 #include <linux/slab.h>
51
52 static int
53 xfs_initxattrs(
54         struct inode            *inode,
55         const struct xattr      *xattr_array,
56         void                    *fs_info)
57 {
58         const struct xattr      *xattr;
59         struct xfs_inode        *ip = XFS_I(inode);
60         int                     error = 0;
61
62         for (xattr = xattr_array; xattr->name != NULL; xattr++) {
63                 error = xfs_attr_set(ip, xattr->name, xattr->value,
64                                      xattr->value_len, ATTR_SECURE);
65                 if (error < 0)
66                         break;
67         }
68         return error;
69 }
70
71 /*
72  * Hook in SELinux.  This is not quite correct yet, what we really need
73  * here (as we do for default ACLs) is a mechanism by which creation of
74  * these attrs can be journalled at inode creation time (along with the
75  * inode, of course, such that log replay can't cause these to be lost).
76  */
77
78 STATIC int
79 xfs_init_security(
80         struct inode    *inode,
81         struct inode    *dir,
82         const struct qstr *qstr)
83 {
84         return security_inode_init_security(inode, dir, qstr,
85                                             &xfs_initxattrs, NULL);
86 }
87
88 static void
89 xfs_dentry_to_name(
90         struct xfs_name *namep,
91         struct dentry   *dentry,
92         int             mode)
93 {
94         namep->name = dentry->d_name.name;
95         namep->len = dentry->d_name.len;
96         namep->type = xfs_mode_to_ftype[(mode & S_IFMT) >> S_SHIFT];
97 }
98
99 STATIC void
100 xfs_cleanup_inode(
101         struct inode    *dir,
102         struct inode    *inode,
103         struct dentry   *dentry)
104 {
105         struct xfs_name teardown;
106
107         /* Oh, the horror.
108          * If we can't add the ACL or we fail in
109          * xfs_init_security we must back out.
110          * ENOSPC can hit here, among other things.
111          */
112         xfs_dentry_to_name(&teardown, dentry, 0);
113
114         xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
115         iput(inode);
116 }
117
118 STATIC int
119 xfs_vn_mknod(
120         struct inode    *dir,
121         struct dentry   *dentry,
122         umode_t         mode,
123         dev_t           rdev)
124 {
125         struct inode    *inode;
126         struct xfs_inode *ip = NULL;
127         struct posix_acl *default_acl = NULL;
128         struct xfs_name name;
129         int             error;
130
131         /*
132          * Irix uses Missed'em'V split, but doesn't want to see
133          * the upper 5 bits of (14bit) major.
134          */
135         if (S_ISCHR(mode) || S_ISBLK(mode)) {
136                 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
137                         return -EINVAL;
138                 rdev = sysv_encode_dev(rdev);
139         } else {
140                 rdev = 0;
141         }
142
143         if (IS_POSIXACL(dir)) {
144                 default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT);
145                 if (IS_ERR(default_acl))
146                         return PTR_ERR(default_acl);
147
148                 if (!default_acl)
149                         mode &= ~current_umask();
150         }
151
152         xfs_dentry_to_name(&name, dentry, mode);
153         error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
154         if (unlikely(error))
155                 goto out_free_acl;
156
157         inode = VFS_I(ip);
158
159         error = xfs_init_security(inode, dir, &dentry->d_name);
160         if (unlikely(error))
161                 goto out_cleanup_inode;
162
163         if (default_acl) {
164                 error = -xfs_inherit_acl(inode, default_acl);
165                 default_acl = NULL;
166                 if (unlikely(error))
167                         goto out_cleanup_inode;
168         }
169
170
171         d_instantiate(dentry, inode);
172         return -error;
173
174  out_cleanup_inode:
175         xfs_cleanup_inode(dir, inode, dentry);
176  out_free_acl:
177         posix_acl_release(default_acl);
178         return -error;
179 }
180
181 STATIC int
182 xfs_vn_create(
183         struct inode    *dir,
184         struct dentry   *dentry,
185         umode_t         mode,
186         bool            flags)
187 {
188         return xfs_vn_mknod(dir, dentry, mode, 0);
189 }
190
191 STATIC int
192 xfs_vn_mkdir(
193         struct inode    *dir,
194         struct dentry   *dentry,
195         umode_t         mode)
196 {
197         return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
198 }
199
200 STATIC struct dentry *
201 xfs_vn_lookup(
202         struct inode    *dir,
203         struct dentry   *dentry,
204         unsigned int flags)
205 {
206         struct xfs_inode *cip;
207         struct xfs_name name;
208         int             error;
209
210         if (dentry->d_name.len >= MAXNAMELEN)
211                 return ERR_PTR(-ENAMETOOLONG);
212
213         xfs_dentry_to_name(&name, dentry, 0);
214         error = xfs_lookup(XFS_I(dir), &name, &cip, NULL);
215         if (unlikely(error)) {
216                 if (unlikely(error != ENOENT))
217                         return ERR_PTR(-error);
218                 d_add(dentry, NULL);
219                 return NULL;
220         }
221
222         return d_splice_alias(VFS_I(cip), dentry);
223 }
224
225 STATIC struct dentry *
226 xfs_vn_ci_lookup(
227         struct inode    *dir,
228         struct dentry   *dentry,
229         unsigned int flags)
230 {
231         struct xfs_inode *ip;
232         struct xfs_name xname;
233         struct xfs_name ci_name;
234         struct qstr     dname;
235         int             error;
236
237         if (dentry->d_name.len >= MAXNAMELEN)
238                 return ERR_PTR(-ENAMETOOLONG);
239
240         xfs_dentry_to_name(&xname, dentry, 0);
241         error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name);
242         if (unlikely(error)) {
243                 if (unlikely(error != ENOENT))
244                         return ERR_PTR(-error);
245                 /*
246                  * call d_add(dentry, NULL) here when d_drop_negative_children
247                  * is called in xfs_vn_mknod (ie. allow negative dentries
248                  * with CI filesystems).
249                  */
250                 return NULL;
251         }
252
253         /* if exact match, just splice and exit */
254         if (!ci_name.name)
255                 return d_splice_alias(VFS_I(ip), dentry);
256
257         /* else case-insensitive match... */
258         dname.name = ci_name.name;
259         dname.len = ci_name.len;
260         dentry = d_add_ci(dentry, VFS_I(ip), &dname);
261         kmem_free(ci_name.name);
262         return dentry;
263 }
264
265 STATIC int
266 xfs_vn_link(
267         struct dentry   *old_dentry,
268         struct inode    *dir,
269         struct dentry   *dentry)
270 {
271         struct inode    *inode = old_dentry->d_inode;
272         struct xfs_name name;
273         int             error;
274
275         xfs_dentry_to_name(&name, dentry, inode->i_mode);
276
277         error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
278         if (unlikely(error))
279                 return -error;
280
281         ihold(inode);
282         d_instantiate(dentry, inode);
283         return 0;
284 }
285
286 STATIC int
287 xfs_vn_unlink(
288         struct inode    *dir,
289         struct dentry   *dentry)
290 {
291         struct xfs_name name;
292         int             error;
293
294         xfs_dentry_to_name(&name, dentry, 0);
295
296         error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
297         if (error)
298                 return error;
299
300         /*
301          * With unlink, the VFS makes the dentry "negative": no inode,
302          * but still hashed. This is incompatible with case-insensitive
303          * mode, so invalidate (unhash) the dentry in CI-mode.
304          */
305         if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
306                 d_invalidate(dentry);
307         return 0;
308 }
309
310 STATIC int
311 xfs_vn_symlink(
312         struct inode    *dir,
313         struct dentry   *dentry,
314         const char      *symname)
315 {
316         struct inode    *inode;
317         struct xfs_inode *cip = NULL;
318         struct xfs_name name;
319         int             error;
320         umode_t         mode;
321
322         mode = S_IFLNK |
323                 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
324         xfs_dentry_to_name(&name, dentry, mode);
325
326         error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
327         if (unlikely(error))
328                 goto out;
329
330         inode = VFS_I(cip);
331
332         error = xfs_init_security(inode, dir, &dentry->d_name);
333         if (unlikely(error))
334                 goto out_cleanup_inode;
335
336         d_instantiate(dentry, inode);
337         return 0;
338
339  out_cleanup_inode:
340         xfs_cleanup_inode(dir, inode, dentry);
341  out:
342         return -error;
343 }
344
345 STATIC int
346 xfs_vn_rename(
347         struct inode    *odir,
348         struct dentry   *odentry,
349         struct inode    *ndir,
350         struct dentry   *ndentry)
351 {
352         struct inode    *new_inode = ndentry->d_inode;
353         struct xfs_name oname;
354         struct xfs_name nname;
355
356         xfs_dentry_to_name(&oname, odentry, 0);
357         xfs_dentry_to_name(&nname, ndentry, odentry->d_inode->i_mode);
358
359         return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode),
360                            XFS_I(ndir), &nname, new_inode ?
361                                                 XFS_I(new_inode) : NULL);
362 }
363
364 /*
365  * careful here - this function can get called recursively, so
366  * we need to be very careful about how much stack we use.
367  * uio is kmalloced for this reason...
368  */
369 STATIC void *
370 xfs_vn_follow_link(
371         struct dentry           *dentry,
372         struct nameidata        *nd)
373 {
374         char                    *link;
375         int                     error = -ENOMEM;
376
377         link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
378         if (!link)
379                 goto out_err;
380
381         error = -xfs_readlink(XFS_I(dentry->d_inode), link);
382         if (unlikely(error))
383                 goto out_kfree;
384
385         nd_set_link(nd, link);
386         return NULL;
387
388  out_kfree:
389         kfree(link);
390  out_err:
391         nd_set_link(nd, ERR_PTR(error));
392         return NULL;
393 }
394
395 STATIC void
396 xfs_vn_put_link(
397         struct dentry   *dentry,
398         struct nameidata *nd,
399         void            *p)
400 {
401         char            *s = nd_get_link(nd);
402
403         if (!IS_ERR(s))
404                 kfree(s);
405 }
406
407 STATIC int
408 xfs_vn_getattr(
409         struct vfsmount         *mnt,
410         struct dentry           *dentry,
411         struct kstat            *stat)
412 {
413         struct inode            *inode = dentry->d_inode;
414         struct xfs_inode        *ip = XFS_I(inode);
415         struct xfs_mount        *mp = ip->i_mount;
416
417         trace_xfs_getattr(ip);
418
419         if (XFS_FORCED_SHUTDOWN(mp))
420                 return -XFS_ERROR(EIO);
421
422         stat->size = XFS_ISIZE(ip);
423         stat->dev = inode->i_sb->s_dev;
424         stat->mode = ip->i_d.di_mode;
425         stat->nlink = ip->i_d.di_nlink;
426         stat->uid = inode->i_uid;
427         stat->gid = inode->i_gid;
428         stat->ino = ip->i_ino;
429         stat->atime = inode->i_atime;
430         stat->mtime = inode->i_mtime;
431         stat->ctime = inode->i_ctime;
432         stat->blocks =
433                 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
434
435
436         switch (inode->i_mode & S_IFMT) {
437         case S_IFBLK:
438         case S_IFCHR:
439                 stat->blksize = BLKDEV_IOSIZE;
440                 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
441                                    sysv_minor(ip->i_df.if_u2.if_rdev));
442                 break;
443         default:
444                 if (XFS_IS_REALTIME_INODE(ip)) {
445                         /*
446                          * If the file blocks are being allocated from a
447                          * realtime volume, then return the inode's realtime
448                          * extent size or the realtime volume's extent size.
449                          */
450                         stat->blksize =
451                                 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
452                 } else
453                         stat->blksize = xfs_preferred_iosize(mp);
454                 stat->rdev = 0;
455                 break;
456         }
457
458         return 0;
459 }
460
461 static void
462 xfs_setattr_mode(
463         struct xfs_inode        *ip,
464         struct iattr            *iattr)
465 {
466         struct inode            *inode = VFS_I(ip);
467         umode_t                 mode = iattr->ia_mode;
468
469         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
470
471         ip->i_d.di_mode &= S_IFMT;
472         ip->i_d.di_mode |= mode & ~S_IFMT;
473
474         inode->i_mode &= S_IFMT;
475         inode->i_mode |= mode & ~S_IFMT;
476 }
477
478 static void
479 xfs_setattr_time(
480         struct xfs_inode        *ip,
481         struct iattr            *iattr)
482 {
483         struct inode            *inode = VFS_I(ip);
484
485         ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
486
487         if (iattr->ia_valid & ATTR_ATIME) {
488                 inode->i_atime = iattr->ia_atime;
489                 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
490                 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
491         }
492         if (iattr->ia_valid & ATTR_CTIME) {
493                 inode->i_ctime = iattr->ia_ctime;
494                 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
495                 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
496         }
497         if (iattr->ia_valid & ATTR_MTIME) {
498                 inode->i_mtime = iattr->ia_mtime;
499                 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
500                 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
501         }
502 }
503
504 int
505 xfs_setattr_nonsize(
506         struct xfs_inode        *ip,
507         struct iattr            *iattr,
508         int                     flags)
509 {
510         xfs_mount_t             *mp = ip->i_mount;
511         struct inode            *inode = VFS_I(ip);
512         int                     mask = iattr->ia_valid;
513         xfs_trans_t             *tp;
514         int                     error;
515         kuid_t                  uid = GLOBAL_ROOT_UID, iuid = GLOBAL_ROOT_UID;
516         kgid_t                  gid = GLOBAL_ROOT_GID, igid = GLOBAL_ROOT_GID;
517         struct xfs_dquot        *udqp = NULL, *gdqp = NULL;
518         struct xfs_dquot        *olddquot1 = NULL, *olddquot2 = NULL;
519
520         trace_xfs_setattr(ip);
521
522         /* If acls are being inherited, we already have this checked */
523         if (!(flags & XFS_ATTR_NOACL)) {
524                 if (mp->m_flags & XFS_MOUNT_RDONLY)
525                         return XFS_ERROR(EROFS);
526
527                 if (XFS_FORCED_SHUTDOWN(mp))
528                         return XFS_ERROR(EIO);
529
530                 error = -inode_change_ok(inode, iattr);
531                 if (error)
532                         return XFS_ERROR(error);
533         }
534
535         ASSERT((mask & ATTR_SIZE) == 0);
536
537         /*
538          * If disk quotas is on, we make sure that the dquots do exist on disk,
539          * before we start any other transactions. Trying to do this later
540          * is messy. We don't care to take a readlock to look at the ids
541          * in inode here, because we can't hold it across the trans_reserve.
542          * If the IDs do change before we take the ilock, we're covered
543          * because the i_*dquot fields will get updated anyway.
544          */
545         if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
546                 uint    qflags = 0;
547
548                 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
549                         uid = iattr->ia_uid;
550                         qflags |= XFS_QMOPT_UQUOTA;
551                 } else {
552                         uid = inode->i_uid;
553                 }
554                 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
555                         gid = iattr->ia_gid;
556                         qflags |= XFS_QMOPT_GQUOTA;
557                 }  else {
558                         gid = inode->i_gid;
559                 }
560
561                 /*
562                  * We take a reference when we initialize udqp and gdqp,
563                  * so it is important that we never blindly double trip on
564                  * the same variable. See xfs_create() for an example.
565                  */
566                 ASSERT(udqp == NULL);
567                 ASSERT(gdqp == NULL);
568                 error = xfs_qm_vop_dqalloc(ip, xfs_kuid_to_uid(uid),
569                                            xfs_kgid_to_gid(gid),
570                                            xfs_get_projid(ip),
571                                            qflags, &udqp, &gdqp, NULL);
572                 if (error)
573                         return error;
574         }
575
576         tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
577         error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
578         if (error)
579                 goto out_dqrele;
580
581         xfs_ilock(ip, XFS_ILOCK_EXCL);
582
583         /*
584          * Change file ownership.  Must be the owner or privileged.
585          */
586         if (mask & (ATTR_UID|ATTR_GID)) {
587                 /*
588                  * These IDs could have changed since we last looked at them.
589                  * But, we're assured that if the ownership did change
590                  * while we didn't have the inode locked, inode's dquot(s)
591                  * would have changed also.
592                  */
593                 iuid = inode->i_uid;
594                 igid = inode->i_gid;
595                 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
596                 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
597
598                 /*
599                  * Do a quota reservation only if uid/gid is actually
600                  * going to change.
601                  */
602                 if (XFS_IS_QUOTA_RUNNING(mp) &&
603                     ((XFS_IS_UQUOTA_ON(mp) && !uid_eq(iuid, uid)) ||
604                      (XFS_IS_GQUOTA_ON(mp) && !gid_eq(igid, gid)))) {
605                         ASSERT(tp);
606                         error = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
607                                                 NULL, capable(CAP_FOWNER) ?
608                                                 XFS_QMOPT_FORCE_RES : 0);
609                         if (error)      /* out of quota */
610                                 goto out_trans_cancel;
611                 }
612         }
613
614         xfs_trans_ijoin(tp, ip, 0);
615
616         /*
617          * Change file ownership.  Must be the owner or privileged.
618          */
619         if (mask & (ATTR_UID|ATTR_GID)) {
620                 /*
621                  * CAP_FSETID overrides the following restrictions:
622                  *
623                  * The set-user-ID and set-group-ID bits of a file will be
624                  * cleared upon successful return from chown()
625                  */
626                 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
627                     !capable(CAP_FSETID))
628                         ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
629
630                 /*
631                  * Change the ownerships and register quota modifications
632                  * in the transaction.
633                  */
634                 if (!uid_eq(iuid, uid)) {
635                         if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
636                                 ASSERT(mask & ATTR_UID);
637                                 ASSERT(udqp);
638                                 olddquot1 = xfs_qm_vop_chown(tp, ip,
639                                                         &ip->i_udquot, udqp);
640                         }
641                         ip->i_d.di_uid = xfs_kuid_to_uid(uid);
642                         inode->i_uid = uid;
643                 }
644                 if (!gid_eq(igid, gid)) {
645                         if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
646                                 ASSERT(xfs_sb_version_has_pquotino(&mp->m_sb) ||
647                                        !XFS_IS_PQUOTA_ON(mp));
648                                 ASSERT(mask & ATTR_GID);
649                                 ASSERT(gdqp);
650                                 olddquot2 = xfs_qm_vop_chown(tp, ip,
651                                                         &ip->i_gdquot, gdqp);
652                         }
653                         ip->i_d.di_gid = xfs_kgid_to_gid(gid);
654                         inode->i_gid = gid;
655                 }
656         }
657
658         if (mask & ATTR_MODE)
659                 xfs_setattr_mode(ip, iattr);
660         if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
661                 xfs_setattr_time(ip, iattr);
662
663         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
664
665         XFS_STATS_INC(xs_ig_attrchg);
666
667         if (mp->m_flags & XFS_MOUNT_WSYNC)
668                 xfs_trans_set_sync(tp);
669         error = xfs_trans_commit(tp, 0);
670
671         xfs_iunlock(ip, XFS_ILOCK_EXCL);
672
673         /*
674          * Release any dquot(s) the inode had kept before chown.
675          */
676         xfs_qm_dqrele(olddquot1);
677         xfs_qm_dqrele(olddquot2);
678         xfs_qm_dqrele(udqp);
679         xfs_qm_dqrele(gdqp);
680
681         if (error)
682                 return XFS_ERROR(error);
683
684         /*
685          * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
686          *           update.  We could avoid this with linked transactions
687          *           and passing down the transaction pointer all the way
688          *           to attr_set.  No previous user of the generic
689          *           Posix ACL code seems to care about this issue either.
690          */
691         if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
692                 error = -xfs_acl_chmod(inode);
693                 if (error)
694                         return XFS_ERROR(error);
695         }
696
697         return 0;
698
699 out_trans_cancel:
700         xfs_trans_cancel(tp, 0);
701         xfs_iunlock(ip, XFS_ILOCK_EXCL);
702 out_dqrele:
703         xfs_qm_dqrele(udqp);
704         xfs_qm_dqrele(gdqp);
705         return error;
706 }
707
708 /*
709  * Truncate file.  Must have write permission and not be a directory.
710  */
711 int
712 xfs_setattr_size(
713         struct xfs_inode        *ip,
714         struct iattr            *iattr)
715 {
716         struct xfs_mount        *mp = ip->i_mount;
717         struct inode            *inode = VFS_I(ip);
718         int                     mask = iattr->ia_valid;
719         xfs_off_t               oldsize, newsize;
720         struct xfs_trans        *tp;
721         int                     error;
722         uint                    lock_flags = 0;
723         uint                    commit_flags = 0;
724
725         trace_xfs_setattr(ip);
726
727         if (mp->m_flags & XFS_MOUNT_RDONLY)
728                 return XFS_ERROR(EROFS);
729
730         if (XFS_FORCED_SHUTDOWN(mp))
731                 return XFS_ERROR(EIO);
732
733         error = -inode_change_ok(inode, iattr);
734         if (error)
735                 return XFS_ERROR(error);
736
737         ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
738         ASSERT(S_ISREG(ip->i_d.di_mode));
739         ASSERT((mask & (ATTR_UID|ATTR_GID|ATTR_ATIME|ATTR_ATIME_SET|
740                         ATTR_MTIME_SET|ATTR_KILL_PRIV|ATTR_TIMES_SET)) == 0);
741
742         oldsize = inode->i_size;
743         newsize = iattr->ia_size;
744
745         /*
746          * Short circuit the truncate case for zero length files.
747          */
748         if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
749                 if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
750                         return 0;
751
752                 /*
753                  * Use the regular setattr path to update the timestamps.
754                  */
755                 iattr->ia_valid &= ~ATTR_SIZE;
756                 return xfs_setattr_nonsize(ip, iattr, 0);
757         }
758
759         /*
760          * Make sure that the dquots are attached to the inode.
761          */
762         error = xfs_qm_dqattach(ip, 0);
763         if (error)
764                 return error;
765
766         /*
767          * Now we can make the changes.  Before we join the inode to the
768          * transaction, take care of the part of the truncation that must be
769          * done without the inode lock.  This needs to be done before joining
770          * the inode to the transaction, because the inode cannot be unlocked
771          * once it is a part of the transaction.
772          */
773         if (newsize > oldsize) {
774                 /*
775                  * Do the first part of growing a file: zero any data in the
776                  * last block that is beyond the old EOF.  We need to do this
777                  * before the inode is joined to the transaction to modify
778                  * i_size.
779                  */
780                 error = xfs_zero_eof(ip, newsize, oldsize);
781                 if (error)
782                         return error;
783         }
784
785         /*
786          * We are going to log the inode size change in this transaction so
787          * any previous writes that are beyond the on disk EOF and the new
788          * EOF that have not been written out need to be written here.  If we
789          * do not write the data out, we expose ourselves to the null files
790          * problem.
791          *
792          * Only flush from the on disk size to the smaller of the in memory
793          * file size or the new size as that's the range we really care about
794          * here and prevents waiting for other data not within the range we
795          * care about here.
796          */
797         if (oldsize != ip->i_d.di_size && newsize > ip->i_d.di_size) {
798                 error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
799                                                       ip->i_d.di_size, newsize);
800                 if (error)
801                         return error;
802         }
803
804         /*
805          * Wait for all direct I/O to complete.
806          */
807         inode_dio_wait(inode);
808
809         error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
810         if (error)
811                 return error;
812
813         tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
814         error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
815         if (error)
816                 goto out_trans_cancel;
817
818         truncate_setsize(inode, newsize);
819
820         commit_flags = XFS_TRANS_RELEASE_LOG_RES;
821         lock_flags |= XFS_ILOCK_EXCL;
822
823         xfs_ilock(ip, XFS_ILOCK_EXCL);
824
825         xfs_trans_ijoin(tp, ip, 0);
826
827         /*
828          * Only change the c/mtime if we are changing the size or we are
829          * explicitly asked to change it.  This handles the semantic difference
830          * between truncate() and ftruncate() as implemented in the VFS.
831          *
832          * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
833          * special case where we need to update the times despite not having
834          * these flags set.  For all other operations the VFS set these flags
835          * explicitly if it wants a timestamp update.
836          */
837         if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME)))) {
838                 iattr->ia_ctime = iattr->ia_mtime =
839                         current_fs_time(inode->i_sb);
840                 mask |= ATTR_CTIME | ATTR_MTIME;
841         }
842
843         /*
844          * The first thing we do is set the size to new_size permanently on
845          * disk.  This way we don't have to worry about anyone ever being able
846          * to look at the data being freed even in the face of a crash.
847          * What we're getting around here is the case where we free a block, it
848          * is allocated to another file, it is written to, and then we crash.
849          * If the new data gets written to the file but the log buffers
850          * containing the free and reallocation don't, then we'd end up with
851          * garbage in the blocks being freed.  As long as we make the new size
852          * permanent before actually freeing any blocks it doesn't matter if
853          * they get written to.
854          */
855         ip->i_d.di_size = newsize;
856         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
857
858         if (newsize <= oldsize) {
859                 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
860                 if (error)
861                         goto out_trans_abort;
862
863                 /*
864                  * Truncated "down", so we're removing references to old data
865                  * here - if we delay flushing for a long time, we expose
866                  * ourselves unduly to the notorious NULL files problem.  So,
867                  * we mark this inode and flush it when the file is closed,
868                  * and do not wait the usual (long) time for writeout.
869                  */
870                 xfs_iflags_set(ip, XFS_ITRUNCATED);
871
872                 /* A truncate down always removes post-EOF blocks. */
873                 xfs_inode_clear_eofblocks_tag(ip);
874         }
875
876         if (mask & ATTR_MODE)
877                 xfs_setattr_mode(ip, iattr);
878         if (mask & (ATTR_ATIME|ATTR_CTIME|ATTR_MTIME))
879                 xfs_setattr_time(ip, iattr);
880
881         xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
882
883         XFS_STATS_INC(xs_ig_attrchg);
884
885         if (mp->m_flags & XFS_MOUNT_WSYNC)
886                 xfs_trans_set_sync(tp);
887
888         error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
889 out_unlock:
890         if (lock_flags)
891                 xfs_iunlock(ip, lock_flags);
892         return error;
893
894 out_trans_abort:
895         commit_flags |= XFS_TRANS_ABORT;
896 out_trans_cancel:
897         xfs_trans_cancel(tp, commit_flags);
898         goto out_unlock;
899 }
900
901 STATIC int
902 xfs_vn_setattr(
903         struct dentry           *dentry,
904         struct iattr            *iattr)
905 {
906         struct xfs_inode        *ip = XFS_I(dentry->d_inode);
907         int                     error;
908
909         if (iattr->ia_valid & ATTR_SIZE) {
910                 xfs_ilock(ip, XFS_IOLOCK_EXCL);
911                 error = xfs_setattr_size(ip, iattr);
912                 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
913         } else {
914                 error = xfs_setattr_nonsize(ip, iattr, 0);
915         }
916
917         return -error;
918 }
919
920 STATIC int
921 xfs_vn_update_time(
922         struct inode            *inode,
923         struct timespec         *now,
924         int                     flags)
925 {
926         struct xfs_inode        *ip = XFS_I(inode);
927         struct xfs_mount        *mp = ip->i_mount;
928         struct xfs_trans        *tp;
929         int                     error;
930
931         trace_xfs_update_time(ip);
932
933         tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
934         error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
935         if (error) {
936                 xfs_trans_cancel(tp, 0);
937                 return -error;
938         }
939
940         xfs_ilock(ip, XFS_ILOCK_EXCL);
941         if (flags & S_CTIME) {
942                 inode->i_ctime = *now;
943                 ip->i_d.di_ctime.t_sec = (__int32_t)now->tv_sec;
944                 ip->i_d.di_ctime.t_nsec = (__int32_t)now->tv_nsec;
945         }
946         if (flags & S_MTIME) {
947                 inode->i_mtime = *now;
948                 ip->i_d.di_mtime.t_sec = (__int32_t)now->tv_sec;
949                 ip->i_d.di_mtime.t_nsec = (__int32_t)now->tv_nsec;
950         }
951         if (flags & S_ATIME) {
952                 inode->i_atime = *now;
953                 ip->i_d.di_atime.t_sec = (__int32_t)now->tv_sec;
954                 ip->i_d.di_atime.t_nsec = (__int32_t)now->tv_nsec;
955         }
956         xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
957         xfs_trans_log_inode(tp, ip, XFS_ILOG_TIMESTAMP);
958         return -xfs_trans_commit(tp, 0);
959 }
960
961 #define XFS_FIEMAP_FLAGS        (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
962
963 /*
964  * Call fiemap helper to fill in user data.
965  * Returns positive errors to xfs_getbmap.
966  */
967 STATIC int
968 xfs_fiemap_format(
969         void                    **arg,
970         struct getbmapx         *bmv,
971         int                     *full)
972 {
973         int                     error;
974         struct fiemap_extent_info *fieinfo = *arg;
975         u32                     fiemap_flags = 0;
976         u64                     logical, physical, length;
977
978         /* Do nothing for a hole */
979         if (bmv->bmv_block == -1LL)
980                 return 0;
981
982         logical = BBTOB(bmv->bmv_offset);
983         physical = BBTOB(bmv->bmv_block);
984         length = BBTOB(bmv->bmv_length);
985
986         if (bmv->bmv_oflags & BMV_OF_PREALLOC)
987                 fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN;
988         else if (bmv->bmv_oflags & BMV_OF_DELALLOC) {
989                 fiemap_flags |= (FIEMAP_EXTENT_DELALLOC |
990                                  FIEMAP_EXTENT_UNKNOWN);
991                 physical = 0;   /* no block yet */
992         }
993         if (bmv->bmv_oflags & BMV_OF_LAST)
994                 fiemap_flags |= FIEMAP_EXTENT_LAST;
995
996         error = fiemap_fill_next_extent(fieinfo, logical, physical,
997                                         length, fiemap_flags);
998         if (error > 0) {
999                 error = 0;
1000                 *full = 1;      /* user array now full */
1001         }
1002
1003         return -error;
1004 }
1005
1006 STATIC int
1007 xfs_vn_fiemap(
1008         struct inode            *inode,
1009         struct fiemap_extent_info *fieinfo,
1010         u64                     start,
1011         u64                     length)
1012 {
1013         xfs_inode_t             *ip = XFS_I(inode);
1014         struct getbmapx         bm;
1015         int                     error;
1016
1017         error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
1018         if (error)
1019                 return error;
1020
1021         /* Set up bmap header for xfs internal routine */
1022         bm.bmv_offset = BTOBB(start);
1023         /* Special case for whole file */
1024         if (length == FIEMAP_MAX_OFFSET)
1025                 bm.bmv_length = -1LL;
1026         else
1027                 bm.bmv_length = BTOBB(length);
1028
1029         /* We add one because in getbmap world count includes the header */
1030         bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM :
1031                                         fieinfo->fi_extents_max + 1;
1032         bm.bmv_count = min_t(__s32, bm.bmv_count,
1033                              (PAGE_SIZE * 16 / sizeof(struct getbmapx)));
1034         bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
1035         if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR)
1036                 bm.bmv_iflags |= BMV_IF_ATTRFORK;
1037         if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC))
1038                 bm.bmv_iflags |= BMV_IF_DELALLOC;
1039
1040         error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
1041         if (error)
1042                 return -error;
1043
1044         return 0;
1045 }
1046
1047 STATIC int
1048 xfs_vn_tmpfile(
1049         struct inode    *dir,
1050         struct dentry   *dentry,
1051         umode_t         mode)
1052 {
1053         int             error;
1054
1055         error = xfs_create_tmpfile(XFS_I(dir), dentry, mode);
1056
1057         return -error;
1058 }
1059
1060 static const struct inode_operations xfs_inode_operations = {
1061         .get_acl                = xfs_get_acl,
1062         .getattr                = xfs_vn_getattr,
1063         .setattr                = xfs_vn_setattr,
1064         .setxattr               = generic_setxattr,
1065         .getxattr               = generic_getxattr,
1066         .removexattr            = generic_removexattr,
1067         .listxattr              = xfs_vn_listxattr,
1068         .fiemap                 = xfs_vn_fiemap,
1069         .update_time            = xfs_vn_update_time,
1070 };
1071
1072 static const struct inode_operations xfs_dir_inode_operations = {
1073         .create                 = xfs_vn_create,
1074         .lookup                 = xfs_vn_lookup,
1075         .link                   = xfs_vn_link,
1076         .unlink                 = xfs_vn_unlink,
1077         .symlink                = xfs_vn_symlink,
1078         .mkdir                  = xfs_vn_mkdir,
1079         /*
1080          * Yes, XFS uses the same method for rmdir and unlink.
1081          *
1082          * There are some subtile differences deeper in the code,
1083          * but we use S_ISDIR to check for those.
1084          */
1085         .rmdir                  = xfs_vn_unlink,
1086         .mknod                  = xfs_vn_mknod,
1087         .rename                 = xfs_vn_rename,
1088         .get_acl                = xfs_get_acl,
1089         .getattr                = xfs_vn_getattr,
1090         .setattr                = xfs_vn_setattr,
1091         .setxattr               = generic_setxattr,
1092         .getxattr               = generic_getxattr,
1093         .removexattr            = generic_removexattr,
1094         .listxattr              = xfs_vn_listxattr,
1095         .update_time            = xfs_vn_update_time,
1096         .tmpfile                = xfs_vn_tmpfile,
1097 };
1098
1099 static const struct inode_operations xfs_dir_ci_inode_operations = {
1100         .create                 = xfs_vn_create,
1101         .lookup                 = xfs_vn_ci_lookup,
1102         .link                   = xfs_vn_link,
1103         .unlink                 = xfs_vn_unlink,
1104         .symlink                = xfs_vn_symlink,
1105         .mkdir                  = xfs_vn_mkdir,
1106         /*
1107          * Yes, XFS uses the same method for rmdir and unlink.
1108          *
1109          * There are some subtile differences deeper in the code,
1110          * but we use S_ISDIR to check for those.
1111          */
1112         .rmdir                  = xfs_vn_unlink,
1113         .mknod                  = xfs_vn_mknod,
1114         .rename                 = xfs_vn_rename,
1115         .get_acl                = xfs_get_acl,
1116         .getattr                = xfs_vn_getattr,
1117         .setattr                = xfs_vn_setattr,
1118         .setxattr               = generic_setxattr,
1119         .getxattr               = generic_getxattr,
1120         .removexattr            = generic_removexattr,
1121         .listxattr              = xfs_vn_listxattr,
1122         .update_time            = xfs_vn_update_time,
1123         .tmpfile                = xfs_vn_tmpfile,
1124 };
1125
1126 static const struct inode_operations xfs_symlink_inode_operations = {
1127         .readlink               = generic_readlink,
1128         .follow_link            = xfs_vn_follow_link,
1129         .put_link               = xfs_vn_put_link,
1130         .get_acl                = xfs_get_acl,
1131         .getattr                = xfs_vn_getattr,
1132         .setattr                = xfs_vn_setattr,
1133         .setxattr               = generic_setxattr,
1134         .getxattr               = generic_getxattr,
1135         .removexattr            = generic_removexattr,
1136         .listxattr              = xfs_vn_listxattr,
1137         .update_time            = xfs_vn_update_time,
1138 };
1139
1140 STATIC void
1141 xfs_diflags_to_iflags(
1142         struct inode            *inode,
1143         struct xfs_inode        *ip)
1144 {
1145         if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
1146                 inode->i_flags |= S_IMMUTABLE;
1147         else
1148                 inode->i_flags &= ~S_IMMUTABLE;
1149         if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
1150                 inode->i_flags |= S_APPEND;
1151         else
1152                 inode->i_flags &= ~S_APPEND;
1153         if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
1154                 inode->i_flags |= S_SYNC;
1155         else
1156                 inode->i_flags &= ~S_SYNC;
1157         if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
1158                 inode->i_flags |= S_NOATIME;
1159         else
1160                 inode->i_flags &= ~S_NOATIME;
1161 }
1162
1163 /*
1164  * Initialize the Linux inode, set up the operation vectors and
1165  * unlock the inode.
1166  *
1167  * When reading existing inodes from disk this is called directly
1168  * from xfs_iget, when creating a new inode it is called from
1169  * xfs_ialloc after setting up the inode.
1170  *
1171  * We are always called with an uninitialised linux inode here.
1172  * We need to initialise the necessary fields and take a reference
1173  * on it.
1174  */
1175 void
1176 xfs_setup_inode(
1177         struct xfs_inode        *ip)
1178 {
1179         struct inode            *inode = &ip->i_vnode;
1180         gfp_t                   gfp_mask;
1181
1182         inode->i_ino = ip->i_ino;
1183         inode->i_state = I_NEW;
1184
1185         inode_sb_list_add(inode);
1186         /* make the inode look hashed for the writeback code */
1187         hlist_add_fake(&inode->i_hash);
1188
1189         inode->i_mode   = ip->i_d.di_mode;
1190         set_nlink(inode, ip->i_d.di_nlink);
1191         inode->i_uid    = xfs_uid_to_kuid(ip->i_d.di_uid);
1192         inode->i_gid    = xfs_gid_to_kgid(ip->i_d.di_gid);
1193
1194         switch (inode->i_mode & S_IFMT) {
1195         case S_IFBLK:
1196         case S_IFCHR:
1197                 inode->i_rdev =
1198                         MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
1199                               sysv_minor(ip->i_df.if_u2.if_rdev));
1200                 break;
1201         default:
1202                 inode->i_rdev = 0;
1203                 break;
1204         }
1205
1206         inode->i_generation = ip->i_d.di_gen;
1207         i_size_write(inode, ip->i_d.di_size);
1208         inode->i_atime.tv_sec   = ip->i_d.di_atime.t_sec;
1209         inode->i_atime.tv_nsec  = ip->i_d.di_atime.t_nsec;
1210         inode->i_mtime.tv_sec   = ip->i_d.di_mtime.t_sec;
1211         inode->i_mtime.tv_nsec  = ip->i_d.di_mtime.t_nsec;
1212         inode->i_ctime.tv_sec   = ip->i_d.di_ctime.t_sec;
1213         inode->i_ctime.tv_nsec  = ip->i_d.di_ctime.t_nsec;
1214         xfs_diflags_to_iflags(inode, ip);
1215
1216         ip->d_ops = ip->i_mount->m_nondir_inode_ops;
1217         switch (inode->i_mode & S_IFMT) {
1218         case S_IFREG:
1219                 inode->i_op = &xfs_inode_operations;
1220                 inode->i_fop = &xfs_file_operations;
1221                 inode->i_mapping->a_ops = &xfs_address_space_operations;
1222                 break;
1223         case S_IFDIR:
1224                 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
1225                         inode->i_op = &xfs_dir_ci_inode_operations;
1226                 else
1227                         inode->i_op = &xfs_dir_inode_operations;
1228                 inode->i_fop = &xfs_dir_file_operations;
1229                 ip->d_ops = ip->i_mount->m_dir_inode_ops;
1230                 break;
1231         case S_IFLNK:
1232                 inode->i_op = &xfs_symlink_inode_operations;
1233                 if (!(ip->i_df.if_flags & XFS_IFINLINE))
1234                         inode->i_mapping->a_ops = &xfs_address_space_operations;
1235                 break;
1236         default:
1237                 inode->i_op = &xfs_inode_operations;
1238                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1239                 break;
1240         }
1241
1242         /*
1243          * Ensure all page cache allocations are done from GFP_NOFS context to
1244          * prevent direct reclaim recursion back into the filesystem and blowing
1245          * stacks or deadlocking.
1246          */
1247         gfp_mask = mapping_gfp_mask(inode->i_mapping);
1248         mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
1249
1250         /*
1251          * If there is no attribute fork no ACL can exist on this inode,
1252          * and it can't have any file capabilities attached to it either.
1253          */
1254         if (!XFS_IFORK_Q(ip)) {
1255                 inode_has_no_xattr(inode);
1256                 cache_no_acl(inode);
1257         }
1258
1259         xfs_iflags_clear(ip, XFS_INEW);
1260         barrier();
1261
1262         unlock_new_inode(inode);
1263 }