blob: d9180020de6328fa8dbbeb17f68027805d368fae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * 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 Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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 Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
19#include "xfs_fs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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 Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
37#include "xfs_inode.h"
38#include "xfs_bmap.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_itable.h"
44#include "xfs_rw.h"
45#include "xfs_acl.h"
46#include "xfs_cap.h"
47#include "xfs_mac.h"
48#include "xfs_attr.h"
49#include "xfs_buf_item.h"
50#include "xfs_utils.h"
51
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080052#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/xattr.h>
54#include <linux/namei.h>
Nathan Scott446ada42006-01-11 15:35:44 +110055#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Nathan Scott4aeb6642005-11-02 11:43:58 +110057/*
Christoph Hellwig75e17b32006-01-11 20:58:44 +110058 * Get a XFS inode from a given vnode.
59 */
60xfs_inode_t *
61xfs_vtoi(
Nathan Scott67fcaa72006-06-09 17:00:52 +100062 bhv_vnode_t *vp)
Christoph Hellwig75e17b32006-01-11 20:58:44 +110063{
64 bhv_desc_t *bdp;
65
66 bdp = bhv_lookup_range(VN_BHV_HEAD(vp),
67 VNODE_POSITION_XFS, VNODE_POSITION_XFS);
68 if (unlikely(bdp == NULL))
69 return NULL;
70 return XFS_BHVTOI(bdp);
71}
72
73/*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110074 * Bring the atime in the XFS inode uptodate.
75 * Used before logging the inode to disk or when the Linux inode goes away.
76 */
77void
78xfs_synchronize_atime(
79 xfs_inode_t *ip)
80{
Nathan Scott67fcaa72006-06-09 17:00:52 +100081 bhv_vnode_t *vp;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110082
83 vp = XFS_ITOV_NULL(ip);
84 if (vp) {
85 struct inode *inode = &vp->v_inode;
86 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
87 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
88 }
89}
90
91/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110092 * Change the requested timestamp in the given inode.
93 * We don't lock across timestamp updates, and we don't log them but
94 * we do record the fact that there is dirty information in core.
95 *
96 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
97 * with XFS_ICHGTIME_ACC to be sure that access time
98 * update will take. Calling first with XFS_ICHGTIME_ACC
99 * and then XFS_ICHGTIME_MOD may fail to modify the access
100 * timestamp if the filesystem is mounted noacctm.
101 */
102void
103xfs_ichgtime(
104 xfs_inode_t *ip,
105 int flags)
106{
Nathan Scottec86dc02006-03-17 17:25:36 +1100107 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Nathan Scott4aeb6642005-11-02 11:43:58 +1100108 timespec_t tv;
109
Nathan Scott4aeb6642005-11-02 11:43:58 +1100110 nanotime(&tv);
111 if (flags & XFS_ICHGTIME_MOD) {
112 inode->i_mtime = tv;
113 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
114 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
115 }
116 if (flags & XFS_ICHGTIME_ACC) {
117 inode->i_atime = tv;
118 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
119 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
120 }
121 if (flags & XFS_ICHGTIME_CHG) {
122 inode->i_ctime = tv;
123 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
124 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
125 }
126
127 /*
128 * We update the i_update_core field _after_ changing
129 * the timestamps in order to coordinate properly with
130 * xfs_iflush() so that we don't lose timestamp updates.
131 * This keeps us from having to hold the inode lock
132 * while doing this. We use the SYNCHRONIZE macro to
133 * ensure that the compiler does not reorder the update
134 * of i_update_core above the timestamp updates above.
135 */
136 SYNCHRONIZE();
137 ip->i_update_core = 1;
138 if (!(inode->i_state & I_LOCK))
139 mark_inode_dirty_sync(inode);
140}
141
142/*
143 * Variant on the above which avoids querying the system clock
144 * in situations where we know the Linux inode timestamps have
145 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100146 */
147void
148xfs_ichgtime_fast(
149 xfs_inode_t *ip,
150 struct inode *inode,
151 int flags)
152{
153 timespec_t *tvp;
154
155 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100156 * Atime updates for read() & friends are handled lazily now, and
157 * explicit updates must go through xfs_ichgtime()
158 */
159 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
160
161 /*
Nathan Scott4aeb6642005-11-02 11:43:58 +1100162 * We're not supposed to change timestamps in readonly-mounted
163 * filesystems. Throw it away if anyone asks us.
164 */
165 if (unlikely(IS_RDONLY(inode)))
166 return;
167
Nathan Scott4aeb6642005-11-02 11:43:58 +1100168 if (flags & XFS_ICHGTIME_MOD) {
169 tvp = &inode->i_mtime;
170 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
171 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
172 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100173 if (flags & XFS_ICHGTIME_CHG) {
174 tvp = &inode->i_ctime;
175 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
176 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
177 }
178
179 /*
180 * We update the i_update_core field _after_ changing
181 * the timestamps in order to coordinate properly with
182 * xfs_iflush() so that we don't lose timestamp updates.
183 * This keeps us from having to hold the inode lock
184 * while doing this. We use the SYNCHRONIZE macro to
185 * ensure that the compiler does not reorder the update
186 * of i_update_core above the timestamp updates above.
187 */
188 SYNCHRONIZE();
189 ip->i_update_core = 1;
190 if (!(inode->i_state & I_LOCK))
191 mark_inode_dirty_sync(inode);
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/*
196 * Pull the link count and size up from the xfs inode to the linux inode
197 */
198STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100199xfs_validate_fields(
Nathan Scott220b5282006-03-14 13:33:36 +1100200 struct inode *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +1000201 bhv_vattr_t *vattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Nathan Scott220b5282006-03-14 13:33:36 +1100203 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000204 if (!bhv_vop_getattr(vn_from_inode(ip), vattr, ATTR_LAZY, NULL)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100205 ip->i_nlink = vattr->va_nlink;
206 ip->i_blocks = vattr->va_nblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Nathan Scott220b5282006-03-14 13:33:36 +1100208 /* we're under i_sem so i_size can't change under us */
209 if (i_size_read(ip) != vattr->va_size)
210 i_size_write(ip, vattr->va_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212}
213
214/*
Nathan Scott446ada42006-01-11 15:35:44 +1100215 * Hook in SELinux. This is not quite correct yet, what we really need
216 * here (as we do for default ACLs) is a mechanism by which creation of
217 * these attrs can be journalled at inode creation time (along with the
218 * inode, of course, such that log replay can't cause these to be lost).
219 */
220STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100221xfs_init_security(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000222 bhv_vnode_t *vp,
Nathan Scott446ada42006-01-11 15:35:44 +1100223 struct inode *dir)
224{
Nathan Scottec86dc02006-03-17 17:25:36 +1100225 struct inode *ip = vn_to_inode(vp);
Nathan Scott446ada42006-01-11 15:35:44 +1100226 size_t length;
227 void *value;
228 char *name;
229 int error;
230
231 error = security_inode_init_security(ip, dir, &name, &value, &length);
232 if (error) {
233 if (error == -EOPNOTSUPP)
234 return 0;
235 return -error;
236 }
237
Nathan Scott67fcaa72006-06-09 17:00:52 +1000238 error = bhv_vop_attr_set(vp, name, value, length, ATTR_SECURE, NULL);
Nathan Scott446ada42006-01-11 15:35:44 +1100239 if (!error)
240 VMODIFY(vp);
241
242 kfree(name);
243 kfree(value);
244 return error;
245}
246
247/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * 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 */
253STATIC inline int
Nathan Scott416c6d52006-03-14 14:00:51 +1100254xfs_has_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 return (task->fs != init_task.fs);
257}
258
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100259STATIC inline void
Nathan Scott416c6d52006-03-14 14:00:51 +1100260xfs_cleanup_inode(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000261 bhv_vnode_t *dvp,
262 bhv_vnode_t *vp,
Nathan Scott220b5282006-03-14 13:33:36 +1100263 struct dentry *dentry,
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100264 int mode)
265{
266 struct dentry teardown = {};
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100267
268 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100269 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100270 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100271 * ENOSPC can hit here, among other things.
272 */
Nathan Scottec86dc02006-03-17 17:25:36 +1100273 teardown.d_inode = vn_to_inode(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100274 teardown.d_name = dentry->d_name;
275
276 if (S_ISDIR(mode))
Nathan Scott67fcaa72006-06-09 17:00:52 +1000277 bhv_vop_rmdir(dvp, &teardown, NULL);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100278 else
Nathan Scott67fcaa72006-06-09 17:00:52 +1000279 bhv_vop_remove(dvp, &teardown, NULL);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100280 VN_RELE(vp);
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100284xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 struct inode *dir,
286 struct dentry *dentry,
287 int mode,
288 dev_t rdev)
289{
290 struct inode *ip;
Nathan Scott8285fb52006-06-09 17:07:12 +1000291 bhv_vattr_t vattr = { 0 };
Nathan Scott67fcaa72006-06-09 17:00:52 +1000292 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 xfs_acl_t *default_acl = NULL;
294 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
295 int error;
296
297 /*
298 * Irix uses Missed'em'V split, but doesn't want to see
299 * the upper 5 bits of (14bit) major.
300 */
Nathan Scott220b5282006-03-14 13:33:36 +1100301 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -EINVAL;
303
Nathan Scott220b5282006-03-14 13:33:36 +1100304 if (unlikely(test_default_acl && test_default_acl(dvp))) {
305 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
309 _ACL_FREE(default_acl);
310 default_acl = NULL;
311 }
312 }
313
Nathan Scott416c6d52006-03-14 14:00:51 +1100314 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 mode &= ~current->fs->umask;
316
Nathan Scott524fbf52006-03-14 14:07:53 +1100317 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
318 vattr.va_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 switch (mode & S_IFMT) {
321 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
Nathan Scott524fbf52006-03-14 14:07:53 +1100322 vattr.va_rdev = sysv_encode_dev(rdev);
323 vattr.va_mask |= XFS_AT_RDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*FALLTHROUGH*/
325 case S_IFREG:
Nathan Scott67fcaa72006-06-09 17:00:52 +1000326 error = bhv_vop_create(dvp, dentry, &vattr, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328 case S_IFDIR:
Nathan Scott67fcaa72006-06-09 17:00:52 +1000329 error = bhv_vop_mkdir(dvp, dentry, &vattr, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 break;
331 default:
332 error = EINVAL;
333 break;
334 }
335
Nathan Scott220b5282006-03-14 13:33:36 +1100336 if (unlikely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100337 error = xfs_init_security(vp, dir);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100338 if (error)
Nathan Scott416c6d52006-03-14 14:00:51 +1100339 xfs_cleanup_inode(dvp, vp, dentry, mode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100340 }
Nathan Scott446ada42006-01-11 15:35:44 +1100341
Nathan Scott220b5282006-03-14 13:33:36 +1100342 if (unlikely(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (!error) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100344 error = _ACL_INHERIT(vp, &vattr, default_acl);
Nathan Scott220b5282006-03-14 13:33:36 +1100345 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 VMODIFY(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100347 else
Nathan Scott416c6d52006-03-14 14:00:51 +1100348 xfs_cleanup_inode(dvp, vp, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 _ACL_FREE(default_acl);
351 }
352
Nathan Scott220b5282006-03-14 13:33:36 +1100353 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 ASSERT(vp);
Nathan Scottec86dc02006-03-17 17:25:36 +1100355 ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (S_ISCHR(mode) || S_ISBLK(mode))
358 ip->i_rdev = rdev;
359 else if (S_ISDIR(mode))
Nathan Scott524fbf52006-03-14 14:07:53 +1100360 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 d_instantiate(dentry, ip);
Nathan Scott524fbf52006-03-14 14:07:53 +1100362 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 return -error;
365}
366
367STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100368xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct inode *dir,
370 struct dentry *dentry,
371 int mode,
372 struct nameidata *nd)
373{
Nathan Scott416c6d52006-03-14 14:00:51 +1100374 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100378xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct inode *dir,
380 struct dentry *dentry,
381 int mode)
382{
Nathan Scott416c6d52006-03-14 14:00:51 +1100383 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100387xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct inode *dir,
389 struct dentry *dentry,
390 struct nameidata *nd)
391{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000392 bhv_vnode_t *vp = vn_from_inode(dir), *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int error;
394
395 if (dentry->d_name.len >= MAXNAMELEN)
396 return ERR_PTR(-ENAMETOOLONG);
397
Nathan Scott67fcaa72006-06-09 17:00:52 +1000398 error = bhv_vop_lookup(vp, dentry, &cvp, 0, NULL, NULL);
399 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (unlikely(error != ENOENT))
401 return ERR_PTR(-error);
402 d_add(dentry, NULL);
403 return NULL;
404 }
405
Nathan Scottec86dc02006-03-17 17:25:36 +1100406 return d_splice_alias(vn_to_inode(cvp), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100410xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct dentry *old_dentry,
412 struct inode *dir,
413 struct dentry *dentry)
414{
415 struct inode *ip; /* inode of guy being linked to */
Nathan Scott67fcaa72006-06-09 17:00:52 +1000416 bhv_vnode_t *tdvp; /* target directory for new name/link */
417 bhv_vnode_t *vp; /* vp of name being linked */
Nathan Scott8285fb52006-06-09 17:07:12 +1000418 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int error;
420
421 ip = old_dentry->d_inode; /* inode being linked to */
Nathan Scottec86dc02006-03-17 17:25:36 +1100422 tdvp = vn_from_inode(dir);
423 vp = vn_from_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Nathan Scott97dfd702006-06-27 16:13:46 +1000425 VN_HOLD(vp);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000426 error = bhv_vop_link(tdvp, vp, dentry, NULL);
Nathan Scott97dfd702006-06-27 16:13:46 +1000427 if (unlikely(error)) {
428 VN_RELE(vp);
429 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 VMODIFY(tdvp);
Nathan Scott524fbf52006-03-14 14:07:53 +1100431 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 d_instantiate(dentry, ip);
433 }
434 return -error;
435}
436
437STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100438xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct inode *dir,
440 struct dentry *dentry)
441{
442 struct inode *inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000443 bhv_vnode_t *dvp; /* directory containing name to remove */
Nathan Scott8285fb52006-06-09 17:07:12 +1000444 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int error;
446
447 inode = dentry->d_inode;
Nathan Scottec86dc02006-03-17 17:25:36 +1100448 dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Nathan Scott67fcaa72006-06-09 17:00:52 +1000450 error = bhv_vop_remove(dvp, dentry, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100451 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100452 xfs_validate_fields(dir, &vattr); /* size needs update */
453 xfs_validate_fields(inode, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return -error;
456}
457
458STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100459xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct inode *dir,
461 struct dentry *dentry,
462 const char *symname)
463{
464 struct inode *ip;
Nathan Scott8285fb52006-06-09 17:07:12 +1000465 bhv_vattr_t va = { 0 };
Nathan Scott67fcaa72006-06-09 17:00:52 +1000466 bhv_vnode_t *dvp; /* directory containing name of symlink */
467 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int error;
469
Nathan Scottec86dc02006-03-17 17:25:36 +1100470 dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 cvp = NULL;
472
Nathan Scott67fcaa72006-06-09 17:00:52 +1000473 va.va_mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000474 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000475 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Nathan Scott67fcaa72006-06-09 17:00:52 +1000477 error = bhv_vop_symlink(dvp, dentry, &va, (char *)symname, &cvp, NULL);
Nathan Scott54245702006-01-16 15:54:05 +1100478 if (likely(!error && cvp)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100479 error = xfs_init_security(cvp, dir);
Nathan Scott54245702006-01-16 15:54:05 +1100480 if (likely(!error)) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100481 ip = vn_to_inode(cvp);
Nathan Scott54245702006-01-16 15:54:05 +1100482 d_instantiate(dentry, ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000483 xfs_validate_fields(dir, &va);
484 xfs_validate_fields(ip, &va);
Nathan Scottce9d37c2006-03-17 17:26:34 +1100485 } else {
486 xfs_cleanup_inode(dvp, cvp, dentry, 0);
Nathan Scott54245702006-01-16 15:54:05 +1100487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 return -error;
490}
491
492STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100493xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct inode *dir,
495 struct dentry *dentry)
496{
497 struct inode *inode = dentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000498 bhv_vnode_t *dvp = vn_from_inode(dir);
Nathan Scott8285fb52006-06-09 17:07:12 +1000499 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 int error;
501
Nathan Scott67fcaa72006-06-09 17:00:52 +1000502 error = bhv_vop_rmdir(dvp, dentry, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100503 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100504 xfs_validate_fields(inode, &vattr);
505 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 return -error;
508}
509
510STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100511xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct inode *odir,
513 struct dentry *odentry,
514 struct inode *ndir,
515 struct dentry *ndentry)
516{
517 struct inode *new_inode = ndentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000518 bhv_vnode_t *fvp; /* from directory */
519 bhv_vnode_t *tvp; /* target directory */
Nathan Scott8285fb52006-06-09 17:07:12 +1000520 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int error;
522
Nathan Scottec86dc02006-03-17 17:25:36 +1100523 fvp = vn_from_inode(odir);
524 tvp = vn_from_inode(ndir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Nathan Scott67fcaa72006-06-09 17:00:52 +1000526 error = bhv_vop_rename(fvp, odentry, tvp, ndentry, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100527 if (likely(!error)) {
528 if (new_inode)
Nathan Scott524fbf52006-03-14 14:07:53 +1100529 xfs_validate_fields(new_inode, &vattr);
530 xfs_validate_fields(odir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100531 if (ndir != odir)
Nathan Scott524fbf52006-03-14 14:07:53 +1100532 xfs_validate_fields(ndir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100533 }
Nathan Scott220b5282006-03-14 13:33:36 +1100534 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537/*
538 * careful here - this function can get called recursively, so
539 * we need to be very careful about how much stack we use.
540 * uio is kmalloced for this reason...
541 */
Al Viro008b1502005-08-20 00:17:39 +0100542STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100543xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct dentry *dentry,
545 struct nameidata *nd)
546{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000547 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 uio_t *uio;
549 iovec_t iov;
550 int error;
551 char *link;
552
553 ASSERT(dentry);
554 ASSERT(nd);
555
Nathan Scott0d1335b2006-01-12 10:32:51 +1100556 link = (char *)kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (!link) {
558 nd_set_link(nd, ERR_PTR(-ENOMEM));
Al Viro008b1502005-08-20 00:17:39 +0100559 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
563 if (!uio) {
564 kfree(link);
565 nd_set_link(nd, ERR_PTR(-ENOMEM));
Al Viro008b1502005-08-20 00:17:39 +0100566 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Nathan Scottec86dc02006-03-17 17:25:36 +1100569 vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 iov.iov_base = link;
Nathan Scott0d1335b2006-01-12 10:32:51 +1100572 iov.iov_len = MAXPATHLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 uio->uio_iov = &iov;
575 uio->uio_offset = 0;
576 uio->uio_segflg = UIO_SYSSPACE;
Nathan Scott0d1335b2006-01-12 10:32:51 +1100577 uio->uio_resid = MAXPATHLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 uio->uio_iovcnt = 1;
579
Nathan Scott67fcaa72006-06-09 17:00:52 +1000580 error = bhv_vop_readlink(vp, uio, 0, NULL);
581 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 kfree(link);
583 link = ERR_PTR(-error);
584 } else {
Nathan Scott0d1335b2006-01-12 10:32:51 +1100585 link[MAXPATHLEN - uio->uio_resid] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 kfree(uio);
588
589 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100590 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Nathan Scottcde410a2005-09-05 11:47:01 +1000593STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100594xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000595 struct dentry *dentry,
596 struct nameidata *nd,
597 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Nathan Scottcde410a2005-09-05 11:47:01 +1000599 char *s = nd_get_link(nd);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!IS_ERR(s))
602 kfree(s);
603}
604
605#ifdef CONFIG_XFS_POSIX_ACL
606STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100607xfs_vn_permission(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 struct inode *inode,
609 int mode,
610 struct nameidata *nd)
611{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000612 return -bhv_vop_access(vn_from_inode(inode), mode << 6, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100615#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616#endif
617
618STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100619xfs_vn_getattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct vfsmount *mnt,
621 struct dentry *dentry,
622 struct kstat *stat)
623{
624 struct inode *inode = dentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000625 bhv_vnode_t *vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int error = 0;
627
628 if (unlikely(vp->v_flag & VMODIFIED))
629 error = vn_revalidate(vp);
630 if (!error)
631 generic_fillattr(inode, stat);
Nathan Scottb76963f2006-06-09 14:58:20 +1000632 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100636xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct dentry *dentry,
638 struct iattr *attr)
639{
640 struct inode *inode = dentry->d_inode;
641 unsigned int ia_valid = attr->ia_valid;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000642 bhv_vnode_t *vp = vn_from_inode(inode);
Nathan Scott8285fb52006-06-09 17:07:12 +1000643 bhv_vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int flags = 0;
645 int error;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (ia_valid & ATTR_UID) {
648 vattr.va_mask |= XFS_AT_UID;
649 vattr.va_uid = attr->ia_uid;
650 }
651 if (ia_valid & ATTR_GID) {
652 vattr.va_mask |= XFS_AT_GID;
653 vattr.va_gid = attr->ia_gid;
654 }
655 if (ia_valid & ATTR_SIZE) {
656 vattr.va_mask |= XFS_AT_SIZE;
657 vattr.va_size = attr->ia_size;
658 }
659 if (ia_valid & ATTR_ATIME) {
660 vattr.va_mask |= XFS_AT_ATIME;
661 vattr.va_atime = attr->ia_atime;
Nathan Scott8c0b5112006-04-11 15:12:45 +1000662 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664 if (ia_valid & ATTR_MTIME) {
665 vattr.va_mask |= XFS_AT_MTIME;
666 vattr.va_mtime = attr->ia_mtime;
667 }
668 if (ia_valid & ATTR_CTIME) {
669 vattr.va_mask |= XFS_AT_CTIME;
670 vattr.va_ctime = attr->ia_ctime;
671 }
672 if (ia_valid & ATTR_MODE) {
673 vattr.va_mask |= XFS_AT_MODE;
674 vattr.va_mode = attr->ia_mode;
675 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
676 inode->i_mode &= ~S_ISGID;
677 }
678
679 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
680 flags |= ATTR_UTIME;
681#ifdef ATTR_NO_BLOCK
682 if ((ia_valid & ATTR_NO_BLOCK))
683 flags |= ATTR_NONBLOCK;
684#endif
685
Nathan Scott67fcaa72006-06-09 17:00:52 +1000686 error = bhv_vop_setattr(vp, &vattr, flags, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100687 if (likely(!error))
688 __vn_revalidate(vp, &vattr);
689 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100693xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct inode *inode)
695{
Nathan Scottc2536662006-03-29 10:44:40 +1000696 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100700xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct dentry *dentry,
702 const char *name,
703 const void *data,
704 size_t size,
705 int flags)
706{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000707 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 char *attr = (char *)name;
709 attrnames_t *namesp;
710 int xflags = 0;
711 int error;
712
713 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
714 if (!namesp)
715 return -EOPNOTSUPP;
716 attr += namesp->attr_namelen;
717 error = namesp->attr_capable(vp, NULL);
718 if (error)
719 return error;
720
721 /* Convert Linux syscall to XFS internal ATTR flags */
722 if (flags & XATTR_CREATE)
723 xflags |= ATTR_CREATE;
724 if (flags & XATTR_REPLACE)
725 xflags |= ATTR_REPLACE;
726 xflags |= namesp->attr_flag;
727 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
728}
729
730STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100731xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct dentry *dentry,
733 const char *name,
734 void *data,
735 size_t size)
736{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000737 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 char *attr = (char *)name;
739 attrnames_t *namesp;
740 int xflags = 0;
741 ssize_t error;
742
743 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
744 if (!namesp)
745 return -EOPNOTSUPP;
746 attr += namesp->attr_namelen;
747 error = namesp->attr_capable(vp, NULL);
748 if (error)
749 return error;
750
751 /* Convert Linux syscall to XFS internal ATTR flags */
752 if (!size) {
753 xflags |= ATTR_KERNOVAL;
754 data = NULL;
755 }
756 xflags |= namesp->attr_flag;
757 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
758}
759
760STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100761xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct dentry *dentry,
763 char *data,
764 size_t size)
765{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000766 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int error, xflags = ATTR_KERNAMELS;
768 ssize_t result;
769
770 if (!size)
771 xflags |= ATTR_KERNOVAL;
772 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
773
774 error = attr_generic_list(vp, data, size, xflags, &result);
775 if (error < 0)
776 return error;
777 return result;
778}
779
780STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100781xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct dentry *dentry,
783 const char *name)
784{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000785 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 char *attr = (char *)name;
787 attrnames_t *namesp;
788 int xflags = 0;
789 int error;
790
791 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
792 if (!namesp)
793 return -EOPNOTSUPP;
794 attr += namesp->attr_namelen;
795 error = namesp->attr_capable(vp, NULL);
796 if (error)
797 return error;
798 xflags |= namesp->attr_flag;
799 return namesp->attr_remove(vp, attr, xflags);
800}
801
802
Nathan Scott416c6d52006-03-14 14:00:51 +1100803struct inode_operations xfs_inode_operations = {
804 .permission = xfs_vn_permission,
805 .truncate = xfs_vn_truncate,
806 .getattr = xfs_vn_getattr,
807 .setattr = xfs_vn_setattr,
808 .setxattr = xfs_vn_setxattr,
809 .getxattr = xfs_vn_getxattr,
810 .listxattr = xfs_vn_listxattr,
811 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812};
813
Nathan Scott416c6d52006-03-14 14:00:51 +1100814struct inode_operations xfs_dir_inode_operations = {
815 .create = xfs_vn_create,
816 .lookup = xfs_vn_lookup,
817 .link = xfs_vn_link,
818 .unlink = xfs_vn_unlink,
819 .symlink = xfs_vn_symlink,
820 .mkdir = xfs_vn_mkdir,
821 .rmdir = xfs_vn_rmdir,
822 .mknod = xfs_vn_mknod,
823 .rename = xfs_vn_rename,
824 .permission = xfs_vn_permission,
825 .getattr = xfs_vn_getattr,
826 .setattr = xfs_vn_setattr,
827 .setxattr = xfs_vn_setxattr,
828 .getxattr = xfs_vn_getxattr,
829 .listxattr = xfs_vn_listxattr,
830 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831};
832
Nathan Scott416c6d52006-03-14 14:00:51 +1100833struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100835 .follow_link = xfs_vn_follow_link,
836 .put_link = xfs_vn_put_link,
837 .permission = xfs_vn_permission,
838 .getattr = xfs_vn_getattr,
839 .setattr = xfs_vn_setattr,
840 .setxattr = xfs_vn_setxattr,
841 .getxattr = xfs_vn_getxattr,
842 .listxattr = xfs_vn_listxattr,
843 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844};