Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
| 19 | #include "xfs_fs.h" |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 20 | #include "xfs_acl.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_alloc.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_quota.h" |
| 29 | #include "xfs_mount.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_dinode.h" |
| 32 | #include "xfs_inode.h" |
| 33 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_rtalloc.h" |
| 35 | #include "xfs_error.h" |
| 36 | #include "xfs_itable.h" |
| 37 | #include "xfs_rw.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "xfs_attr.h" |
| 39 | #include "xfs_buf_item.h" |
| 40 | #include "xfs_utils.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 41 | #include "xfs_vnodeops.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 42 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 44 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/xattr.h> |
| 46 | #include <linux/namei.h> |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 47 | #include <linux/posix_acl.h> |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 48 | #include <linux/security.h> |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 49 | #include <linux/falloc.h> |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 50 | #include <linux/fiemap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 51 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 53 | /* |
Christoph Hellwig | f9581b1 | 2009-10-06 20:29:26 +0000 | [diff] [blame] | 54 | * Bring the timestamps in the XFS inode uptodate. |
| 55 | * |
| 56 | * Used before writing the inode to disk. |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 57 | */ |
| 58 | void |
Christoph Hellwig | f9581b1 | 2009-10-06 20:29:26 +0000 | [diff] [blame] | 59 | xfs_synchronize_times( |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 60 | xfs_inode_t *ip) |
| 61 | { |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 62 | struct inode *inode = VFS_I(ip); |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 63 | |
Christoph Hellwig | f9581b1 | 2009-10-06 20:29:26 +0000 | [diff] [blame] | 64 | ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec; |
| 65 | ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec; |
| 66 | ip->i_d.di_ctime.t_sec = (__int32_t)inode->i_ctime.tv_sec; |
| 67 | ip->i_d.di_ctime.t_nsec = (__int32_t)inode->i_ctime.tv_nsec; |
| 68 | ip->i_d.di_mtime.t_sec = (__int32_t)inode->i_mtime.tv_sec; |
| 69 | ip->i_d.di_mtime.t_nsec = (__int32_t)inode->i_mtime.tv_nsec; |
Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 73 | * If the linux inode is valid, mark it dirty. |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 74 | * Used when commiting a dirty inode into a transaction so that |
| 75 | * the inode will get written back by the linux code |
| 76 | */ |
| 77 | void |
| 78 | xfs_mark_inode_dirty_sync( |
| 79 | xfs_inode_t *ip) |
| 80 | { |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 81 | struct inode *inode = VFS_I(ip); |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 82 | |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 83 | if (!(inode->i_state & (I_WILL_FREE|I_FREEING))) |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 84 | mark_inode_dirty_sync(inode); |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 85 | } |
| 86 | |
Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 87 | void |
| 88 | xfs_mark_inode_dirty( |
| 89 | xfs_inode_t *ip) |
| 90 | { |
| 91 | struct inode *inode = VFS_I(ip); |
| 92 | |
Al Viro | a4ffdde | 2010-06-02 17:38:30 -0400 | [diff] [blame] | 93 | if (!(inode->i_state & (I_WILL_FREE|I_FREEING))) |
Christoph Hellwig | 66d834e | 2010-02-15 09:44:49 +0000 | [diff] [blame] | 94 | mark_inode_dirty(inode); |
| 95 | } |
| 96 | |
David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame] | 97 | /* |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 98 | * Hook in SELinux. This is not quite correct yet, what we really need |
| 99 | * here (as we do for default ACLs) is a mechanism by which creation of |
| 100 | * these attrs can be journalled at inode creation time (along with the |
| 101 | * inode, of course, such that log replay can't cause these to be lost). |
| 102 | */ |
| 103 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 104 | xfs_init_security( |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 105 | struct inode *inode, |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 106 | struct inode *dir) |
| 107 | { |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 108 | struct xfs_inode *ip = XFS_I(inode); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 109 | size_t length; |
| 110 | void *value; |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 111 | unsigned char *name; |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 112 | int error; |
| 113 | |
Dave Chinner | a9273ca | 2010-01-20 10:47:48 +1100 | [diff] [blame] | 114 | error = security_inode_init_security(inode, dir, (char **)&name, |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 115 | &value, &length); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 116 | if (error) { |
| 117 | if (error == -EOPNOTSUPP) |
| 118 | return 0; |
| 119 | return -error; |
| 120 | } |
| 121 | |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 122 | error = xfs_attr_set(ip, name, value, length, ATTR_SECURE); |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 123 | |
| 124 | kfree(name); |
| 125 | kfree(value); |
| 126 | return error; |
| 127 | } |
| 128 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 129 | static void |
| 130 | xfs_dentry_to_name( |
| 131 | struct xfs_name *namep, |
| 132 | struct dentry *dentry) |
| 133 | { |
| 134 | namep->name = dentry->d_name.name; |
| 135 | namep->len = dentry->d_name.len; |
| 136 | } |
| 137 | |
David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 138 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 139 | xfs_cleanup_inode( |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 140 | struct inode *dir, |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 141 | struct inode *inode, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 142 | struct dentry *dentry) |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 143 | { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 144 | struct xfs_name teardown; |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 145 | |
| 146 | /* Oh, the horror. |
Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 147 | * If we can't add the ACL or we fail in |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 148 | * xfs_init_security we must back out. |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 149 | * ENOSPC can hit here, among other things. |
| 150 | */ |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 151 | xfs_dentry_to_name(&teardown, dentry); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 152 | |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 153 | xfs_remove(XFS_I(dir), &teardown, XFS_I(inode)); |
Christoph Hellwig | af04819 | 2008-03-06 13:46:43 +1100 | [diff] [blame] | 154 | iput(inode); |
Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 158 | xfs_vn_mknod( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | struct inode *dir, |
| 160 | struct dentry *dentry, |
| 161 | int mode, |
| 162 | dev_t rdev) |
| 163 | { |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 164 | struct inode *inode; |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 165 | struct xfs_inode *ip = NULL; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 166 | struct posix_acl *default_acl = NULL; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 167 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | int error; |
| 169 | |
| 170 | /* |
| 171 | * Irix uses Missed'em'V split, but doesn't want to see |
| 172 | * the upper 5 bits of (14bit) major. |
| 173 | */ |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 174 | if (S_ISCHR(mode) || S_ISBLK(mode)) { |
| 175 | if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) |
| 176 | return -EINVAL; |
| 177 | rdev = sysv_encode_dev(rdev); |
| 178 | } else { |
| 179 | rdev = 0; |
| 180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 182 | if (IS_POSIXACL(dir)) { |
| 183 | default_acl = xfs_get_acl(dir, ACL_TYPE_DEFAULT); |
| 184 | if (IS_ERR(default_acl)) |
| 185 | return -PTR_ERR(default_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Christoph Hellwig | e83f1eb | 2009-06-12 11:19:11 -0400 | [diff] [blame] | 187 | if (!default_acl) |
| 188 | mode &= ~current_umask(); |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Christoph Hellwig | 517b5e8 | 2009-02-09 08:38:02 +0100 | [diff] [blame] | 191 | xfs_dentry_to_name(&name, dentry); |
Christoph Hellwig | 6c77b0e | 2010-10-06 18:41:17 +0000 | [diff] [blame] | 192 | error = xfs_create(XFS_I(dir), &name, mode, rdev, &ip); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 193 | if (unlikely(error)) |
| 194 | goto out_free_acl; |
Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 195 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 196 | inode = VFS_I(ip); |
Christoph Hellwig | 979ebab | 2008-03-06 13:46:05 +1100 | [diff] [blame] | 197 | |
| 198 | error = xfs_init_security(inode, dir); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 199 | if (unlikely(error)) |
| 200 | goto out_cleanup_inode; |
| 201 | |
| 202 | if (default_acl) { |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 203 | error = -xfs_inherit_acl(inode, default_acl); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 204 | if (unlikely(error)) |
| 205 | goto out_cleanup_inode; |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 206 | posix_acl_release(default_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 210 | d_instantiate(dentry, inode); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 211 | return -error; |
| 212 | |
| 213 | out_cleanup_inode: |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 214 | xfs_cleanup_inode(dir, inode, dentry); |
Christoph Hellwig | db0bb7b | 2008-03-06 13:44:35 +1100 | [diff] [blame] | 215 | out_free_acl: |
Christoph Hellwig | ef14f0c | 2009-06-10 17:07:47 +0200 | [diff] [blame] | 216 | posix_acl_release(default_acl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return -error; |
| 218 | } |
| 219 | |
| 220 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 221 | xfs_vn_create( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | struct inode *dir, |
| 223 | struct dentry *dentry, |
| 224 | int mode, |
| 225 | struct nameidata *nd) |
| 226 | { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 227 | return xfs_vn_mknod(dir, dentry, mode, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 231 | xfs_vn_mkdir( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | struct inode *dir, |
| 233 | struct dentry *dentry, |
| 234 | int mode) |
| 235 | { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 236 | return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | STATIC struct dentry * |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 240 | xfs_vn_lookup( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | struct inode *dir, |
| 242 | struct dentry *dentry, |
| 243 | struct nameidata *nd) |
| 244 | { |
Christoph Hellwig | ef1f5e7 | 2008-03-06 13:46:25 +1100 | [diff] [blame] | 245 | struct xfs_inode *cip; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 246 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | int error; |
| 248 | |
| 249 | if (dentry->d_name.len >= MAXNAMELEN) |
| 250 | return ERR_PTR(-ENAMETOOLONG); |
| 251 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 252 | xfs_dentry_to_name(&name, dentry); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 253 | error = xfs_lookup(XFS_I(dir), &name, &cip, NULL); |
Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 254 | if (unlikely(error)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | if (unlikely(error != ENOENT)) |
| 256 | return ERR_PTR(-error); |
| 257 | d_add(dentry, NULL); |
| 258 | return NULL; |
| 259 | } |
| 260 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 261 | return d_splice_alias(VFS_I(cip), dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 264 | STATIC struct dentry * |
| 265 | xfs_vn_ci_lookup( |
| 266 | struct inode *dir, |
| 267 | struct dentry *dentry, |
| 268 | struct nameidata *nd) |
| 269 | { |
| 270 | struct xfs_inode *ip; |
| 271 | struct xfs_name xname; |
| 272 | struct xfs_name ci_name; |
| 273 | struct qstr dname; |
| 274 | int error; |
| 275 | |
| 276 | if (dentry->d_name.len >= MAXNAMELEN) |
| 277 | return ERR_PTR(-ENAMETOOLONG); |
| 278 | |
| 279 | xfs_dentry_to_name(&xname, dentry); |
| 280 | error = xfs_lookup(XFS_I(dir), &xname, &ip, &ci_name); |
| 281 | if (unlikely(error)) { |
| 282 | if (unlikely(error != ENOENT)) |
| 283 | return ERR_PTR(-error); |
Barry Naujok | 866d5dc | 2008-05-22 17:21:40 +1000 | [diff] [blame] | 284 | /* |
| 285 | * call d_add(dentry, NULL) here when d_drop_negative_children |
| 286 | * is called in xfs_vn_mknod (ie. allow negative dentries |
| 287 | * with CI filesystems). |
| 288 | */ |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | /* if exact match, just splice and exit */ |
| 293 | if (!ci_name.name) |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 294 | return d_splice_alias(VFS_I(ip), dentry); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 295 | |
| 296 | /* else case-insensitive match... */ |
| 297 | dname.name = ci_name.name; |
| 298 | dname.len = ci_name.len; |
Christoph Hellwig | e45b590 | 2008-08-07 23:49:07 +0200 | [diff] [blame] | 299 | dentry = d_add_ci(dentry, VFS_I(ip), &dname); |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 300 | kmem_free(ci_name.name); |
| 301 | return dentry; |
| 302 | } |
| 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 305 | xfs_vn_link( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | struct dentry *old_dentry, |
| 307 | struct inode *dir, |
| 308 | struct dentry *dentry) |
| 309 | { |
Christoph Hellwig | d9424b3 | 2008-12-03 12:20:27 +0100 | [diff] [blame] | 310 | struct inode *inode = old_dentry->d_inode; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 311 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | int error; |
| 313 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 314 | xfs_dentry_to_name(&name, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 316 | error = xfs_link(XFS_I(dir), XFS_I(inode), &name); |
Christoph Hellwig | d9424b3 | 2008-12-03 12:20:27 +0100 | [diff] [blame] | 317 | if (unlikely(error)) |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 318 | return -error; |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 319 | |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 320 | ihold(inode); |
Christoph Hellwig | a3da789 | 2008-03-06 13:46:12 +1100 | [diff] [blame] | 321 | d_instantiate(dentry, inode); |
| 322 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 326 | xfs_vn_unlink( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | struct inode *dir, |
| 328 | struct dentry *dentry) |
| 329 | { |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 330 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | int error; |
| 332 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 333 | xfs_dentry_to_name(&name, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Christoph Hellwig | e570070 | 2008-06-23 13:25:25 +1000 | [diff] [blame] | 335 | error = -xfs_remove(XFS_I(dir), &name, XFS_I(dentry->d_inode)); |
| 336 | if (error) |
| 337 | return error; |
| 338 | |
| 339 | /* |
| 340 | * With unlink, the VFS makes the dentry "negative": no inode, |
| 341 | * but still hashed. This is incompatible with case-insensitive |
| 342 | * mode, so invalidate (unhash) the dentry in CI-mode. |
| 343 | */ |
| 344 | if (xfs_sb_version_hasasciici(&XFS_M(dir->i_sb)->m_sb)) |
| 345 | d_invalidate(dentry); |
| 346 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 350 | xfs_vn_symlink( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | struct inode *dir, |
| 352 | struct dentry *dentry, |
| 353 | const char *symname) |
| 354 | { |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 355 | struct inode *inode; |
| 356 | struct xfs_inode *cip = NULL; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 357 | struct xfs_name name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | int error; |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 359 | mode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | |
Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 361 | mode = S_IFLNK | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 362 | (irix_symlink_mode ? 0777 & ~current_umask() : S_IRWXUGO); |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 363 | xfs_dentry_to_name(&name, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Christoph Hellwig | 6c77b0e | 2010-10-06 18:41:17 +0000 | [diff] [blame] | 365 | error = xfs_symlink(XFS_I(dir), &name, symname, mode, &cip); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 366 | if (unlikely(error)) |
| 367 | goto out; |
| 368 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 369 | inode = VFS_I(cip); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 370 | |
| 371 | error = xfs_init_security(inode, dir); |
| 372 | if (unlikely(error)) |
| 373 | goto out_cleanup_inode; |
| 374 | |
| 375 | d_instantiate(dentry, inode); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 376 | return 0; |
| 377 | |
| 378 | out_cleanup_inode: |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 379 | xfs_cleanup_inode(dir, inode, dentry); |
Christoph Hellwig | 3937be5 | 2008-03-06 13:46:19 +1100 | [diff] [blame] | 380 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | return -error; |
| 382 | } |
| 383 | |
| 384 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 385 | xfs_vn_rename( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | struct inode *odir, |
| 387 | struct dentry *odentry, |
| 388 | struct inode *ndir, |
| 389 | struct dentry *ndentry) |
| 390 | { |
| 391 | struct inode *new_inode = ndentry->d_inode; |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 392 | struct xfs_name oname; |
| 393 | struct xfs_name nname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Barry Naujok | 556b8b1 | 2008-04-10 12:22:07 +1000 | [diff] [blame] | 395 | xfs_dentry_to_name(&oname, odentry); |
| 396 | xfs_dentry_to_name(&nname, ndentry); |
| 397 | |
Christoph Hellwig | e570070 | 2008-06-23 13:25:25 +1000 | [diff] [blame] | 398 | return -xfs_rename(XFS_I(odir), &oname, XFS_I(odentry->d_inode), |
Christoph Hellwig | cfa853e | 2008-04-22 17:34:06 +1000 | [diff] [blame] | 399 | XFS_I(ndir), &nname, new_inode ? |
| 400 | XFS_I(new_inode) : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* |
| 404 | * careful here - this function can get called recursively, so |
| 405 | * we need to be very careful about how much stack we use. |
| 406 | * uio is kmalloced for this reason... |
| 407 | */ |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 408 | STATIC void * |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 409 | xfs_vn_follow_link( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | struct dentry *dentry, |
| 411 | struct nameidata *nd) |
| 412 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | char *link; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 414 | int error = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Panagiotis Issaris | f52720c | 2006-09-27 01:49:39 -0700 | [diff] [blame] | 416 | link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 417 | if (!link) |
| 418 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 420 | error = -xfs_readlink(XFS_I(dentry->d_inode), link); |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 421 | if (unlikely(error)) |
| 422 | goto out_kfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | nd_set_link(nd, link); |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 425 | return NULL; |
Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 426 | |
| 427 | out_kfree: |
| 428 | kfree(link); |
| 429 | out_err: |
| 430 | nd_set_link(nd, ERR_PTR(error)); |
| 431 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 434 | STATIC void |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 435 | xfs_vn_put_link( |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 436 | struct dentry *dentry, |
| 437 | struct nameidata *nd, |
| 438 | void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 440 | char *s = nd_get_link(nd); |
| 441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (!IS_ERR(s)) |
| 443 | kfree(s); |
| 444 | } |
| 445 | |
Christoph Hellwig | 4576758 | 2008-02-05 12:13:24 +1100 | [diff] [blame] | 446 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 447 | xfs_vn_getattr( |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 448 | struct vfsmount *mnt, |
| 449 | struct dentry *dentry, |
| 450 | struct kstat *stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 452 | struct inode *inode = dentry->d_inode; |
| 453 | struct xfs_inode *ip = XFS_I(inode); |
| 454 | struct xfs_mount *mp = ip->i_mount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Christoph Hellwig | cca28fb | 2010-06-24 11:57:09 +1000 | [diff] [blame] | 456 | trace_xfs_getattr(ip); |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 457 | |
| 458 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 459 | return XFS_ERROR(EIO); |
| 460 | |
| 461 | stat->size = XFS_ISIZE(ip); |
| 462 | stat->dev = inode->i_sb->s_dev; |
| 463 | stat->mode = ip->i_d.di_mode; |
| 464 | stat->nlink = ip->i_d.di_nlink; |
| 465 | stat->uid = ip->i_d.di_uid; |
| 466 | stat->gid = ip->i_d.di_gid; |
| 467 | stat->ino = ip->i_ino; |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 468 | stat->atime = inode->i_atime; |
Christoph Hellwig | f9581b1 | 2009-10-06 20:29:26 +0000 | [diff] [blame] | 469 | stat->mtime = inode->i_mtime; |
| 470 | stat->ctime = inode->i_ctime; |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 471 | stat->blocks = |
| 472 | XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); |
| 473 | |
| 474 | |
| 475 | switch (inode->i_mode & S_IFMT) { |
| 476 | case S_IFBLK: |
| 477 | case S_IFCHR: |
| 478 | stat->blksize = BLKDEV_IOSIZE; |
| 479 | stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, |
| 480 | sysv_minor(ip->i_df.if_u2.if_rdev)); |
| 481 | break; |
| 482 | default: |
Eric Sandeen | 71ddabb | 2007-11-23 16:29:42 +1100 | [diff] [blame] | 483 | if (XFS_IS_REALTIME_INODE(ip)) { |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 484 | /* |
| 485 | * If the file blocks are being allocated from a |
| 486 | * realtime volume, then return the inode's realtime |
| 487 | * extent size or the realtime volume's extent size. |
| 488 | */ |
| 489 | stat->blksize = |
| 490 | xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog; |
| 491 | } else |
| 492 | stat->blksize = xfs_preferred_iosize(mp); |
| 493 | stat->rdev = 0; |
| 494 | break; |
Nathan Scott | 69e23b9 | 2006-09-28 11:01:22 +1000 | [diff] [blame] | 495 | } |
Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 496 | |
| 497 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | STATIC int |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 501 | xfs_vn_setattr( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | struct dentry *dentry, |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 503 | struct iattr *iattr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
Christoph Hellwig | ea5a3dc8 | 2008-10-30 18:27:48 +1100 | [diff] [blame] | 505 | return -xfs_setattr(XFS_I(dentry->d_inode), iattr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |
| 507 | |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 508 | STATIC long |
| 509 | xfs_vn_fallocate( |
| 510 | struct inode *inode, |
| 511 | int mode, |
| 512 | loff_t offset, |
| 513 | loff_t len) |
| 514 | { |
| 515 | long error; |
| 516 | loff_t new_size = 0; |
| 517 | xfs_flock64_t bf; |
| 518 | xfs_inode_t *ip = XFS_I(inode); |
| 519 | |
| 520 | /* preallocation on directories not yet supported */ |
| 521 | error = -ENODEV; |
| 522 | if (S_ISDIR(inode->i_mode)) |
| 523 | goto out_error; |
| 524 | |
| 525 | bf.l_whence = 0; |
| 526 | bf.l_start = offset; |
| 527 | bf.l_len = len; |
| 528 | |
| 529 | xfs_ilock(ip, XFS_IOLOCK_EXCL); |
Dave Chinner | 07f1a4f | 2010-05-21 05:47:59 +0000 | [diff] [blame] | 530 | |
| 531 | /* check the new inode size is valid before allocating */ |
| 532 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 533 | offset + len > i_size_read(inode)) { |
| 534 | new_size = offset + len; |
| 535 | error = inode_newsize_ok(inode, new_size); |
| 536 | if (error) |
| 537 | goto out_unlock; |
| 538 | } |
| 539 | |
Jason Gunthorpe | 44a743f | 2009-11-24 21:52:53 +0000 | [diff] [blame] | 540 | error = -xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf, |
| 541 | 0, XFS_ATTR_NOLOCK); |
Dave Chinner | 07f1a4f | 2010-05-21 05:47:59 +0000 | [diff] [blame] | 542 | if (error) |
| 543 | goto out_unlock; |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 544 | |
| 545 | /* Change file size if needed */ |
| 546 | if (new_size) { |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 547 | struct iattr iattr; |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 548 | |
Christoph Hellwig | 0f285c8 | 2008-07-18 17:13:28 +1000 | [diff] [blame] | 549 | iattr.ia_valid = ATTR_SIZE; |
| 550 | iattr.ia_size = new_size; |
Jason Gunthorpe | 44a743f | 2009-11-24 21:52:53 +0000 | [diff] [blame] | 551 | error = -xfs_setattr(ip, &iattr, XFS_ATTR_NOLOCK); |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 552 | } |
| 553 | |
Dave Chinner | 07f1a4f | 2010-05-21 05:47:59 +0000 | [diff] [blame] | 554 | out_unlock: |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 555 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); |
| 556 | out_error: |
| 557 | return error; |
| 558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 560 | #define XFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) |
| 561 | |
| 562 | /* |
| 563 | * Call fiemap helper to fill in user data. |
| 564 | * Returns positive errors to xfs_getbmap. |
| 565 | */ |
| 566 | STATIC int |
| 567 | xfs_fiemap_format( |
| 568 | void **arg, |
| 569 | struct getbmapx *bmv, |
| 570 | int *full) |
| 571 | { |
| 572 | int error; |
| 573 | struct fiemap_extent_info *fieinfo = *arg; |
| 574 | u32 fiemap_flags = 0; |
| 575 | u64 logical, physical, length; |
| 576 | |
| 577 | /* Do nothing for a hole */ |
| 578 | if (bmv->bmv_block == -1LL) |
| 579 | return 0; |
| 580 | |
| 581 | logical = BBTOB(bmv->bmv_offset); |
| 582 | physical = BBTOB(bmv->bmv_block); |
| 583 | length = BBTOB(bmv->bmv_length); |
| 584 | |
| 585 | if (bmv->bmv_oflags & BMV_OF_PREALLOC) |
| 586 | fiemap_flags |= FIEMAP_EXTENT_UNWRITTEN; |
| 587 | else if (bmv->bmv_oflags & BMV_OF_DELALLOC) { |
| 588 | fiemap_flags |= FIEMAP_EXTENT_DELALLOC; |
| 589 | physical = 0; /* no block yet */ |
| 590 | } |
| 591 | if (bmv->bmv_oflags & BMV_OF_LAST) |
| 592 | fiemap_flags |= FIEMAP_EXTENT_LAST; |
| 593 | |
| 594 | error = fiemap_fill_next_extent(fieinfo, logical, physical, |
| 595 | length, fiemap_flags); |
| 596 | if (error > 0) { |
| 597 | error = 0; |
| 598 | *full = 1; /* user array now full */ |
| 599 | } |
| 600 | |
| 601 | return -error; |
| 602 | } |
| 603 | |
| 604 | STATIC int |
| 605 | xfs_vn_fiemap( |
| 606 | struct inode *inode, |
| 607 | struct fiemap_extent_info *fieinfo, |
| 608 | u64 start, |
| 609 | u64 length) |
| 610 | { |
| 611 | xfs_inode_t *ip = XFS_I(inode); |
| 612 | struct getbmapx bm; |
| 613 | int error; |
| 614 | |
| 615 | error = fiemap_check_flags(fieinfo, XFS_FIEMAP_FLAGS); |
| 616 | if (error) |
| 617 | return error; |
| 618 | |
| 619 | /* Set up bmap header for xfs internal routine */ |
| 620 | bm.bmv_offset = BTOBB(start); |
| 621 | /* Special case for whole file */ |
| 622 | if (length == FIEMAP_MAX_OFFSET) |
| 623 | bm.bmv_length = -1LL; |
| 624 | else |
| 625 | bm.bmv_length = BTOBB(length); |
| 626 | |
Eric Sandeen | 97db39a | 2009-07-26 21:52:01 -0500 | [diff] [blame] | 627 | /* We add one because in getbmap world count includes the header */ |
Tao Ma | 2d1ff3c | 2010-04-29 15:13:56 +1000 | [diff] [blame] | 628 | bm.bmv_count = !fieinfo->fi_extents_max ? MAXEXTNUM : |
| 629 | fieinfo->fi_extents_max + 1; |
| 630 | bm.bmv_count = min_t(__s32, bm.bmv_count, |
| 631 | (PAGE_SIZE * 16 / sizeof(struct getbmapx))); |
Tao Ma | 9af2546 | 2010-08-30 02:44:03 +0000 | [diff] [blame] | 632 | bm.bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES; |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 633 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) |
| 634 | bm.bmv_iflags |= BMV_IF_ATTRFORK; |
| 635 | if (!(fieinfo->fi_flags & FIEMAP_FLAG_SYNC)) |
| 636 | bm.bmv_iflags |= BMV_IF_DELALLOC; |
| 637 | |
| 638 | error = xfs_getbmap(ip, &bm, xfs_fiemap_format, fieinfo); |
| 639 | if (error) |
| 640 | return -error; |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 645 | static const struct inode_operations xfs_inode_operations = { |
Linus Torvalds | 18f4c64 | 2009-08-28 12:29:03 -0700 | [diff] [blame] | 646 | .check_acl = xfs_check_acl, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 647 | .getattr = xfs_vn_getattr, |
| 648 | .setattr = xfs_vn_setattr, |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 649 | .setxattr = generic_setxattr, |
| 650 | .getxattr = generic_getxattr, |
| 651 | .removexattr = generic_removexattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 652 | .listxattr = xfs_vn_listxattr, |
David Chinner | 3ed6526 | 2007-11-23 16:29:25 +1100 | [diff] [blame] | 653 | .fallocate = xfs_vn_fallocate, |
Eric Sandeen | f35642e | 2008-11-28 14:23:35 +1100 | [diff] [blame] | 654 | .fiemap = xfs_vn_fiemap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | }; |
| 656 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 657 | static const struct inode_operations xfs_dir_inode_operations = { |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 658 | .create = xfs_vn_create, |
| 659 | .lookup = xfs_vn_lookup, |
| 660 | .link = xfs_vn_link, |
| 661 | .unlink = xfs_vn_unlink, |
| 662 | .symlink = xfs_vn_symlink, |
| 663 | .mkdir = xfs_vn_mkdir, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 664 | /* |
| 665 | * Yes, XFS uses the same method for rmdir and unlink. |
| 666 | * |
| 667 | * There are some subtile differences deeper in the code, |
| 668 | * but we use S_ISDIR to check for those. |
| 669 | */ |
| 670 | .rmdir = xfs_vn_unlink, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 671 | .mknod = xfs_vn_mknod, |
| 672 | .rename = xfs_vn_rename, |
Linus Torvalds | 18f4c64 | 2009-08-28 12:29:03 -0700 | [diff] [blame] | 673 | .check_acl = xfs_check_acl, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 674 | .getattr = xfs_vn_getattr, |
| 675 | .setattr = xfs_vn_setattr, |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 676 | .setxattr = generic_setxattr, |
| 677 | .getxattr = generic_getxattr, |
| 678 | .removexattr = generic_removexattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 679 | .listxattr = xfs_vn_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | }; |
| 681 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 682 | static const struct inode_operations xfs_dir_ci_inode_operations = { |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 683 | .create = xfs_vn_create, |
| 684 | .lookup = xfs_vn_ci_lookup, |
| 685 | .link = xfs_vn_link, |
| 686 | .unlink = xfs_vn_unlink, |
| 687 | .symlink = xfs_vn_symlink, |
| 688 | .mkdir = xfs_vn_mkdir, |
Christoph Hellwig | 8f112e3 | 2008-06-23 13:25:17 +1000 | [diff] [blame] | 689 | /* |
| 690 | * Yes, XFS uses the same method for rmdir and unlink. |
| 691 | * |
| 692 | * There are some subtile differences deeper in the code, |
| 693 | * but we use S_ISDIR to check for those. |
| 694 | */ |
| 695 | .rmdir = xfs_vn_unlink, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 696 | .mknod = xfs_vn_mknod, |
| 697 | .rename = xfs_vn_rename, |
Linus Torvalds | 18f4c64 | 2009-08-28 12:29:03 -0700 | [diff] [blame] | 698 | .check_acl = xfs_check_acl, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 699 | .getattr = xfs_vn_getattr, |
| 700 | .setattr = xfs_vn_setattr, |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 701 | .setxattr = generic_setxattr, |
| 702 | .getxattr = generic_getxattr, |
| 703 | .removexattr = generic_removexattr, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 704 | .listxattr = xfs_vn_listxattr, |
Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 705 | }; |
| 706 | |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 707 | static const struct inode_operations xfs_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | .readlink = generic_readlink, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 709 | .follow_link = xfs_vn_follow_link, |
| 710 | .put_link = xfs_vn_put_link, |
Linus Torvalds | 18f4c64 | 2009-08-28 12:29:03 -0700 | [diff] [blame] | 711 | .check_acl = xfs_check_acl, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 712 | .getattr = xfs_vn_getattr, |
| 713 | .setattr = xfs_vn_setattr, |
Lachlan McIlroy | 0ec5851 | 2008-06-23 13:23:01 +1000 | [diff] [blame] | 714 | .setxattr = generic_setxattr, |
| 715 | .getxattr = generic_getxattr, |
| 716 | .removexattr = generic_removexattr, |
Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 717 | .listxattr = xfs_vn_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | }; |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 719 | |
| 720 | STATIC void |
| 721 | xfs_diflags_to_iflags( |
| 722 | struct inode *inode, |
| 723 | struct xfs_inode *ip) |
| 724 | { |
| 725 | if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE) |
| 726 | inode->i_flags |= S_IMMUTABLE; |
| 727 | else |
| 728 | inode->i_flags &= ~S_IMMUTABLE; |
| 729 | if (ip->i_d.di_flags & XFS_DIFLAG_APPEND) |
| 730 | inode->i_flags |= S_APPEND; |
| 731 | else |
| 732 | inode->i_flags &= ~S_APPEND; |
| 733 | if (ip->i_d.di_flags & XFS_DIFLAG_SYNC) |
| 734 | inode->i_flags |= S_SYNC; |
| 735 | else |
| 736 | inode->i_flags &= ~S_SYNC; |
| 737 | if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME) |
| 738 | inode->i_flags |= S_NOATIME; |
| 739 | else |
| 740 | inode->i_flags &= ~S_NOATIME; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * Initialize the Linux inode, set up the operation vectors and |
| 745 | * unlock the inode. |
| 746 | * |
| 747 | * When reading existing inodes from disk this is called directly |
| 748 | * from xfs_iget, when creating a new inode it is called from |
| 749 | * xfs_ialloc after setting up the inode. |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 750 | * |
| 751 | * We are always called with an uninitialised linux inode here. |
| 752 | * We need to initialise the necessary fields and take a reference |
| 753 | * on it. |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 754 | */ |
| 755 | void |
| 756 | xfs_setup_inode( |
| 757 | struct xfs_inode *ip) |
| 758 | { |
David Chinner | bf90424 | 2008-10-30 17:36:14 +1100 | [diff] [blame] | 759 | struct inode *inode = &ip->i_vnode; |
| 760 | |
| 761 | inode->i_ino = ip->i_ino; |
Christoph Hellwig | eaff807 | 2009-12-17 14:25:01 +0100 | [diff] [blame] | 762 | inode->i_state = I_NEW; |
Christoph Hellwig | 646ec46 | 2010-10-23 07:15:32 -0400 | [diff] [blame] | 763 | |
| 764 | inode_sb_list_add(inode); |
Christoph Hellwig | c6f6cd0 | 2010-11-06 11:43:08 +0000 | [diff] [blame^] | 765 | /* make the inode look hashed for the writeback code */ |
| 766 | hlist_add_fake(&inode->i_hash); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 767 | |
| 768 | inode->i_mode = ip->i_d.di_mode; |
| 769 | inode->i_nlink = ip->i_d.di_nlink; |
| 770 | inode->i_uid = ip->i_d.di_uid; |
| 771 | inode->i_gid = ip->i_d.di_gid; |
| 772 | |
| 773 | switch (inode->i_mode & S_IFMT) { |
| 774 | case S_IFBLK: |
| 775 | case S_IFCHR: |
| 776 | inode->i_rdev = |
| 777 | MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, |
| 778 | sysv_minor(ip->i_df.if_u2.if_rdev)); |
| 779 | break; |
| 780 | default: |
| 781 | inode->i_rdev = 0; |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | inode->i_generation = ip->i_d.di_gen; |
| 786 | i_size_write(inode, ip->i_d.di_size); |
| 787 | inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec; |
| 788 | inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec; |
| 789 | inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec; |
| 790 | inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; |
| 791 | inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec; |
| 792 | inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; |
| 793 | xfs_diflags_to_iflags(inode, ip); |
Christoph Hellwig | 41be8be | 2008-08-13 16:23:13 +1000 | [diff] [blame] | 794 | |
| 795 | switch (inode->i_mode & S_IFMT) { |
| 796 | case S_IFREG: |
| 797 | inode->i_op = &xfs_inode_operations; |
| 798 | inode->i_fop = &xfs_file_operations; |
| 799 | inode->i_mapping->a_ops = &xfs_address_space_operations; |
| 800 | break; |
| 801 | case S_IFDIR: |
| 802 | if (xfs_sb_version_hasasciici(&XFS_M(inode->i_sb)->m_sb)) |
| 803 | inode->i_op = &xfs_dir_ci_inode_operations; |
| 804 | else |
| 805 | inode->i_op = &xfs_dir_inode_operations; |
| 806 | inode->i_fop = &xfs_dir_file_operations; |
| 807 | break; |
| 808 | case S_IFLNK: |
| 809 | inode->i_op = &xfs_symlink_inode_operations; |
| 810 | if (!(ip->i_df.if_flags & XFS_IFINLINE)) |
| 811 | inode->i_mapping->a_ops = &xfs_address_space_operations; |
| 812 | break; |
| 813 | default: |
| 814 | inode->i_op = &xfs_inode_operations; |
| 815 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 816 | break; |
| 817 | } |
| 818 | |
| 819 | xfs_iflags_clear(ip, XFS_INEW); |
| 820 | barrier(); |
| 821 | |
| 822 | unlock_new_inode(inode); |
| 823 | } |