blob: 9c4d99709705e6ee4a817db69c6289b7b829fece [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) {
Christoph Hellwig0a74cd12007-08-29 11:53:12 +100068 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 Hellwig42fe2b12006-01-11 15:35:17 +110070 }
71}
72
73/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110074 * Change the requested timestamp in the given inode.
75 * We don't lock across timestamp updates, and we don't log them but
76 * we do record the fact that there is dirty information in core.
77 *
78 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
79 * with XFS_ICHGTIME_ACC to be sure that access time
80 * update will take. Calling first with XFS_ICHGTIME_ACC
81 * and then XFS_ICHGTIME_MOD may fail to modify the access
82 * timestamp if the filesystem is mounted noacctm.
83 */
84void
85xfs_ichgtime(
86 xfs_inode_t *ip,
87 int flags)
88{
Nathan Scottec86dc02006-03-17 17:25:36 +110089 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Nathan Scott4aeb6642005-11-02 11:43:58 +110090 timespec_t tv;
91
Nathan Scott4aeb6642005-11-02 11:43:58 +110092 nanotime(&tv);
93 if (flags & XFS_ICHGTIME_MOD) {
94 inode->i_mtime = tv;
95 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
96 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
97 }
98 if (flags & XFS_ICHGTIME_ACC) {
99 inode->i_atime = tv;
100 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
101 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
102 }
103 if (flags & XFS_ICHGTIME_CHG) {
104 inode->i_ctime = tv;
105 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
106 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
107 }
108
109 /*
110 * We update the i_update_core field _after_ changing
111 * the timestamps in order to coordinate properly with
112 * xfs_iflush() so that we don't lose timestamp updates.
113 * This keeps us from having to hold the inode lock
114 * while doing this. We use the SYNCHRONIZE macro to
115 * ensure that the compiler does not reorder the update
116 * of i_update_core above the timestamp updates above.
117 */
118 SYNCHRONIZE();
119 ip->i_update_core = 1;
120 if (!(inode->i_state & I_LOCK))
121 mark_inode_dirty_sync(inode);
122}
123
124/*
125 * Variant on the above which avoids querying the system clock
126 * in situations where we know the Linux inode timestamps have
127 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100128 */
129void
130xfs_ichgtime_fast(
131 xfs_inode_t *ip,
132 struct inode *inode,
133 int flags)
134{
135 timespec_t *tvp;
136
137 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100138 * Atime updates for read() & friends are handled lazily now, and
139 * explicit updates must go through xfs_ichgtime()
140 */
141 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
142
143 /*
Nathan Scott4aeb6642005-11-02 11:43:58 +1100144 * We're not supposed to change timestamps in readonly-mounted
145 * filesystems. Throw it away if anyone asks us.
146 */
147 if (unlikely(IS_RDONLY(inode)))
148 return;
149
Nathan Scott4aeb6642005-11-02 11:43:58 +1100150 if (flags & XFS_ICHGTIME_MOD) {
151 tvp = &inode->i_mtime;
152 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
153 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
154 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100155 if (flags & XFS_ICHGTIME_CHG) {
156 tvp = &inode->i_ctime;
157 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
158 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
159 }
160
161 /*
162 * We update the i_update_core field _after_ changing
163 * the timestamps in order to coordinate properly with
164 * xfs_iflush() so that we don't lose timestamp updates.
165 * This keeps us from having to hold the inode lock
166 * while doing this. We use the SYNCHRONIZE macro to
167 * ensure that the compiler does not reorder the update
168 * of i_update_core above the timestamp updates above.
169 */
170 SYNCHRONIZE();
171 ip->i_update_core = 1;
172 if (!(inode->i_state & I_LOCK))
173 mark_inode_dirty_sync(inode);
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177/*
178 * Pull the link count and size up from the xfs inode to the linux inode
179 */
180STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100181xfs_validate_fields(
Nathan Scott220b5282006-03-14 13:33:36 +1100182 struct inode *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +1000183 bhv_vattr_t *vattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Nathan Scott220b5282006-03-14 13:33:36 +1100185 vattr->va_mask = XFS_AT_NLINK|XFS_AT_SIZE|XFS_AT_NBLOCKS;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000186 if (!xfs_getattr(XFS_I(ip), vattr, ATTR_LAZY)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100187 ip->i_nlink = vattr->va_nlink;
188 ip->i_blocks = vattr->va_nblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Nathan Scott220b5282006-03-14 13:33:36 +1100190 /* we're under i_sem so i_size can't change under us */
191 if (i_size_read(ip) != vattr->va_size)
192 i_size_write(ip, vattr->va_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194}
195
196/*
Nathan Scott446ada42006-01-11 15:35:44 +1100197 * Hook in SELinux. This is not quite correct yet, what we really need
198 * here (as we do for default ACLs) is a mechanism by which creation of
199 * these attrs can be journalled at inode creation time (along with the
200 * inode, of course, such that log replay can't cause these to be lost).
201 */
202STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100203xfs_init_security(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000204 bhv_vnode_t *vp,
Nathan Scott446ada42006-01-11 15:35:44 +1100205 struct inode *dir)
206{
Nathan Scottec86dc02006-03-17 17:25:36 +1100207 struct inode *ip = vn_to_inode(vp);
Nathan Scott446ada42006-01-11 15:35:44 +1100208 size_t length;
209 void *value;
210 char *name;
211 int error;
212
213 error = security_inode_init_security(ip, dir, &name, &value, &length);
214 if (error) {
215 if (error == -EOPNOTSUPP)
216 return 0;
217 return -error;
218 }
219
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000220 error = xfs_attr_set(XFS_I(ip), name, value,
221 length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100222 if (!error)
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000223 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100224
225 kfree(name);
226 kfree(value);
227 return error;
228}
229
230/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * Determine whether a process has a valid fs_struct (kernel daemons
232 * like knfsd don't have an fs_struct).
233 *
234 * XXX(hch): nfsd is broken, better fix it instead.
235 */
David Chinner7989cb82007-02-10 18:34:56 +1100236STATIC_INLINE int
Nathan Scott416c6d52006-03-14 14:00:51 +1100237xfs_has_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 return (task->fs != init_task.fs);
240}
241
David Chinner7989cb82007-02-10 18:34:56 +1100242STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100243xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000244 struct inode *dir,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000245 bhv_vnode_t *vp,
Nathan Scott220b5282006-03-14 13:33:36 +1100246 struct dentry *dentry,
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100247 int mode)
248{
249 struct dentry teardown = {};
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100250
251 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100252 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100253 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100254 * ENOSPC can hit here, among other things.
255 */
Nathan Scottec86dc02006-03-17 17:25:36 +1100256 teardown.d_inode = vn_to_inode(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100257 teardown.d_name = dentry->d_name;
258
259 if (S_ISDIR(mode))
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000260 xfs_rmdir(XFS_I(dir), &teardown);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100261 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000262 xfs_remove(XFS_I(dir), &teardown);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100263 VN_RELE(vp);
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100267xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct inode *dir,
269 struct dentry *dentry,
270 int mode,
271 dev_t rdev)
272{
273 struct inode *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000274 bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 xfs_acl_t *default_acl = NULL;
276 attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS;
277 int error;
278
279 /*
280 * Irix uses Missed'em'V split, but doesn't want to see
281 * the upper 5 bits of (14bit) major.
282 */
Nathan Scott220b5282006-03-14 13:33:36 +1100283 if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -EINVAL;
285
Nathan Scott220b5282006-03-14 13:33:36 +1100286 if (unlikely(test_default_acl && test_default_acl(dvp))) {
287 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!_ACL_GET_DEFAULT(dvp, default_acl)) {
291 _ACL_FREE(default_acl);
292 default_acl = NULL;
293 }
294 }
295
Nathan Scott416c6d52006-03-14 14:00:51 +1100296 if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mode &= ~current->fs->umask;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 switch (mode & S_IFMT) {
300 case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000301 rdev = sysv_encode_dev(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 case S_IFREG:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000303 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 case S_IFDIR:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000306 error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 default:
309 error = EINVAL;
310 break;
311 }
312
Nathan Scott220b5282006-03-14 13:33:36 +1100313 if (unlikely(!error)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100314 error = xfs_init_security(vp, dir);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100315 if (error)
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000316 xfs_cleanup_inode(dir, vp, dentry, mode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100317 }
Nathan Scott446ada42006-01-11 15:35:44 +1100318
Nathan Scott220b5282006-03-14 13:33:36 +1100319 if (unlikely(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!error) {
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000321 error = _ACL_INHERIT(vp, mode, default_acl);
Nathan Scott220b5282006-03-14 13:33:36 +1100322 if (!error)
Christoph Hellwig0a74cd12007-08-29 11:53:12 +1000323 xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100324 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000325 xfs_cleanup_inode(dir, vp, dentry, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 _ACL_FREE(default_acl);
328 }
329
Nathan Scott220b5282006-03-14 13:33:36 +1100330 if (likely(!error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ASSERT(vp);
Nathan Scottec86dc02006-03-17 17:25:36 +1100332 ip = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (S_ISCHR(mode) || S_ISBLK(mode))
335 ip->i_rdev = rdev;
336 else if (S_ISDIR(mode))
Nathan Scott524fbf52006-03-14 14:07:53 +1100337 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 d_instantiate(dentry, ip);
Nathan Scott524fbf52006-03-14 14:07:53 +1100339 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 return -error;
342}
343
344STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100345xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct inode *dir,
347 struct dentry *dentry,
348 int mode,
349 struct nameidata *nd)
350{
Nathan Scott416c6d52006-03-14 14:00:51 +1100351 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100355xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct inode *dir,
357 struct dentry *dentry,
358 int mode)
359{
Nathan Scott416c6d52006-03-14 14:00:51 +1100360 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100364xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct inode *dir,
366 struct dentry *dentry,
367 struct nameidata *nd)
368{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000369 bhv_vnode_t *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int error;
371
372 if (dentry->d_name.len >= MAXNAMELEN)
373 return ERR_PTR(-ENAMETOOLONG);
374
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000375 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000376 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (unlikely(error != ENOENT))
378 return ERR_PTR(-error);
379 d_add(dentry, NULL);
380 return NULL;
381 }
382
Nathan Scottec86dc02006-03-17 17:25:36 +1100383 return d_splice_alias(vn_to_inode(cvp), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100387xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct dentry *old_dentry,
389 struct inode *dir,
390 struct dentry *dentry)
391{
392 struct inode *ip; /* inode of guy being linked to */
Nathan Scott67fcaa72006-06-09 17:00:52 +1000393 bhv_vnode_t *vp; /* vp of name being linked */
Nathan Scott8285fb52006-06-09 17:07:12 +1000394 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int error;
396
397 ip = old_dentry->d_inode; /* inode being linked to */
Nathan Scottec86dc02006-03-17 17:25:36 +1100398 vp = vn_from_inode(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Nathan Scott97dfd702006-06-27 16:13:46 +1000400 VN_HOLD(vp);
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000401 error = xfs_link(XFS_I(dir), vp, dentry);
Nathan Scott97dfd702006-06-27 16:13:46 +1000402 if (unlikely(error)) {
403 VN_RELE(vp);
404 } else {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000405 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
Nathan Scott524fbf52006-03-14 14:07:53 +1100406 xfs_validate_fields(ip, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 d_instantiate(dentry, ip);
408 }
409 return -error;
410}
411
412STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100413xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct inode *dir,
415 struct dentry *dentry)
416{
417 struct inode *inode;
Nathan Scott8285fb52006-06-09 17:07:12 +1000418 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int error;
420
421 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000423 error = xfs_remove(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100424 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100425 xfs_validate_fields(dir, &vattr); /* size needs update */
426 xfs_validate_fields(inode, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -error;
429}
430
431STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100432xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct inode *dir,
434 struct dentry *dentry,
435 const char *symname)
436{
437 struct inode *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000438 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000440 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 cvp = NULL;
443
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000444 mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000445 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000447 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
448 &cvp, NULL);
Nathan Scott54245702006-01-16 15:54:05 +1100449 if (likely(!error && cvp)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100450 error = xfs_init_security(cvp, dir);
Nathan Scott54245702006-01-16 15:54:05 +1100451 if (likely(!error)) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100452 ip = vn_to_inode(cvp);
Nathan Scott54245702006-01-16 15:54:05 +1100453 d_instantiate(dentry, ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000454 xfs_validate_fields(dir, &va);
455 xfs_validate_fields(ip, &va);
Nathan Scottce9d37c2006-03-17 17:26:34 +1100456 } else {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000457 xfs_cleanup_inode(dir, cvp, dentry, 0);
Nathan Scott54245702006-01-16 15:54:05 +1100458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 return -error;
461}
462
463STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100464xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct inode *dir,
466 struct dentry *dentry)
467{
468 struct inode *inode = dentry->d_inode;
Nathan Scott8285fb52006-06-09 17:07:12 +1000469 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int error;
471
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000472 error = xfs_rmdir(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100473 if (likely(!error)) {
Nathan Scott524fbf52006-03-14 14:07:53 +1100474 xfs_validate_fields(inode, &vattr);
475 xfs_validate_fields(dir, &vattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 return -error;
478}
479
480STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100481xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct inode *odir,
483 struct dentry *odentry,
484 struct inode *ndir,
485 struct dentry *ndentry)
486{
487 struct inode *new_inode = ndentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000488 bhv_vnode_t *tvp; /* target directory */
Nathan Scott8285fb52006-06-09 17:07:12 +1000489 bhv_vattr_t vattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int error;
491
Nathan Scottec86dc02006-03-17 17:25:36 +1100492 tvp = vn_from_inode(ndir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000494 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100495 if (likely(!error)) {
496 if (new_inode)
Nathan Scott524fbf52006-03-14 14:07:53 +1100497 xfs_validate_fields(new_inode, &vattr);
498 xfs_validate_fields(odir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100499 if (ndir != odir)
Nathan Scott524fbf52006-03-14 14:07:53 +1100500 xfs_validate_fields(ndir, &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100501 }
Nathan Scott220b5282006-03-14 13:33:36 +1100502 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*
506 * careful here - this function can get called recursively, so
507 * we need to be very careful about how much stack we use.
508 * uio is kmalloced for this reason...
509 */
Al Viro008b1502005-08-20 00:17:39 +0100510STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100511xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct dentry *dentry,
513 struct nameidata *nd)
514{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000516 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700518 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000519 if (!link)
520 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000522 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000523 if (unlikely(error))
524 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100527 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000528
529 out_kfree:
530 kfree(link);
531 out_err:
532 nd_set_link(nd, ERR_PTR(error));
533 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Nathan Scottcde410a2005-09-05 11:47:01 +1000536STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100537xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000538 struct dentry *dentry,
539 struct nameidata *nd,
540 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Nathan Scottcde410a2005-09-05 11:47:01 +1000542 char *s = nd_get_link(nd);
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (!IS_ERR(s))
545 kfree(s);
546}
547
548#ifdef CONFIG_XFS_POSIX_ACL
549STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100550xfs_vn_permission(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct inode *inode,
552 int mode,
553 struct nameidata *nd)
554{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000555 return -xfs_access(XFS_I(inode), mode << 6, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100558#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559#endif
560
561STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100562xfs_vn_getattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct vfsmount *mnt,
564 struct dentry *dentry,
565 struct kstat *stat)
566{
567 struct inode *inode = dentry->d_inode;
Nathan Scott69e23b92006-09-28 11:01:22 +1000568 bhv_vattr_t vattr = { .va_mask = XFS_AT_STAT };
569 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000571 error = xfs_getattr(XFS_I(inode), &vattr, ATTR_LAZY);
Nathan Scott69e23b92006-09-28 11:01:22 +1000572 if (likely(!error)) {
573 stat->size = i_size_read(inode);
574 stat->dev = inode->i_sb->s_dev;
575 stat->rdev = (vattr.va_rdev == 0) ? 0 :
576 MKDEV(sysv_major(vattr.va_rdev) & 0x1ff,
577 sysv_minor(vattr.va_rdev));
578 stat->mode = vattr.va_mode;
579 stat->nlink = vattr.va_nlink;
580 stat->uid = vattr.va_uid;
581 stat->gid = vattr.va_gid;
582 stat->ino = vattr.va_nodeid;
583 stat->atime = vattr.va_atime;
584 stat->mtime = vattr.va_mtime;
585 stat->ctime = vattr.va_ctime;
586 stat->blocks = vattr.va_nblocks;
587 stat->blksize = vattr.va_blocksize;
588 }
Nathan Scottb76963f2006-06-09 14:58:20 +1000589 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100593xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct dentry *dentry,
595 struct iattr *attr)
596{
597 struct inode *inode = dentry->d_inode;
598 unsigned int ia_valid = attr->ia_valid;
Nathan Scott8285fb52006-06-09 17:07:12 +1000599 bhv_vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int flags = 0;
601 int error;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (ia_valid & ATTR_UID) {
604 vattr.va_mask |= XFS_AT_UID;
605 vattr.va_uid = attr->ia_uid;
606 }
607 if (ia_valid & ATTR_GID) {
608 vattr.va_mask |= XFS_AT_GID;
609 vattr.va_gid = attr->ia_gid;
610 }
611 if (ia_valid & ATTR_SIZE) {
612 vattr.va_mask |= XFS_AT_SIZE;
613 vattr.va_size = attr->ia_size;
614 }
615 if (ia_valid & ATTR_ATIME) {
616 vattr.va_mask |= XFS_AT_ATIME;
617 vattr.va_atime = attr->ia_atime;
Nathan Scott8c0b5112006-04-11 15:12:45 +1000618 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 if (ia_valid & ATTR_MTIME) {
621 vattr.va_mask |= XFS_AT_MTIME;
622 vattr.va_mtime = attr->ia_mtime;
623 }
624 if (ia_valid & ATTR_CTIME) {
625 vattr.va_mask |= XFS_AT_CTIME;
626 vattr.va_ctime = attr->ia_ctime;
627 }
628 if (ia_valid & ATTR_MODE) {
629 vattr.va_mask |= XFS_AT_MODE;
630 vattr.va_mode = attr->ia_mode;
631 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
632 inode->i_mode &= ~S_ISGID;
633 }
634
635 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
636 flags |= ATTR_UTIME;
637#ifdef ATTR_NO_BLOCK
638 if ((ia_valid & ATTR_NO_BLOCK))
639 flags |= ATTR_NONBLOCK;
640#endif
641
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000642 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100643 if (likely(!error))
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000644 __vn_revalidate(vn_from_inode(inode), &vattr);
Nathan Scott220b5282006-03-14 13:33:36 +1100645 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100649xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct inode *inode)
651{
Nathan Scottc2536662006-03-29 10:44:40 +1000652 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100656xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct dentry *dentry,
658 const char *name,
659 const void *data,
660 size_t size,
661 int flags)
662{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000663 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 char *attr = (char *)name;
665 attrnames_t *namesp;
666 int xflags = 0;
667 int error;
668
669 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
670 if (!namesp)
671 return -EOPNOTSUPP;
672 attr += namesp->attr_namelen;
673 error = namesp->attr_capable(vp, NULL);
674 if (error)
675 return error;
676
677 /* Convert Linux syscall to XFS internal ATTR flags */
678 if (flags & XATTR_CREATE)
679 xflags |= ATTR_CREATE;
680 if (flags & XATTR_REPLACE)
681 xflags |= ATTR_REPLACE;
682 xflags |= namesp->attr_flag;
683 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
684}
685
686STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100687xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct dentry *dentry,
689 const char *name,
690 void *data,
691 size_t size)
692{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000693 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 char *attr = (char *)name;
695 attrnames_t *namesp;
696 int xflags = 0;
697 ssize_t error;
698
699 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
700 if (!namesp)
701 return -EOPNOTSUPP;
702 attr += namesp->attr_namelen;
703 error = namesp->attr_capable(vp, NULL);
704 if (error)
705 return error;
706
707 /* Convert Linux syscall to XFS internal ATTR flags */
708 if (!size) {
709 xflags |= ATTR_KERNOVAL;
710 data = NULL;
711 }
712 xflags |= namesp->attr_flag;
713 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
714}
715
716STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100717xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct dentry *dentry,
719 char *data,
720 size_t size)
721{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000722 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int error, xflags = ATTR_KERNAMELS;
724 ssize_t result;
725
726 if (!size)
727 xflags |= ATTR_KERNOVAL;
728 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
729
730 error = attr_generic_list(vp, data, size, xflags, &result);
731 if (error < 0)
732 return error;
733 return result;
734}
735
736STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100737xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct dentry *dentry,
739 const char *name)
740{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000741 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 char *attr = (char *)name;
743 attrnames_t *namesp;
744 int xflags = 0;
745 int error;
746
747 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
748 if (!namesp)
749 return -EOPNOTSUPP;
750 attr += namesp->attr_namelen;
751 error = namesp->attr_capable(vp, NULL);
752 if (error)
753 return error;
754 xflags |= namesp->attr_flag;
755 return namesp->attr_remove(vp, attr, xflags);
756}
757
758
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800759const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100760 .permission = xfs_vn_permission,
761 .truncate = xfs_vn_truncate,
762 .getattr = xfs_vn_getattr,
763 .setattr = xfs_vn_setattr,
764 .setxattr = xfs_vn_setxattr,
765 .getxattr = xfs_vn_getxattr,
766 .listxattr = xfs_vn_listxattr,
767 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768};
769
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800770const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100771 .create = xfs_vn_create,
772 .lookup = xfs_vn_lookup,
773 .link = xfs_vn_link,
774 .unlink = xfs_vn_unlink,
775 .symlink = xfs_vn_symlink,
776 .mkdir = xfs_vn_mkdir,
777 .rmdir = xfs_vn_rmdir,
778 .mknod = xfs_vn_mknod,
779 .rename = xfs_vn_rename,
780 .permission = xfs_vn_permission,
781 .getattr = xfs_vn_getattr,
782 .setattr = xfs_vn_setattr,
783 .setxattr = xfs_vn_setxattr,
784 .getxattr = xfs_vn_getxattr,
785 .listxattr = xfs_vn_listxattr,
786 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787};
788
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800789const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100791 .follow_link = xfs_vn_follow_link,
792 .put_link = xfs_vn_put_link,
793 .permission = xfs_vn_permission,
794 .getattr = xfs_vn_getattr,
795 .setattr = xfs_vn_setattr,
796 .setxattr = xfs_vn_setxattr,
797 .getxattr = xfs_vn_getxattr,
798 .listxattr = xfs_vn_listxattr,
799 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800};