2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
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.
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.
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
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_inode.h"
30 #include "xfs_bmap_util.h"
32 #include "xfs_quota.h"
33 #include "xfs_error.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"
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>
55 const struct xattr *xattr_array,
58 const struct xattr *xattr;
59 struct xfs_inode *ip = XFS_I(inode);
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);
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).
82 const struct qstr *qstr)
84 return security_inode_init_security(inode, dir, qstr,
85 &xfs_initxattrs, NULL);
90 struct xfs_name *namep,
91 struct dentry *dentry,
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];
103 struct dentry *dentry)
105 struct xfs_name teardown;
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.
112 xfs_dentry_to_name(&teardown, dentry, 0);
114 xfs_remove(XFS_I(dir), &teardown, XFS_I(inode));
121 struct dentry *dentry,
126 struct xfs_inode *ip = NULL;
127 struct posix_acl *default_acl = NULL;
128 struct xfs_name name;
132 * Irix uses Missed'em'V split, but doesn't want to see
133 * the upper 5 bits of (14bit) major.
135 if (S_ISCHR(mode) || S_ISBLK(mode)) {
136 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
138 rdev = sysv_encode_dev(rdev);
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);
149 mode &= ~current_umask();
152 xfs_dentry_to_name(&name, dentry, mode);
153 error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip);
159 error = xfs_init_security(inode, dir, &dentry->d_name);
161 goto out_cleanup_inode;
164 error = -xfs_inherit_acl(inode, default_acl);
167 goto out_cleanup_inode;
171 d_instantiate(dentry, inode);
175 xfs_cleanup_inode(dir, inode, dentry);
177 posix_acl_release(default_acl);
184 struct dentry *dentry,
188 return xfs_vn_mknod(dir, dentry, mode, 0);
194 struct dentry *dentry,
197 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
200 STATIC struct dentry *
203 struct dentry *dentry,
206 struct xfs_inode *cip;
207 struct xfs_name name;
210 if (dentry->d_name.len >= MAXNAMELEN)
211 return ERR_PTR(-ENAMETOOLONG);
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);
222 return d_splice_alias(VFS_I(cip), dentry);
225 STATIC struct dentry *
228 struct dentry *dentry,
231 struct xfs_inode *ip;
232 struct xfs_name xname;
233 struct xfs_name ci_name;
237 if (dentry->d_name.len >= MAXNAMELEN)
238 return ERR_PTR(-ENAMETOOLONG);
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);
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).
253 /* if exact match, just splice and exit */
255 return d_splice_alias(VFS_I(ip), dentry);
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);
267 struct dentry *old_dentry,
269 struct dentry *dentry)
271 struct inode *inode = old_dentry->d_inode;
272 struct xfs_name name;
275 xfs_dentry_to_name(&name, dentry, inode->i_mode);
277 error = xfs_link(XFS_I(dir), XFS_I(inode), &name);
282 d_instantiate(dentry, inode);
289 struct dentry *dentry)
291 struct xfs_name name;
294 xfs_dentry_to_name(&name, dentry, 0);
296 error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode));
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.
305 if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb))
306 d_invalidate(dentry);
313 struct dentry *dentry,
317 struct xfs_inode *cip = NULL;
318 struct xfs_name name;
323 (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO);
324 xfs_dentry_to_name(&name, dentry, mode);
326 error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip);
332 error = xfs_init_security(inode, dir, &dentry->d_name);
334 goto out_cleanup_inode;
336 d_instantiate(dentry, inode);
340 xfs_cleanup_inode(dir, inode, dentry);
348 struct dentry *odentry,
350 struct dentry *ndentry)
352 struct inode *new_inode = ndentry->d_inode;
353 struct xfs_name oname;
354 struct xfs_name nname;
356 xfs_dentry_to_name(&oname, odentry, 0);
357 xfs_dentry_to_name(&nname, ndentry, odentry->d_inode->i_mode);
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);
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...
371 struct dentry *dentry,
372 struct nameidata *nd)
377 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
381 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
385 nd_set_link(nd, link);
391 nd_set_link(nd, ERR_PTR(error));
397 struct dentry *dentry,
398 struct nameidata *nd,
401 char *s = nd_get_link(nd);
409 struct vfsmount *mnt,
410 struct dentry *dentry,
413 struct inode *inode = dentry->d_inode;
414 struct xfs_inode *ip = XFS_I(inode);
415 struct xfs_mount *mp = ip->i_mount;
417 trace_xfs_getattr(ip);
419 if (XFS_FORCED_SHUTDOWN(mp))
420 return -XFS_ERROR(EIO);
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;
433 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
436 switch (inode->i_mode & S_IFMT) {
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));
444 if (XFS_IS_REALTIME_INODE(ip)) {
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.
451 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
453 stat->blksize = xfs_preferred_iosize(mp);
463 struct xfs_inode *ip,
466 struct inode *inode = VFS_I(ip);
467 umode_t mode = iattr->ia_mode;
469 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
471 ip->i_d.di_mode &= S_IFMT;
472 ip->i_d.di_mode |= mode & ~S_IFMT;
474 inode->i_mode &= S_IFMT;
475 inode->i_mode |= mode & ~S_IFMT;
480 struct xfs_inode *ip,
483 struct inode *inode = VFS_I(ip);
485 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
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;
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;
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;
506 struct xfs_inode *ip,
510 xfs_mount_t *mp = ip->i_mount;
511 struct inode *inode = VFS_I(ip);
512 int mask = iattr->ia_valid;
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;
520 trace_xfs_setattr(ip);
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);
527 if (XFS_FORCED_SHUTDOWN(mp))
528 return XFS_ERROR(EIO);
530 error = -inode_change_ok(inode, iattr);
532 return XFS_ERROR(error);
535 ASSERT((mask & ATTR_SIZE) == 0);
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.
545 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
548 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
550 qflags |= XFS_QMOPT_UQUOTA;
554 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
556 qflags |= XFS_QMOPT_GQUOTA;
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.
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),
571 qflags, &udqp, &gdqp, NULL);
576 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
577 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
581 xfs_ilock(ip, XFS_ILOCK_EXCL);
584 * Change file ownership. Must be the owner or privileged.
586 if (mask & (ATTR_UID|ATTR_GID)) {
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.
595 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
596 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
599 * Do a quota reservation only if uid/gid is actually
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)))) {
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;
614 xfs_trans_ijoin(tp, ip, 0);
617 * Change file ownership. Must be the owner or privileged.
619 if (mask & (ATTR_UID|ATTR_GID)) {
621 * CAP_FSETID overrides the following restrictions:
623 * The set-user-ID and set-group-ID bits of a file will be
624 * cleared upon successful return from chown()
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);
631 * Change the ownerships and register quota modifications
632 * in the transaction.
634 if (!uid_eq(iuid, uid)) {
635 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
636 ASSERT(mask & ATTR_UID);
638 olddquot1 = xfs_qm_vop_chown(tp, ip,
639 &ip->i_udquot, udqp);
641 ip->i_d.di_uid = xfs_kuid_to_uid(uid);
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);
650 olddquot2 = xfs_qm_vop_chown(tp, ip,
651 &ip->i_gdquot, gdqp);
653 ip->i_d.di_gid = xfs_kgid_to_gid(gid);
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);
663 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
665 XFS_STATS_INC(xs_ig_attrchg);
667 if (mp->m_flags & XFS_MOUNT_WSYNC)
668 xfs_trans_set_sync(tp);
669 error = xfs_trans_commit(tp, 0);
671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
674 * Release any dquot(s) the inode had kept before chown.
676 xfs_qm_dqrele(olddquot1);
677 xfs_qm_dqrele(olddquot2);
682 return XFS_ERROR(error);
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.
691 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
692 error = -xfs_acl_chmod(inode);
694 return XFS_ERROR(error);
700 xfs_trans_cancel(tp, 0);
701 xfs_iunlock(ip, XFS_ILOCK_EXCL);
709 * Truncate file. Must have write permission and not be a directory.
713 struct xfs_inode *ip,
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;
723 uint commit_flags = 0;
725 trace_xfs_setattr(ip);
727 if (mp->m_flags & XFS_MOUNT_RDONLY)
728 return XFS_ERROR(EROFS);
730 if (XFS_FORCED_SHUTDOWN(mp))
731 return XFS_ERROR(EIO);
733 error = -inode_change_ok(inode, iattr);
735 return XFS_ERROR(error);
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);
742 oldsize = inode->i_size;
743 newsize = iattr->ia_size;
746 * Short circuit the truncate case for zero length files.
748 if (newsize == 0 && oldsize == 0 && ip->i_d.di_nextents == 0) {
749 if (!(mask & (ATTR_CTIME|ATTR_MTIME)))
753 * Use the regular setattr path to update the timestamps.
755 iattr->ia_valid &= ~ATTR_SIZE;
756 return xfs_setattr_nonsize(ip, iattr, 0);
760 * Make sure that the dquots are attached to the inode.
762 error = xfs_qm_dqattach(ip, 0);
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.
773 if (newsize > oldsize) {
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
780 error = xfs_zero_eof(ip, newsize, oldsize);
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
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
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);
805 * Wait for all direct I/O to complete.
807 inode_dio_wait(inode);
809 error = -block_truncate_page(inode->i_mapping, newsize, xfs_get_blocks);
813 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
814 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
816 goto out_trans_cancel;
818 truncate_setsize(inode, newsize);
820 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
821 lock_flags |= XFS_ILOCK_EXCL;
823 xfs_ilock(ip, XFS_ILOCK_EXCL);
825 xfs_trans_ijoin(tp, ip, 0);
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.
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.
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;
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.
855 ip->i_d.di_size = newsize;
856 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
858 if (newsize <= oldsize) {
859 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, newsize);
861 goto out_trans_abort;
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.
870 xfs_iflags_set(ip, XFS_ITRUNCATED);
872 /* A truncate down always removes post-EOF blocks. */
873 xfs_inode_clear_eofblocks_tag(ip);
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);
881 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
883 XFS_STATS_INC(xs_ig_attrchg);
885 if (mp->m_flags & XFS_MOUNT_WSYNC)
886 xfs_trans_set_sync(tp);
888 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
891 xfs_iunlock(ip, lock_flags);
895 commit_flags |= XFS_TRANS_ABORT;
897 xfs_trans_cancel(tp, commit_flags);
903 struct dentry *dentry,
906 struct xfs_inode *ip = XFS_I(dentry->d_inode);
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);
914 error = xfs_setattr_nonsize(ip, iattr, 0);
923 struct timespec *now,
926 struct xfs_inode *ip = XFS_I(inode);
927 struct xfs_mount *mp = ip->i_mount;
928 struct xfs_trans *tp;
931 trace_xfs_update_time(ip);
933 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
934 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
936 xfs_trans_cancel(tp, 0);
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;
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;
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;
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);
961 #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
964 * Call fiemap helper to fill in user data.
965 * Returns positive errors to xfs_getbmap.
970 struct getbmapx *bmv,
974 struct fiemap_extent_info *fieinfo = *arg;
975 u32 fiemap_flags = 0;
976 u64 logical, physical, length;
978 /* Do nothing for a hole */
979 if (bmv->bmv_block == -1LL)
982 logical = BBTOB(bmv->bmv_offset);
983 physical = BBTOB(bmv->bmv_block);
984 length = BBTOB(bmv->bmv_length);
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 */
993 if (bmv->bmv_oflags & BMV_OF_LAST)
994 fiemap_flags |= FIEMAP_EXTENT_LAST;
996 error = fiemap_fill_next_extent(fieinfo, logical, physical,
997 length, fiemap_flags);
1000 *full = 1; /* user array now full */
1008 struct inode *inode,
1009 struct fiemap_extent_info *fieinfo,
1013 xfs_inode_t *ip = XFS_I(inode);
1017 error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS);
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;
1027 bm.bmv_length = BTOBB(length);
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;
1040 error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo);
1050 struct dentry *dentry,
1055 error = xfs_create_tmpfile(XFS_I(dir), dentry, mode);
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,
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,
1080 * Yes, XFS uses the same method for rmdir and unlink.
1082 * There are some subtile differences deeper in the code,
1083 * but we use S_ISDIR to check for those.
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,
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,
1107 * Yes, XFS uses the same method for rmdir and unlink.
1109 * There are some subtile differences deeper in the code,
1110 * but we use S_ISDIR to check for those.
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,
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,
1141 xfs_diflags_to_iflags(
1142 struct inode *inode,
1143 struct xfs_inode *ip)
1145 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
1146 inode->i_flags |= S_IMMUTABLE;
1148 inode->i_flags &= ~S_IMMUTABLE;
1149 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
1150 inode->i_flags |= S_APPEND;
1152 inode->i_flags &= ~S_APPEND;
1153 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
1154 inode->i_flags |= S_SYNC;
1156 inode->i_flags &= ~S_SYNC;
1157 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
1158 inode->i_flags |= S_NOATIME;
1160 inode->i_flags &= ~S_NOATIME;
1164 * Initialize the Linux inode, set up the operation vectors and
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.
1171 * We are always called with an uninitialised linux inode here.
1172 * We need to initialise the necessary fields and take a reference
1177 struct xfs_inode *ip)
1179 struct inode *inode = &ip->i_vnode;
1182 inode->i_ino = ip->i_ino;
1183 inode->i_state = I_NEW;
1185 inode_sb_list_add(inode);
1186 /* make the inode look hashed for the writeback code */
1187 hlist_add_fake(&inode->i_hash);
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);
1194 switch (inode->i_mode & S_IFMT) {
1198 MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
1199 sysv_minor(ip->i_df.if_u2.if_rdev));
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);
1216 ip->d_ops = ip->i_mount->m_nondir_inode_ops;
1217 switch (inode->i_mode & S_IFMT) {
1219 inode->i_op = &xfs_inode_operations;
1220 inode->i_fop = &xfs_file_operations;
1221 inode->i_mapping->a_ops = &xfs_address_space_operations;
1224 if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb))
1225 inode->i_op = &xfs_dir_ci_inode_operations;
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;
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;
1237 inode->i_op = &xfs_inode_operations;
1238 init_special_inode(inode, inode->i_mode, inode->i_rdev);
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.
1247 gfp_mask = mapping_gfp_mask(inode->i_mapping);
1248 mapping_set_gfp_mask(inode->i_mapping, (gfp_mask & ~(__GFP_FS)));
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.
1254 if (!XFS_IFORK_Q(ip)) {
1255 inode_has_no_xattr(inode);
1256 cache_no_acl(inode);
1259 xfs_iflags_clear(ip, XFS_INEW);
1262 unlock_new_inode(inode);