| 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" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_bit.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_inum.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_trans.h" | 
 | 24 | #include "xfs_sb.h" | 
 | 25 | #include "xfs_ag.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_dir2.h" | 
 | 27 | #include "xfs_alloc.h" | 
 | 28 | #include "xfs_dmapi.h" | 
 | 29 | #include "xfs_quota.h" | 
 | 30 | #include "xfs_mount.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_dir2_sf.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 35 | #include "xfs_attr_sf.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_dinode.h" | 
 | 37 | #include "xfs_inode.h" | 
 | 38 | #include "xfs_bmap.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 39 | #include "xfs_btree.h" | 
 | 40 | #include "xfs_ialloc.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "xfs_rtalloc.h" | 
 | 42 | #include "xfs_error.h" | 
 | 43 | #include "xfs_itable.h" | 
 | 44 | #include "xfs_rw.h" | 
 | 45 | #include "xfs_acl.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include "xfs_attr.h" | 
 | 47 | #include "xfs_buf_item.h" | 
 | 48 | #include "xfs_utils.h" | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 49 | #include "xfs_vnodeops.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 |  | 
| Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 51 | #include <linux/capability.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/xattr.h> | 
 | 53 | #include <linux/namei.h> | 
| Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 54 | #include <linux/security.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 |  | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 56 | /* | 
| Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 57 |  * Bring the atime in the XFS inode uptodate. | 
 | 58 |  * Used before logging the inode to disk or when the Linux inode goes away. | 
 | 59 |  */ | 
 | 60 | void | 
 | 61 | xfs_synchronize_atime( | 
 | 62 | 	xfs_inode_t	*ip) | 
 | 63 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 64 | 	bhv_vnode_t	*vp; | 
| Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 65 |  | 
 | 66 | 	vp = XFS_ITOV_NULL(ip); | 
 | 67 | 	if (vp) { | 
| Christoph Hellwig | 0a74cd1 | 2007-08-29 11:53:12 +1000 | [diff] [blame] | 68 | 		ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec; | 
 | 69 | 		ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec; | 
| Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 70 | 	} | 
 | 71 | } | 
 | 72 |  | 
 | 73 | /* | 
| David Chinner | 5d51eff | 2007-11-23 16:29:18 +1100 | [diff] [blame^] | 74 |  * If the linux inode exists, mark it dirty. | 
 | 75 |  * Used when commiting a dirty inode into a transaction so that | 
 | 76 |  * the inode will get written back by the linux code | 
 | 77 |  */ | 
 | 78 | void | 
 | 79 | xfs_mark_inode_dirty_sync( | 
 | 80 | 	xfs_inode_t	*ip) | 
 | 81 | { | 
 | 82 | 	bhv_vnode_t	*vp; | 
 | 83 |  | 
 | 84 | 	vp = XFS_ITOV_NULL(ip); | 
 | 85 | 	if (vp) | 
 | 86 | 		mark_inode_dirty_sync(vn_to_inode(vp)); | 
 | 87 | } | 
 | 88 |  | 
 | 89 | /* | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 90 |  * Change the requested timestamp in the given inode. | 
 | 91 |  * We don't lock across timestamp updates, and we don't log them but | 
 | 92 |  * we do record the fact that there is dirty information in core. | 
 | 93 |  * | 
 | 94 |  * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG | 
 | 95 |  *		with XFS_ICHGTIME_ACC to be sure that access time | 
 | 96 |  *		update will take.  Calling first with XFS_ICHGTIME_ACC | 
 | 97 |  *		and then XFS_ICHGTIME_MOD may fail to modify the access | 
 | 98 |  *		timestamp if the filesystem is mounted noacctm. | 
 | 99 |  */ | 
 | 100 | void | 
 | 101 | xfs_ichgtime( | 
 | 102 | 	xfs_inode_t	*ip, | 
 | 103 | 	int		flags) | 
 | 104 | { | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 105 | 	struct inode	*inode = vn_to_inode(XFS_ITOV(ip)); | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 106 | 	timespec_t	tv; | 
 | 107 |  | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 108 | 	nanotime(&tv); | 
 | 109 | 	if (flags & XFS_ICHGTIME_MOD) { | 
 | 110 | 		inode->i_mtime = tv; | 
 | 111 | 		ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec; | 
 | 112 | 		ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec; | 
 | 113 | 	} | 
 | 114 | 	if (flags & XFS_ICHGTIME_ACC) { | 
 | 115 | 		inode->i_atime = tv; | 
 | 116 | 		ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec; | 
 | 117 | 		ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec; | 
 | 118 | 	} | 
 | 119 | 	if (flags & XFS_ICHGTIME_CHG) { | 
 | 120 | 		inode->i_ctime = tv; | 
 | 121 | 		ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec; | 
 | 122 | 		ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec; | 
 | 123 | 	} | 
 | 124 |  | 
 | 125 | 	/* | 
 | 126 | 	 * We update the i_update_core field _after_ changing | 
 | 127 | 	 * the timestamps in order to coordinate properly with | 
 | 128 | 	 * xfs_iflush() so that we don't lose timestamp updates. | 
 | 129 | 	 * This keeps us from having to hold the inode lock | 
 | 130 | 	 * while doing this.  We use the SYNCHRONIZE macro to | 
 | 131 | 	 * ensure that the compiler does not reorder the update | 
 | 132 | 	 * of i_update_core above the timestamp updates above. | 
 | 133 | 	 */ | 
 | 134 | 	SYNCHRONIZE(); | 
 | 135 | 	ip->i_update_core = 1; | 
| David Chinner | cf10e82 | 2007-12-07 14:09:11 +1100 | [diff] [blame] | 136 | 	if (!(inode->i_state & I_NEW)) | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 137 | 		mark_inode_dirty_sync(inode); | 
 | 138 | } | 
 | 139 |  | 
 | 140 | /* | 
 | 141 |  * Variant on the above which avoids querying the system clock | 
 | 142 |  * in situations where we know the Linux inode timestamps have | 
 | 143 |  * just been updated (and so we can update our inode cheaply). | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 144 |  */ | 
 | 145 | void | 
 | 146 | xfs_ichgtime_fast( | 
 | 147 | 	xfs_inode_t	*ip, | 
 | 148 | 	struct inode	*inode, | 
 | 149 | 	int		flags) | 
 | 150 | { | 
 | 151 | 	timespec_t	*tvp; | 
 | 152 |  | 
 | 153 | 	/* | 
| Christoph Hellwig | 42fe2b1 | 2006-01-11 15:35:17 +1100 | [diff] [blame] | 154 | 	 * Atime updates for read() & friends are handled lazily now, and | 
 | 155 | 	 * explicit updates must go through xfs_ichgtime() | 
 | 156 | 	 */ | 
 | 157 | 	ASSERT((flags & XFS_ICHGTIME_ACC) == 0); | 
 | 158 |  | 
 | 159 | 	/* | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 160 | 	 * We're not supposed to change timestamps in readonly-mounted | 
 | 161 | 	 * filesystems.  Throw it away if anyone asks us. | 
 | 162 | 	 */ | 
 | 163 | 	if (unlikely(IS_RDONLY(inode))) | 
 | 164 | 		return; | 
 | 165 |  | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 166 | 	if (flags & XFS_ICHGTIME_MOD) { | 
 | 167 | 		tvp = &inode->i_mtime; | 
 | 168 | 		ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec; | 
 | 169 | 		ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec; | 
 | 170 | 	} | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 171 | 	if (flags & XFS_ICHGTIME_CHG) { | 
 | 172 | 		tvp = &inode->i_ctime; | 
 | 173 | 		ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec; | 
 | 174 | 		ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec; | 
 | 175 | 	} | 
 | 176 |  | 
 | 177 | 	/* | 
 | 178 | 	 * We update the i_update_core field _after_ changing | 
 | 179 | 	 * the timestamps in order to coordinate properly with | 
 | 180 | 	 * xfs_iflush() so that we don't lose timestamp updates. | 
 | 181 | 	 * This keeps us from having to hold the inode lock | 
 | 182 | 	 * while doing this.  We use the SYNCHRONIZE macro to | 
 | 183 | 	 * ensure that the compiler does not reorder the update | 
 | 184 | 	 * of i_update_core above the timestamp updates above. | 
 | 185 | 	 */ | 
 | 186 | 	SYNCHRONIZE(); | 
 | 187 | 	ip->i_update_core = 1; | 
| David Chinner | cf10e82 | 2007-12-07 14:09:11 +1100 | [diff] [blame] | 188 | 	if (!(inode->i_state & I_NEW)) | 
| Nathan Scott | 4aeb664 | 2005-11-02 11:43:58 +1100 | [diff] [blame] | 189 | 		mark_inode_dirty_sync(inode); | 
 | 190 | } | 
 | 191 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 |  | 
 | 193 | /* | 
 | 194 |  * Pull the link count and size up from the xfs inode to the linux inode | 
 | 195 |  */ | 
 | 196 | STATIC void | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 197 | xfs_validate_fields( | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 198 | 	struct inode		*inode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 200 | 	struct xfs_inode	*ip = XFS_I(inode); | 
 | 201 | 	loff_t size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 |  | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 203 | 	inode->i_nlink = ip->i_d.di_nlink; | 
 | 204 | 	inode->i_blocks = | 
 | 205 | 		XFS_FSB_TO_BB(ip->i_mount, ip->i_d.di_nblocks + | 
 | 206 | 					   ip->i_delayed_blks); | 
 | 207 | 	/* we're under i_sem so i_size can't change under us */ | 
 | 208 | 	size = XFS_ISIZE(ip); | 
 | 209 | 	if (i_size_read(inode) != size) | 
 | 210 | 		i_size_write(inode, size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } | 
 | 212 |  | 
 | 213 | /* | 
| Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 214 |  * Hook in SELinux.  This is not quite correct yet, what we really need | 
 | 215 |  * here (as we do for default ACLs) is a mechanism by which creation of | 
 | 216 |  * these attrs can be journalled at inode creation time (along with the | 
 | 217 |  * inode, of course, such that log replay can't cause these to be lost). | 
 | 218 |  */ | 
 | 219 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 220 | xfs_init_security( | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 221 | 	bhv_vnode_t	*vp, | 
| Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 222 | 	struct inode	*dir) | 
 | 223 | { | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 224 | 	struct inode	*ip = vn_to_inode(vp); | 
| Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 225 | 	size_t		length; | 
 | 226 | 	void		*value; | 
 | 227 | 	char		*name; | 
 | 228 | 	int		error; | 
 | 229 |  | 
 | 230 | 	error = security_inode_init_security(ip, dir, &name, &value, &length); | 
 | 231 | 	if (error) { | 
 | 232 | 		if (error == -EOPNOTSUPP) | 
 | 233 | 			return 0; | 
 | 234 | 		return -error; | 
 | 235 | 	} | 
 | 236 |  | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 237 | 	error = xfs_attr_set(XFS_I(ip), name, value, | 
 | 238 | 			length, ATTR_SECURE); | 
| Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 239 | 	if (!error) | 
| Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 240 | 		xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED); | 
| Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 241 |  | 
 | 242 | 	kfree(name); | 
 | 243 | 	kfree(value); | 
 | 244 | 	return error; | 
 | 245 | } | 
 | 246 |  | 
 | 247 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 |  * Determine whether a process has a valid fs_struct (kernel daemons | 
 | 249 |  * like knfsd don't have an fs_struct). | 
 | 250 |  * | 
 | 251 |  * XXX(hch):  nfsd is broken, better fix it instead. | 
 | 252 |  */ | 
| David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 253 | STATIC_INLINE int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 254 | xfs_has_fs_struct(struct task_struct *task) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { | 
 | 256 | 	return (task->fs != init_task.fs); | 
 | 257 | } | 
 | 258 |  | 
| David Chinner | 7989cb8 | 2007-02-10 18:34:56 +1100 | [diff] [blame] | 259 | STATIC void | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 260 | xfs_cleanup_inode( | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 261 | 	struct inode	*dir, | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 262 | 	bhv_vnode_t	*vp, | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 263 | 	struct dentry	*dentry, | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 264 | 	int		mode) | 
 | 265 | { | 
 | 266 | 	struct dentry   teardown = {}; | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 267 |  | 
 | 268 | 	/* Oh, the horror. | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 269 | 	 * If we can't add the ACL or we fail in | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 270 | 	 * xfs_init_security we must back out. | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 271 | 	 * ENOSPC can hit here, among other things. | 
 | 272 | 	 */ | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 273 | 	teardown.d_inode = vn_to_inode(vp); | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 274 | 	teardown.d_name = dentry->d_name; | 
 | 275 |  | 
 | 276 | 	if (S_ISDIR(mode)) | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 277 | 		xfs_rmdir(XFS_I(dir), &teardown); | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 278 | 	else | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 279 | 		xfs_remove(XFS_I(dir), &teardown); | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 280 | 	VN_RELE(vp); | 
 | 281 | } | 
 | 282 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 284 | xfs_vn_mknod( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | 	struct inode	*dir, | 
 | 286 | 	struct dentry	*dentry, | 
 | 287 | 	int		mode, | 
 | 288 | 	dev_t		rdev) | 
 | 289 | { | 
 | 290 | 	struct inode	*ip; | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 291 | 	bhv_vnode_t	*vp = NULL, *dvp = vn_from_inode(dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | 	xfs_acl_t	*default_acl = NULL; | 
 | 293 | 	attrexists_t	test_default_acl = _ACL_DEFAULT_EXISTS; | 
 | 294 | 	int		error; | 
 | 295 |  | 
 | 296 | 	/* | 
 | 297 | 	 * Irix uses Missed'em'V split, but doesn't want to see | 
 | 298 | 	 * the upper 5 bits of (14bit) major. | 
 | 299 | 	 */ | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 300 | 	if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | 		return -EINVAL; | 
 | 302 |  | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 303 | 	if (unlikely(test_default_acl && test_default_acl(dvp))) { | 
 | 304 | 		if (!_ACL_ALLOC(default_acl)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | 			return -ENOMEM; | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 306 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | 		if (!_ACL_GET_DEFAULT(dvp, default_acl)) { | 
 | 308 | 			_ACL_FREE(default_acl); | 
 | 309 | 			default_acl = NULL; | 
 | 310 | 		} | 
 | 311 | 	} | 
 | 312 |  | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 313 | 	if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | 		mode &= ~current->fs->umask; | 
 | 315 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | 	switch (mode & S_IFMT) { | 
 | 317 | 	case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK: | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 318 | 		rdev = sysv_encode_dev(rdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | 	case S_IFREG: | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 320 | 		error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | 		break; | 
 | 322 | 	case S_IFDIR: | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 323 | 		error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | 		break; | 
 | 325 | 	default: | 
 | 326 | 		error = EINVAL; | 
 | 327 | 		break; | 
 | 328 | 	} | 
 | 329 |  | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 330 | 	if (unlikely(!error)) { | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 331 | 		error = xfs_init_security(vp, dir); | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 332 | 		if (error) | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 333 | 			xfs_cleanup_inode(dir, vp, dentry, mode); | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 334 | 	} | 
| Nathan Scott | 446ada4 | 2006-01-11 15:35:44 +1100 | [diff] [blame] | 335 |  | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 336 | 	if (unlikely(default_acl)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | 		if (!error) { | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 338 | 			error = _ACL_INHERIT(vp, mode, default_acl); | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 339 | 			if (!error) | 
| Christoph Hellwig | 0a74cd1 | 2007-08-29 11:53:12 +1000 | [diff] [blame] | 340 | 				xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED); | 
| Yingping Lu | 3a69c7d | 2006-02-01 12:14:34 +1100 | [diff] [blame] | 341 | 			else | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 342 | 				xfs_cleanup_inode(dir, vp, dentry, mode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | 		} | 
 | 344 | 		_ACL_FREE(default_acl); | 
 | 345 | 	} | 
 | 346 |  | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 347 | 	if (likely(!error)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | 		ASSERT(vp); | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 349 | 		ip = vn_to_inode(vp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 |  | 
| Christoph Hellwig | bad60fd | 2007-12-21 10:58:56 +1100 | [diff] [blame] | 351 | 		if (S_ISDIR(mode)) | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 352 | 			xfs_validate_fields(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | 		d_instantiate(dentry, ip); | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 354 | 		xfs_validate_fields(dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | 	} | 
 | 356 | 	return -error; | 
 | 357 | } | 
 | 358 |  | 
 | 359 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 360 | xfs_vn_create( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | 	struct inode	*dir, | 
 | 362 | 	struct dentry	*dentry, | 
 | 363 | 	int		mode, | 
 | 364 | 	struct nameidata *nd) | 
 | 365 | { | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 366 | 	return xfs_vn_mknod(dir, dentry, mode, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } | 
 | 368 |  | 
 | 369 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 370 | xfs_vn_mkdir( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | 	struct inode	*dir, | 
 | 372 | 	struct dentry	*dentry, | 
 | 373 | 	int		mode) | 
 | 374 | { | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 375 | 	return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } | 
 | 377 |  | 
 | 378 | STATIC struct dentry * | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 379 | xfs_vn_lookup( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | 	struct inode	*dir, | 
 | 381 | 	struct dentry	*dentry, | 
 | 382 | 	struct nameidata *nd) | 
 | 383 | { | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 384 | 	bhv_vnode_t	*cvp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | 	int		error; | 
 | 386 |  | 
 | 387 | 	if (dentry->d_name.len >= MAXNAMELEN) | 
 | 388 | 		return ERR_PTR(-ENAMETOOLONG); | 
 | 389 |  | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 390 | 	error = xfs_lookup(XFS_I(dir), dentry, &cvp); | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 391 | 	if (unlikely(error)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | 		if (unlikely(error != ENOENT)) | 
 | 393 | 			return ERR_PTR(-error); | 
 | 394 | 		d_add(dentry, NULL); | 
 | 395 | 		return NULL; | 
 | 396 | 	} | 
 | 397 |  | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 398 | 	return d_splice_alias(vn_to_inode(cvp), dentry); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } | 
 | 400 |  | 
 | 401 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 402 | xfs_vn_link( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | 	struct dentry	*old_dentry, | 
 | 404 | 	struct inode	*dir, | 
 | 405 | 	struct dentry	*dentry) | 
 | 406 | { | 
 | 407 | 	struct inode	*ip;	/* inode of guy being linked to */ | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 408 | 	bhv_vnode_t	*vp;	/* vp of name being linked */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | 	int		error; | 
 | 410 |  | 
 | 411 | 	ip = old_dentry->d_inode;	/* inode being linked to */ | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 412 | 	vp = vn_from_inode(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 |  | 
| Nathan Scott | 97dfd70 | 2006-06-27 16:13:46 +1000 | [diff] [blame] | 414 | 	VN_HOLD(vp); | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 415 | 	error = xfs_link(XFS_I(dir), vp, dentry); | 
| Nathan Scott | 97dfd70 | 2006-06-27 16:13:46 +1000 | [diff] [blame] | 416 | 	if (unlikely(error)) { | 
 | 417 | 		VN_RELE(vp); | 
 | 418 | 	} else { | 
| Christoph Hellwig | b3aea4e | 2007-08-29 11:44:37 +1000 | [diff] [blame] | 419 | 		xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED); | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 420 | 		xfs_validate_fields(ip); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | 		d_instantiate(dentry, ip); | 
 | 422 | 	} | 
 | 423 | 	return -error; | 
 | 424 | } | 
 | 425 |  | 
 | 426 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 427 | xfs_vn_unlink( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | 	struct inode	*dir, | 
 | 429 | 	struct dentry	*dentry) | 
 | 430 | { | 
 | 431 | 	struct inode	*inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | 	int		error; | 
 | 433 |  | 
 | 434 | 	inode = dentry->d_inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 |  | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 436 | 	error = xfs_remove(XFS_I(dir), dentry); | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 437 | 	if (likely(!error)) { | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 438 | 		xfs_validate_fields(dir);	/* size needs update */ | 
 | 439 | 		xfs_validate_fields(inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | 	return -error; | 
 | 442 | } | 
 | 443 |  | 
 | 444 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 445 | xfs_vn_symlink( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | 	struct inode	*dir, | 
 | 447 | 	struct dentry	*dentry, | 
 | 448 | 	const char	*symname) | 
 | 449 | { | 
 | 450 | 	struct inode	*ip; | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 451 | 	bhv_vnode_t	*cvp;	/* used to lookup symlink to put in dentry */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | 	int		error; | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 453 | 	mode_t		mode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | 	cvp = NULL; | 
 | 456 |  | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 457 | 	mode = S_IFLNK | | 
| Christoph Hellwig | 0432dab | 2005-09-02 16:46:51 +1000 | [diff] [blame] | 458 | 		(irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 |  | 
| Christoph Hellwig | 3e5daf0 | 2007-10-11 18:09:12 +1000 | [diff] [blame] | 460 | 	error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode, | 
 | 461 | 			    &cvp, NULL); | 
| Nathan Scott | 5424570 | 2006-01-16 15:54:05 +1100 | [diff] [blame] | 462 | 	if (likely(!error && cvp)) { | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 463 | 		error = xfs_init_security(cvp, dir); | 
| Nathan Scott | 5424570 | 2006-01-16 15:54:05 +1100 | [diff] [blame] | 464 | 		if (likely(!error)) { | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 465 | 			ip = vn_to_inode(cvp); | 
| Nathan Scott | 5424570 | 2006-01-16 15:54:05 +1100 | [diff] [blame] | 466 | 			d_instantiate(dentry, ip); | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 467 | 			xfs_validate_fields(dir); | 
 | 468 | 			xfs_validate_fields(ip); | 
| Nathan Scott | ce9d37c | 2006-03-17 17:26:34 +1100 | [diff] [blame] | 469 | 		} else { | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 470 | 			xfs_cleanup_inode(dir, cvp, dentry, 0); | 
| Nathan Scott | 5424570 | 2006-01-16 15:54:05 +1100 | [diff] [blame] | 471 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | 	} | 
 | 473 | 	return -error; | 
 | 474 | } | 
 | 475 |  | 
 | 476 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 477 | xfs_vn_rmdir( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | 	struct inode	*dir, | 
 | 479 | 	struct dentry	*dentry) | 
 | 480 | { | 
 | 481 | 	struct inode	*inode = dentry->d_inode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | 	int		error; | 
 | 483 |  | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 484 | 	error = xfs_rmdir(XFS_I(dir), dentry); | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 485 | 	if (likely(!error)) { | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 486 | 		xfs_validate_fields(inode); | 
 | 487 | 		xfs_validate_fields(dir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | 	} | 
 | 489 | 	return -error; | 
 | 490 | } | 
 | 491 |  | 
 | 492 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 493 | xfs_vn_rename( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | 	struct inode	*odir, | 
 | 495 | 	struct dentry	*odentry, | 
 | 496 | 	struct inode	*ndir, | 
 | 497 | 	struct dentry	*ndentry) | 
 | 498 | { | 
 | 499 | 	struct inode	*new_inode = ndentry->d_inode; | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 500 | 	bhv_vnode_t	*tvp;	/* target directory */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | 	int		error; | 
 | 502 |  | 
| Nathan Scott | ec86dc0 | 2006-03-17 17:25:36 +1100 | [diff] [blame] | 503 | 	tvp = vn_from_inode(ndir); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 |  | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 505 | 	error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry); | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 506 | 	if (likely(!error)) { | 
 | 507 | 		if (new_inode) | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 508 | 			xfs_validate_fields(new_inode); | 
 | 509 | 		xfs_validate_fields(odir); | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 510 | 		if (ndir != odir) | 
| Christoph Hellwig | 6572bc2 | 2007-09-19 15:27:39 +1000 | [diff] [blame] | 511 | 			xfs_validate_fields(ndir); | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 512 | 	} | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 513 | 	return -error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } | 
 | 515 |  | 
 | 516 | /* | 
 | 517 |  * careful here - this function can get called recursively, so | 
 | 518 |  * we need to be very careful about how much stack we use. | 
 | 519 |  * uio is kmalloced for this reason... | 
 | 520 |  */ | 
| Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 521 | STATIC void * | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 522 | xfs_vn_follow_link( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | 	struct dentry		*dentry, | 
 | 524 | 	struct nameidata	*nd) | 
 | 525 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | 	char			*link; | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 527 | 	int			error = -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 |  | 
| Panagiotis Issaris | f52720c | 2006-09-27 01:49:39 -0700 | [diff] [blame] | 529 | 	link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 530 | 	if (!link) | 
 | 531 | 		goto out_err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 |  | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 533 | 	error = -xfs_readlink(XFS_I(dentry->d_inode), link); | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 534 | 	if (unlikely(error)) | 
 | 535 | 		goto out_kfree; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 |  | 
 | 537 | 	nd_set_link(nd, link); | 
| Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 538 | 	return NULL; | 
| Christoph Hellwig | 804c83c | 2007-08-28 13:59:03 +1000 | [diff] [blame] | 539 |  | 
 | 540 |  out_kfree: | 
 | 541 | 	kfree(link); | 
 | 542 |  out_err: | 
 | 543 | 	nd_set_link(nd, ERR_PTR(error)); | 
 | 544 | 	return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } | 
 | 546 |  | 
| Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 547 | STATIC void | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 548 | xfs_vn_put_link( | 
| Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 549 | 	struct dentry	*dentry, | 
 | 550 | 	struct nameidata *nd, | 
 | 551 | 	void		*p) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { | 
| Nathan Scott | cde410a | 2005-09-05 11:47:01 +1000 | [diff] [blame] | 553 | 	char		*s = nd_get_link(nd); | 
 | 554 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | 	if (!IS_ERR(s)) | 
 | 556 | 		kfree(s); | 
 | 557 | } | 
 | 558 |  | 
 | 559 | #ifdef CONFIG_XFS_POSIX_ACL | 
 | 560 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 561 | xfs_vn_permission( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | 	struct inode	*inode, | 
 | 563 | 	int		mode, | 
 | 564 | 	struct nameidata *nd) | 
 | 565 | { | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 566 | 	return -xfs_access(XFS_I(inode), mode << 6, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } | 
 | 568 | #else | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 569 | #define xfs_vn_permission NULL | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | #endif | 
 | 571 |  | 
 | 572 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 573 | xfs_vn_getattr( | 
| Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 574 | 	struct vfsmount		*mnt, | 
 | 575 | 	struct dentry		*dentry, | 
 | 576 | 	struct kstat		*stat) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | { | 
| Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 578 | 	struct inode		*inode = dentry->d_inode; | 
 | 579 | 	struct xfs_inode	*ip = XFS_I(inode); | 
 | 580 | 	struct xfs_mount	*mp = ip->i_mount; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 |  | 
| Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 582 | 	xfs_itrace_entry(ip); | 
 | 583 |  | 
 | 584 | 	if (XFS_FORCED_SHUTDOWN(mp)) | 
 | 585 | 		return XFS_ERROR(EIO); | 
 | 586 |  | 
 | 587 | 	stat->size = XFS_ISIZE(ip); | 
 | 588 | 	stat->dev = inode->i_sb->s_dev; | 
 | 589 | 	stat->mode = ip->i_d.di_mode; | 
 | 590 | 	stat->nlink = ip->i_d.di_nlink; | 
 | 591 | 	stat->uid = ip->i_d.di_uid; | 
 | 592 | 	stat->gid = ip->i_d.di_gid; | 
 | 593 | 	stat->ino = ip->i_ino; | 
 | 594 | #if XFS_BIG_INUMS | 
 | 595 | 	stat->ino += mp->m_inoadd; | 
 | 596 | #endif | 
 | 597 | 	stat->atime = inode->i_atime; | 
 | 598 | 	stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec; | 
 | 599 | 	stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; | 
 | 600 | 	stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec; | 
 | 601 | 	stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; | 
 | 602 | 	stat->blocks = | 
 | 603 | 		XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); | 
 | 604 |  | 
 | 605 |  | 
 | 606 | 	switch (inode->i_mode & S_IFMT) { | 
 | 607 | 	case S_IFBLK: | 
 | 608 | 	case S_IFCHR: | 
 | 609 | 		stat->blksize = BLKDEV_IOSIZE; | 
 | 610 | 		stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, | 
 | 611 | 				   sysv_minor(ip->i_df.if_u2.if_rdev)); | 
 | 612 | 		break; | 
 | 613 | 	default: | 
 | 614 | 		if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) { | 
 | 615 | 			/* | 
 | 616 | 			 * If the file blocks are being allocated from a | 
 | 617 | 			 * realtime volume, then return the inode's realtime | 
 | 618 | 			 * extent size or the realtime volume's extent size. | 
 | 619 | 			 */ | 
 | 620 | 			stat->blksize = | 
 | 621 | 				xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog; | 
 | 622 | 		} else | 
 | 623 | 			stat->blksize = xfs_preferred_iosize(mp); | 
 | 624 | 		stat->rdev = 0; | 
 | 625 | 		break; | 
| Nathan Scott | 69e23b9 | 2006-09-28 11:01:22 +1000 | [diff] [blame] | 626 | 	} | 
| Christoph Hellwig | c43f408 | 2007-10-11 17:46:39 +1000 | [diff] [blame] | 627 |  | 
 | 628 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } | 
 | 630 |  | 
 | 631 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 632 | xfs_vn_setattr( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | 	struct dentry	*dentry, | 
 | 634 | 	struct iattr	*attr) | 
 | 635 | { | 
 | 636 | 	struct inode	*inode = dentry->d_inode; | 
 | 637 | 	unsigned int	ia_valid = attr->ia_valid; | 
| Nathan Scott | 8285fb5 | 2006-06-09 17:07:12 +1000 | [diff] [blame] | 638 | 	bhv_vattr_t	vattr = { 0 }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | 	int		flags = 0; | 
 | 640 | 	int		error; | 
 | 641 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | 	if (ia_valid & ATTR_UID) { | 
 | 643 | 		vattr.va_mask |= XFS_AT_UID; | 
 | 644 | 		vattr.va_uid = attr->ia_uid; | 
 | 645 | 	} | 
 | 646 | 	if (ia_valid & ATTR_GID) { | 
 | 647 | 		vattr.va_mask |= XFS_AT_GID; | 
 | 648 | 		vattr.va_gid = attr->ia_gid; | 
 | 649 | 	} | 
 | 650 | 	if (ia_valid & ATTR_SIZE) { | 
 | 651 | 		vattr.va_mask |= XFS_AT_SIZE; | 
 | 652 | 		vattr.va_size = attr->ia_size; | 
 | 653 | 	} | 
 | 654 | 	if (ia_valid & ATTR_ATIME) { | 
 | 655 | 		vattr.va_mask |= XFS_AT_ATIME; | 
 | 656 | 		vattr.va_atime = attr->ia_atime; | 
| Nathan Scott | 8c0b511 | 2006-04-11 15:12:45 +1000 | [diff] [blame] | 657 | 		inode->i_atime = attr->ia_atime; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | 	} | 
 | 659 | 	if (ia_valid & ATTR_MTIME) { | 
 | 660 | 		vattr.va_mask |= XFS_AT_MTIME; | 
 | 661 | 		vattr.va_mtime = attr->ia_mtime; | 
 | 662 | 	} | 
 | 663 | 	if (ia_valid & ATTR_CTIME) { | 
 | 664 | 		vattr.va_mask |= XFS_AT_CTIME; | 
 | 665 | 		vattr.va_ctime = attr->ia_ctime; | 
 | 666 | 	} | 
 | 667 | 	if (ia_valid & ATTR_MODE) { | 
 | 668 | 		vattr.va_mask |= XFS_AT_MODE; | 
 | 669 | 		vattr.va_mode = attr->ia_mode; | 
 | 670 | 		if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | 
 | 671 | 			inode->i_mode &= ~S_ISGID; | 
 | 672 | 	} | 
 | 673 |  | 
 | 674 | 	if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) | 
 | 675 | 		flags |= ATTR_UTIME; | 
 | 676 | #ifdef ATTR_NO_BLOCK | 
 | 677 | 	if ((ia_valid & ATTR_NO_BLOCK)) | 
 | 678 | 		flags |= ATTR_NONBLOCK; | 
 | 679 | #endif | 
 | 680 |  | 
| Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 681 | 	error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL); | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 682 | 	if (likely(!error)) | 
| Christoph Hellwig | 21a6254 | 2007-09-19 15:27:49 +1000 | [diff] [blame] | 683 | 		vn_revalidate(vn_from_inode(inode)); | 
| Nathan Scott | 220b528 | 2006-03-14 13:33:36 +1100 | [diff] [blame] | 684 | 	return -error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } | 
 | 686 |  | 
 | 687 | STATIC void | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 688 | xfs_vn_truncate( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | 	struct inode	*inode) | 
 | 690 | { | 
| Nathan Scott | c253666 | 2006-03-29 10:44:40 +1000 | [diff] [blame] | 691 | 	block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } | 
 | 693 |  | 
 | 694 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 695 | xfs_vn_setxattr( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | 	struct dentry	*dentry, | 
 | 697 | 	const char	*name, | 
 | 698 | 	const void	*data, | 
 | 699 | 	size_t		size, | 
 | 700 | 	int		flags) | 
 | 701 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 702 | 	bhv_vnode_t	*vp = vn_from_inode(dentry->d_inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | 	char		*attr = (char *)name; | 
 | 704 | 	attrnames_t	*namesp; | 
 | 705 | 	int		xflags = 0; | 
 | 706 | 	int		error; | 
 | 707 |  | 
 | 708 | 	namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | 
 | 709 | 	if (!namesp) | 
 | 710 | 		return -EOPNOTSUPP; | 
 | 711 | 	attr += namesp->attr_namelen; | 
 | 712 | 	error = namesp->attr_capable(vp, NULL); | 
 | 713 | 	if (error) | 
 | 714 | 		return error; | 
 | 715 |  | 
 | 716 | 	/* Convert Linux syscall to XFS internal ATTR flags */ | 
 | 717 | 	if (flags & XATTR_CREATE) | 
 | 718 | 		xflags |= ATTR_CREATE; | 
 | 719 | 	if (flags & XATTR_REPLACE) | 
 | 720 | 		xflags |= ATTR_REPLACE; | 
 | 721 | 	xflags |= namesp->attr_flag; | 
 | 722 | 	return namesp->attr_set(vp, attr, (void *)data, size, xflags); | 
 | 723 | } | 
 | 724 |  | 
 | 725 | STATIC ssize_t | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 726 | xfs_vn_getxattr( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | 	struct dentry	*dentry, | 
 | 728 | 	const char	*name, | 
 | 729 | 	void		*data, | 
 | 730 | 	size_t		size) | 
 | 731 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 732 | 	bhv_vnode_t	*vp = vn_from_inode(dentry->d_inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | 	char		*attr = (char *)name; | 
 | 734 | 	attrnames_t	*namesp; | 
 | 735 | 	int		xflags = 0; | 
 | 736 | 	ssize_t		error; | 
 | 737 |  | 
 | 738 | 	namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | 
 | 739 | 	if (!namesp) | 
 | 740 | 		return -EOPNOTSUPP; | 
 | 741 | 	attr += namesp->attr_namelen; | 
 | 742 | 	error = namesp->attr_capable(vp, NULL); | 
 | 743 | 	if (error) | 
 | 744 | 		return error; | 
 | 745 |  | 
 | 746 | 	/* Convert Linux syscall to XFS internal ATTR flags */ | 
 | 747 | 	if (!size) { | 
 | 748 | 		xflags |= ATTR_KERNOVAL; | 
 | 749 | 		data = NULL; | 
 | 750 | 	} | 
 | 751 | 	xflags |= namesp->attr_flag; | 
 | 752 | 	return namesp->attr_get(vp, attr, (void *)data, size, xflags); | 
 | 753 | } | 
 | 754 |  | 
 | 755 | STATIC ssize_t | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 756 | xfs_vn_listxattr( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | 	struct dentry		*dentry, | 
 | 758 | 	char			*data, | 
 | 759 | 	size_t			size) | 
 | 760 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 761 | 	bhv_vnode_t		*vp = vn_from_inode(dentry->d_inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | 	int			error, xflags = ATTR_KERNAMELS; | 
 | 763 | 	ssize_t			result; | 
 | 764 |  | 
 | 765 | 	if (!size) | 
 | 766 | 		xflags |= ATTR_KERNOVAL; | 
 | 767 | 	xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS; | 
 | 768 |  | 
 | 769 | 	error = attr_generic_list(vp, data, size, xflags, &result); | 
 | 770 | 	if (error < 0) | 
 | 771 | 		return error; | 
 | 772 | 	return result; | 
 | 773 | } | 
 | 774 |  | 
 | 775 | STATIC int | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 776 | xfs_vn_removexattr( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | 	struct dentry	*dentry, | 
 | 778 | 	const char	*name) | 
 | 779 | { | 
| Nathan Scott | 67fcaa7 | 2006-06-09 17:00:52 +1000 | [diff] [blame] | 780 | 	bhv_vnode_t	*vp = vn_from_inode(dentry->d_inode); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | 	char		*attr = (char *)name; | 
 | 782 | 	attrnames_t	*namesp; | 
 | 783 | 	int		xflags = 0; | 
 | 784 | 	int		error; | 
 | 785 |  | 
 | 786 | 	namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | 
 | 787 | 	if (!namesp) | 
 | 788 | 		return -EOPNOTSUPP; | 
 | 789 | 	attr += namesp->attr_namelen; | 
 | 790 | 	error = namesp->attr_capable(vp, NULL); | 
 | 791 | 	if (error) | 
 | 792 | 		return error; | 
 | 793 | 	xflags |= namesp->attr_flag; | 
 | 794 | 	return namesp->attr_remove(vp, attr, xflags); | 
 | 795 | } | 
 | 796 |  | 
 | 797 |  | 
| Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 798 | const struct inode_operations xfs_inode_operations = { | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 799 | 	.permission		= xfs_vn_permission, | 
 | 800 | 	.truncate		= xfs_vn_truncate, | 
 | 801 | 	.getattr		= xfs_vn_getattr, | 
 | 802 | 	.setattr		= xfs_vn_setattr, | 
 | 803 | 	.setxattr		= xfs_vn_setxattr, | 
 | 804 | 	.getxattr		= xfs_vn_getxattr, | 
 | 805 | 	.listxattr		= xfs_vn_listxattr, | 
 | 806 | 	.removexattr		= xfs_vn_removexattr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | }; | 
 | 808 |  | 
| Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 809 | const struct inode_operations xfs_dir_inode_operations = { | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 810 | 	.create			= xfs_vn_create, | 
 | 811 | 	.lookup			= xfs_vn_lookup, | 
 | 812 | 	.link			= xfs_vn_link, | 
 | 813 | 	.unlink			= xfs_vn_unlink, | 
 | 814 | 	.symlink		= xfs_vn_symlink, | 
 | 815 | 	.mkdir			= xfs_vn_mkdir, | 
 | 816 | 	.rmdir			= xfs_vn_rmdir, | 
 | 817 | 	.mknod			= xfs_vn_mknod, | 
 | 818 | 	.rename			= xfs_vn_rename, | 
 | 819 | 	.permission		= xfs_vn_permission, | 
 | 820 | 	.getattr		= xfs_vn_getattr, | 
 | 821 | 	.setattr		= xfs_vn_setattr, | 
 | 822 | 	.setxattr		= xfs_vn_setxattr, | 
 | 823 | 	.getxattr		= xfs_vn_getxattr, | 
 | 824 | 	.listxattr		= xfs_vn_listxattr, | 
 | 825 | 	.removexattr		= xfs_vn_removexattr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | }; | 
 | 827 |  | 
| Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 828 | const struct inode_operations xfs_symlink_inode_operations = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | 	.readlink		= generic_readlink, | 
| Nathan Scott | 416c6d5 | 2006-03-14 14:00:51 +1100 | [diff] [blame] | 830 | 	.follow_link		= xfs_vn_follow_link, | 
 | 831 | 	.put_link		= xfs_vn_put_link, | 
 | 832 | 	.permission		= xfs_vn_permission, | 
 | 833 | 	.getattr		= xfs_vn_getattr, | 
 | 834 | 	.setattr		= xfs_vn_setattr, | 
 | 835 | 	.setxattr		= xfs_vn_setxattr, | 
 | 836 | 	.getxattr		= xfs_vn_getxattr, | 
 | 837 | 	.listxattr		= xfs_vn_listxattr, | 
 | 838 | 	.removexattr		= xfs_vn_removexattr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | }; |