blob: 98d5a7e13bd5126feba8e3875a16621294135dd9 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_attr.h"
47#include "xfs_buf_item.h"
48#include "xfs_utils.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100049#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080051#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/xattr.h>
53#include <linux/namei.h>
Nathan Scott446ada42006-01-11 15:35:44 +110054#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Nathan Scott4aeb6642005-11-02 11:43:58 +110056/*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110057 * Bring the atime in the XFS inode uptodate.
58 * Used before logging the inode to disk or when the Linux inode goes away.
59 */
60void
61xfs_synchronize_atime(
62 xfs_inode_t *ip)
63{
Nathan Scott67fcaa72006-06-09 17:00:52 +100064 bhv_vnode_t *vp;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110065
66 vp = XFS_ITOV_NULL(ip);
67 if (vp) {
68 struct inode *inode = &vp->v_inode;
69 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
70 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
71 }
72}
73
74/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110075 * Change the requested timestamp in the given inode.
76 * We don't lock across timestamp updates, and we don't log them but
77 * we do record the fact that there is dirty information in core.
78 *
79 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
80 * with XFS_ICHGTIME_ACC to be sure that access time
81 * update will take. Calling first with XFS_ICHGTIME_ACC
82 * and then XFS_ICHGTIME_MOD may fail to modify the access
83 * timestamp if the filesystem is mounted noacctm.
84 */
85void
86xfs_ichgtime(
87 xfs_inode_t *ip,
88 int flags)
89{
Nathan Scottec86dc02006-03-17 17:25:36 +110090 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Nathan Scott4aeb6642005-11-02 11:43:58 +110091 timespec_t tv;
92
Nathan Scott4aeb6642005-11-02 11:43:58 +110093 nanotime(&tv);
94 if (flags & XFS_ICHGTIME_MOD) {
95 inode->i_mtime = tv;
96 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
97 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
98 }
99 if (flags & XFS_ICHGTIME_ACC) {
100 inode->i_atime = tv;
101 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
102 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
103 }
104 if (flags & XFS_ICHGTIME_CHG) {
105 inode->i_ctime = tv;
106 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
107 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
108 }
109
110 /*
111 * We update the i_update_core field _after_ changing
112 * the timestamps in order to coordinate properly with
113 * xfs_iflush() so that we don't lose timestamp updates.
114 * This keeps us from having to hold the inode lock
115 * while doing this. We use the SYNCHRONIZE macro to
116 * ensure that the compiler does not reorder the update
117 * of i_update_core above the timestamp updates above.
118 */
119 SYNCHRONIZE();
120 ip->i_update_core = 1;
121 if (!(inode->i_state & I_LOCK))
122 mark_inode_dirty_sync(inode);
123}
124
125/*
126 * Variant on the above which avoids querying the system clock
127 * in situations where we know the Linux inode timestamps have
128 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100129 */
130void
131xfs_ichgtime_fast(
132 xfs_inode_t *ip,
133 struct inode *inode,
134 int flags)
135{
136 timespec_t *tvp;
137
138 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100139 * Atime updates for read() & friends are handled lazily now, and
140 * explicit updates must go through xfs_ichgtime()
141 */
142 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
143
144 /*
Nathan Scott4aeb6642005-11-02 11:43:58 +1100145 * We're not supposed to change timestamps in readonly-mounted
146 * filesystems. Throw it away if anyone asks us.
147 */
148 if (unlikely(IS_RDONLY(inode)))
149 return;
150
Nathan Scott4aeb6642005-11-02 11:43:58 +1100151 if (flags & XFS_ICHGTIME_MOD) {
152 tvp = &inode->i_mtime;
153 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
154 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
155 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100156 if (flags & XFS_ICHGTIME_CHG) {
157 tvp = &inode->i_ctime;
158 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
159 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
160 }
161
162 /*
163 * We update the i_update_core field _after_ changing
164 * the timestamps in order to coordinate properly with
165 * xfs_iflush() so that we don't lose timestamp updates.
166 * This keeps us from having to hold the inode lock
167 * while doing this. We use the SYNCHRONIZE macro to
168 * ensure that the compiler does not reorder the update
169 * of i_update_core above the timestamp updates above.
170 */
171 SYNCHRONIZE();
172 ip->i_update_core = 1;
173 if (!(inode->i_state & I_LOCK))
174 mark_inode_dirty_sync(inode);
175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/*
179 * Pull the link count and size up from the xfs inode to the linux inode
180 */
181STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100182xfs_validate_fields(
Nathan Scott220b5282006-03-14 13:33:36 +1100183 struct inode *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +1000184 bhv_vattr_t *vattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Nathan Scott220b5282006-03-14 13:33:36 +1100186 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000187 if (!xfs_getattr(XFS_I(ip), vattr, ATTR_LAZY)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100188 ip->i_nlink = vattr->va_nlink;
189 ip->i_blocks = vattr->va_nblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Nathan Scott220b5282006-03-14 13:33:36 +1100191 /* we're under i_sem so i_size can't change under us */
192 if (i_size_read(ip) != vattr->va_size)
193 i_size_write(ip, vattr->va_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195}
196
197/*
Nathan Scott446ada42006-01-11 15:35:44 +1100198 * Hook in SELinux. This is not quite correct yet, what we really need
199 * here (as we do for default ACLs) is a mechanism by which creation of
200 * these attrs can be journalled at inode creation time (along with the
201 * inode, of course, such that log replay can't cause these to be lost).
202 */
203STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100204xfs_init_security(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000205 bhv_vnode_t *vp,
Nathan Scott446ada42006-01-11 15:35:44 +1100206 struct inode *dir)
207{
Nathan Scottec86dc02006-03-17 17:25:36 +1100208 struct inode *ip = vn_to_inode(vp);
Nathan Scott446ada42006-01-11 15:35:44 +1100209 size_t length;
210 void *value;
211 char *name;
212 int error;
213
214 error = security_inode_init_security(ip, dir, &name, &value, &length);
215 if (error) {
216 if (error == -EOPNOTSUPP)
217 return 0;
218 return -error;
219 }
220
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000221 error = xfs_attr_set(XFS_I(ip), name, value,
222 length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100223 if (!error)
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000224 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100225
226 kfree(name);
227 kfree(value);
228 return error;
229}
230
231/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * Determine whether a process has a valid fs_struct (kernel daemons
233 * like knfsd don't have an fs_struct).
234 *
235 * XXX(hch): nfsd is broken, better fix it instead.
236 */
David Chinner7989cb82007-02-10 18:34:56 +1100237STATIC_INLINE int
Nathan Scott416c6d52006-03-14 14:00:51 +1100238xfs_has_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 return (task->fs != init_task.fs);
241}
242
David Chinner7989cb82007-02-10 18:34:56 +1100243STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100244xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000245 struct inode *dir,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000246 bhv_vnode_t *vp,
Nathan Scott220b5282006-03-14 13:33:36 +1100247 struct dentry *dentry,
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100248 int mode)
249{
250 struct dentry teardown = {};
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100251
252 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100253 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100254 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100255 * ENOSPC can hit here, among other things.
256 */
Nathan Scottec86dc02006-03-17 17:25:36 +1100257 teardown.d_inode = vn_to_inode(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100258 teardown.d_name = dentry->d_name;
259
260 if (S_ISDIR(mode))
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000261 xfs_rmdir(XFS_I(dir), &teardown);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100262 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000263 xfs_remove(XFS_I(dir), &teardown);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100264 VN_RELE(vp);
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100268xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct inode *dir,
270 struct dentry *dentry,
271 int mode,
272 dev_t rdev)
273{
274 struct inode *ip;
Nathan Scott8285fb52006-06-09 17:07:12 +1000275 bhv_vattr_t vattr = { 0 };
Nathan Scott67fcaa72006-06-09 17:00:52 +1000276 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 xfs_acl_t *default_acl = NULL;
278 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
279 int error;
280
281 /*
282 * Irix uses Missed'em'V split, but doesn't want to see
283 * the upper 5 bits of (14bit) major.
284 */
Nathan Scott220b5282006-03-14 13:33:36 +1100285 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return -EINVAL;
287
Nathan Scott220b5282006-03-14 13:33:36 +1100288 if (unlikely(test_default_acl && test_default_acl(dvp))) {
289 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
293 _ACL_FREE(default_acl);
294 default_acl = NULL;
295 }
296 }
297
Nathan Scott416c6d52006-03-14 14:00:51 +1100298 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 mode &= ~current->fs->umask;
300
Nathan Scott524fbf52006-03-14 14:07:53 +1100301 vattr.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
302 vattr.va_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 switch (mode & S_IFMT) {
305 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
Nathan Scott524fbf52006-03-14 14:07:53 +1100306 vattr.va_rdev = sysv_encode_dev(rdev);
307 vattr.va_mask |= XFS_AT_RDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /*FALLTHROUGH*/
309 case S_IFREG:
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000310 error = xfs_create(XFS_I(dir), dentry, &vattr, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312 case S_IFDIR:
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000313 error = xfs_mkdir(XFS_I(dir), dentry, &vattr, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
315 default:
316 error = EINVAL;
317 break;
318 }
319
Nathan Scott220b5282006-03-14 13:33:36 +1100320 if (unlikely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100321 error = xfs_init_security(vp, dir);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100322 if (error)
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000323 xfs_cleanup_inode(dir, vp, dentry, mode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100324 }
Nathan Scott446ada42006-01-11 15:35:44 +1100325
Nathan Scott220b5282006-03-14 13:33:36 +1100326 if (unlikely(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!error) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100328 error = _ACL_INHERIT(vp, &vattr, default_acl);
Nathan Scott220b5282006-03-14 13:33:36 +1100329 if (!error)
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000330 xfs_iflags_set(XFS_I(&vp->v_inode), XFS_IMODIFIED);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100331 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000332 xfs_cleanup_inode(dir, vp, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 _ACL_FREE(default_acl);
335 }
336
Nathan Scott220b5282006-03-14 13:33:36 +1100337 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ASSERT(vp);
Nathan Scottec86dc02006-03-17 17:25:36 +1100339 ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (S_ISCHR(mode) || S_ISBLK(mode))
342 ip->i_rdev = rdev;
343 else if (S_ISDIR(mode))
Nathan Scott524fbf52006-03-14 14:07:53 +1100344 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 d_instantiate(dentry, ip);
Nathan Scott524fbf52006-03-14 14:07:53 +1100346 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 return -error;
349}
350
351STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100352xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 struct inode *dir,
354 struct dentry *dentry,
355 int mode,
356 struct nameidata *nd)
357{
Nathan Scott416c6d52006-03-14 14:00:51 +1100358 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100362xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct inode *dir,
364 struct dentry *dentry,
365 int mode)
366{
Nathan Scott416c6d52006-03-14 14:00:51 +1100367 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100371xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct inode *dir,
373 struct dentry *dentry,
374 struct nameidata *nd)
375{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000376 bhv_vnode_t *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int error;
378
379 if (dentry->d_name.len >= MAXNAMELEN)
380 return ERR_PTR(-ENAMETOOLONG);
381
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000382 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000383 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (unlikely(error != ENOENT))
385 return ERR_PTR(-error);
386 d_add(dentry, NULL);
387 return NULL;
388 }
389
Nathan Scottec86dc02006-03-17 17:25:36 +1100390 return d_splice_alias(vn_to_inode(cvp), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100394xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct dentry *old_dentry,
396 struct inode *dir,
397 struct dentry *dentry)
398{
399 struct inode *ip; /* inode of guy being linked to */
Nathan Scott67fcaa72006-06-09 17:00:52 +1000400 bhv_vnode_t *vp; /* vp of name being linked */
Nathan Scott8285fb52006-06-09 17:07:12 +1000401 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int error;
403
404 ip = old_dentry->d_inode; /* inode being linked to */
Nathan Scottec86dc02006-03-17 17:25:36 +1100405 vp = vn_from_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Nathan Scott97dfd702006-06-27 16:13:46 +1000407 VN_HOLD(vp);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000408 error = xfs_link(XFS_I(dir), vp, dentry);
Nathan Scott97dfd702006-06-27 16:13:46 +1000409 if (unlikely(error)) {
410 VN_RELE(vp);
411 } else {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000412 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
Nathan Scott524fbf52006-03-14 14:07:53 +1100413 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 d_instantiate(dentry, ip);
415 }
416 return -error;
417}
418
419STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100420xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct inode *dir,
422 struct dentry *dentry)
423{
424 struct inode *inode;
Nathan Scott8285fb52006-06-09 17:07:12 +1000425 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int error;
427
428 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000430 error = xfs_remove(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100431 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100432 xfs_validate_fields(dir, &vattr); /* size needs update */
433 xfs_validate_fields(inode, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -error;
436}
437
438STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100439xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct inode *dir,
441 struct dentry *dentry,
442 const char *symname)
443{
444 struct inode *ip;
Nathan Scott8285fb52006-06-09 17:07:12 +1000445 bhv_vattr_t va = { 0 };
Nathan Scott67fcaa72006-06-09 17:00:52 +1000446 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 int error;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 cvp = NULL;
450
Nathan Scott67fcaa72006-06-09 17:00:52 +1000451 va.va_mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000452 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000453 va.va_mask = XFS_AT_TYPE|XFS_AT_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000455 error = xfs_symlink(XFS_I(dir), dentry, &va,
456 (char *)symname, &cvp, NULL);
Nathan Scott54245702006-01-16 15:54:05 +1100457 if (likely(!error && cvp)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100458 error = xfs_init_security(cvp, dir);
Nathan Scott54245702006-01-16 15:54:05 +1100459 if (likely(!error)) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100460 ip = vn_to_inode(cvp);
Nathan Scott54245702006-01-16 15:54:05 +1100461 d_instantiate(dentry, ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000462 xfs_validate_fields(dir, &va);
463 xfs_validate_fields(ip, &va);
Nathan Scottce9d37c2006-03-17 17:26:34 +1100464 } else {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000465 xfs_cleanup_inode(dir, cvp, dentry, 0);
Nathan Scott54245702006-01-16 15:54:05 +1100466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 return -error;
469}
470
471STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100472xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct inode *dir,
474 struct dentry *dentry)
475{
476 struct inode *inode = dentry->d_inode;
Nathan Scott8285fb52006-06-09 17:07:12 +1000477 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int error;
479
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000480 error = xfs_rmdir(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100481 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100482 xfs_validate_fields(inode, &vattr);
483 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 return -error;
486}
487
488STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100489xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct inode *odir,
491 struct dentry *odentry,
492 struct inode *ndir,
493 struct dentry *ndentry)
494{
495 struct inode *new_inode = ndentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000496 bhv_vnode_t *tvp; /* target directory */
Nathan Scott8285fb52006-06-09 17:07:12 +1000497 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int error;
499
Nathan Scottec86dc02006-03-17 17:25:36 +1100500 tvp = vn_from_inode(ndir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000502 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100503 if (likely(!error)) {
504 if (new_inode)
Nathan Scott524fbf52006-03-14 14:07:53 +1100505 xfs_validate_fields(new_inode, &vattr);
506 xfs_validate_fields(odir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100507 if (ndir != odir)
Nathan Scott524fbf52006-03-14 14:07:53 +1100508 xfs_validate_fields(ndir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100509 }
Nathan Scott220b5282006-03-14 13:33:36 +1100510 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/*
514 * careful here - this function can get called recursively, so
515 * we need to be very careful about how much stack we use.
516 * uio is kmalloced for this reason...
517 */
Al Viro008b1502005-08-20 00:17:39 +0100518STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100519xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct dentry *dentry,
521 struct nameidata *nd)
522{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000524 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700526 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000527 if (!link)
528 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000530 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000531 if (unlikely(error))
532 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100535 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000536
537 out_kfree:
538 kfree(link);
539 out_err:
540 nd_set_link(nd, ERR_PTR(error));
541 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Nathan Scottcde410a2005-09-05 11:47:01 +1000544STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100545xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000546 struct dentry *dentry,
547 struct nameidata *nd,
548 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Nathan Scottcde410a2005-09-05 11:47:01 +1000550 char *s = nd_get_link(nd);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (!IS_ERR(s))
553 kfree(s);
554}
555
556#ifdef CONFIG_XFS_POSIX_ACL
557STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100558xfs_vn_permission(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct inode *inode,
560 int mode,
561 struct nameidata *nd)
562{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000563 return -xfs_access(XFS_I(inode), mode << 6, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100566#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#endif
568
569STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100570xfs_vn_getattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct vfsmount *mnt,
572 struct dentry *dentry,
573 struct kstat *stat)
574{
575 struct inode *inode = dentry->d_inode;
Nathan Scott69e23b92006-09-28 11:01:22 +1000576 bhv_vattr_t vattr = { .va_mask = XFS_AT_STAT };
577 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000579 error = xfs_getattr(XFS_I(inode), &vattr, ATTR_LAZY);
Nathan Scott69e23b92006-09-28 11:01:22 +1000580 if (likely(!error)) {
581 stat->size = i_size_read(inode);
582 stat->dev = inode->i_sb->s_dev;
583 stat->rdev = (vattr.va_rdev == 0) ? 0 :
584 MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
585 sysv_minor(vattr.va_rdev));
586 stat->mode = vattr.va_mode;
587 stat->nlink = vattr.va_nlink;
588 stat->uid = vattr.va_uid;
589 stat->gid = vattr.va_gid;
590 stat->ino = vattr.va_nodeid;
591 stat->atime = vattr.va_atime;
592 stat->mtime = vattr.va_mtime;
593 stat->ctime = vattr.va_ctime;
594 stat->blocks = vattr.va_nblocks;
595 stat->blksize = vattr.va_blocksize;
596 }
Nathan Scottb76963f2006-06-09 14:58:20 +1000597 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100601xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct dentry *dentry,
603 struct iattr *attr)
604{
605 struct inode *inode = dentry->d_inode;
606 unsigned int ia_valid = attr->ia_valid;
Nathan Scott8285fb52006-06-09 17:07:12 +1000607 bhv_vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int flags = 0;
609 int error;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (ia_valid & ATTR_UID) {
612 vattr.va_mask |= XFS_AT_UID;
613 vattr.va_uid = attr->ia_uid;
614 }
615 if (ia_valid & ATTR_GID) {
616 vattr.va_mask |= XFS_AT_GID;
617 vattr.va_gid = attr->ia_gid;
618 }
619 if (ia_valid & ATTR_SIZE) {
620 vattr.va_mask |= XFS_AT_SIZE;
621 vattr.va_size = attr->ia_size;
622 }
623 if (ia_valid & ATTR_ATIME) {
624 vattr.va_mask |= XFS_AT_ATIME;
625 vattr.va_atime = attr->ia_atime;
Nathan Scott8c0b5112006-04-11 15:12:45 +1000626 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 if (ia_valid & ATTR_MTIME) {
629 vattr.va_mask |= XFS_AT_MTIME;
630 vattr.va_mtime = attr->ia_mtime;
631 }
632 if (ia_valid & ATTR_CTIME) {
633 vattr.va_mask |= XFS_AT_CTIME;
634 vattr.va_ctime = attr->ia_ctime;
635 }
636 if (ia_valid & ATTR_MODE) {
637 vattr.va_mask |= XFS_AT_MODE;
638 vattr.va_mode = attr->ia_mode;
639 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
640 inode->i_mode &= ~S_ISGID;
641 }
642
643 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
644 flags |= ATTR_UTIME;
645#ifdef ATTR_NO_BLOCK
646 if ((ia_valid & ATTR_NO_BLOCK))
647 flags |= ATTR_NONBLOCK;
648#endif
649
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000650 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100651 if (likely(!error))
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000652 __vn_revalidate(vn_from_inode(inode), &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100653 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100657xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct inode *inode)
659{
Nathan Scottc2536662006-03-29 10:44:40 +1000660 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100664xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 struct dentry *dentry,
666 const char *name,
667 const void *data,
668 size_t size,
669 int flags)
670{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000671 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 char *attr = (char *)name;
673 attrnames_t *namesp;
674 int xflags = 0;
675 int error;
676
677 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
678 if (!namesp)
679 return -EOPNOTSUPP;
680 attr += namesp->attr_namelen;
681 error = namesp->attr_capable(vp, NULL);
682 if (error)
683 return error;
684
685 /* Convert Linux syscall to XFS internal ATTR flags */
686 if (flags & XATTR_CREATE)
687 xflags |= ATTR_CREATE;
688 if (flags & XATTR_REPLACE)
689 xflags |= ATTR_REPLACE;
690 xflags |= namesp->attr_flag;
691 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
692}
693
694STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100695xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct dentry *dentry,
697 const char *name,
698 void *data,
699 size_t size)
700{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000701 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 char *attr = (char *)name;
703 attrnames_t *namesp;
704 int xflags = 0;
705 ssize_t error;
706
707 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
708 if (!namesp)
709 return -EOPNOTSUPP;
710 attr += namesp->attr_namelen;
711 error = namesp->attr_capable(vp, NULL);
712 if (error)
713 return error;
714
715 /* Convert Linux syscall to XFS internal ATTR flags */
716 if (!size) {
717 xflags |= ATTR_KERNOVAL;
718 data = NULL;
719 }
720 xflags |= namesp->attr_flag;
721 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
722}
723
724STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100725xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct dentry *dentry,
727 char *data,
728 size_t size)
729{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000730 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int error, xflags = ATTR_KERNAMELS;
732 ssize_t result;
733
734 if (!size)
735 xflags |= ATTR_KERNOVAL;
736 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
737
738 error = attr_generic_list(vp, data, size, xflags, &result);
739 if (error < 0)
740 return error;
741 return result;
742}
743
744STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100745xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct dentry *dentry,
747 const char *name)
748{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000749 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 char *attr = (char *)name;
751 attrnames_t *namesp;
752 int xflags = 0;
753 int error;
754
755 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
756 if (!namesp)
757 return -EOPNOTSUPP;
758 attr += namesp->attr_namelen;
759 error = namesp->attr_capable(vp, NULL);
760 if (error)
761 return error;
762 xflags |= namesp->attr_flag;
763 return namesp->attr_remove(vp, attr, xflags);
764}
765
766
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800767const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100768 .permission = xfs_vn_permission,
769 .truncate = xfs_vn_truncate,
770 .getattr = xfs_vn_getattr,
771 .setattr = xfs_vn_setattr,
772 .setxattr = xfs_vn_setxattr,
773 .getxattr = xfs_vn_getxattr,
774 .listxattr = xfs_vn_listxattr,
775 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776};
777
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800778const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100779 .create = xfs_vn_create,
780 .lookup = xfs_vn_lookup,
781 .link = xfs_vn_link,
782 .unlink = xfs_vn_unlink,
783 .symlink = xfs_vn_symlink,
784 .mkdir = xfs_vn_mkdir,
785 .rmdir = xfs_vn_rmdir,
786 .mknod = xfs_vn_mknod,
787 .rename = xfs_vn_rename,
788 .permission = xfs_vn_permission,
789 .getattr = xfs_vn_getattr,
790 .setattr = xfs_vn_setattr,
791 .setxattr = xfs_vn_setxattr,
792 .getxattr = xfs_vn_getxattr,
793 .listxattr = xfs_vn_listxattr,
794 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
796
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800797const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100799 .follow_link = xfs_vn_follow_link,
800 .put_link = xfs_vn_put_link,
801 .permission = xfs_vn_permission,
802 .getattr = xfs_vn_getattr,
803 .setattr = xfs_vn_setattr,
804 .setxattr = xfs_vn_setxattr,
805 .getxattr = xfs_vn_getxattr,
806 .listxattr = xfs_vn_listxattr,
807 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808};