blob: 1df48209d60ad9945d19de4e3dc2e7bddd12a947 [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>
David Chinner3ed65262007-11-23 16:29:25 +110055#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Nathan Scott4aeb6642005-11-02 11:43:58 +110057/*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110058 * Bring the atime in the XFS inode uptodate.
59 * Used before logging the inode to disk or when the Linux inode goes away.
60 */
61void
62xfs_synchronize_atime(
63 xfs_inode_t *ip)
64{
Nathan Scott67fcaa72006-06-09 17:00:52 +100065 bhv_vnode_t *vp;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110066
67 vp = XFS_ITOV_NULL(ip);
68 if (vp) {
Christoph Hellwig0a74cd12007-08-29 11:53:12 +100069 ip->i_d.di_atime.t_sec = (__int32_t)vp->i_atime.tv_sec;
70 ip->i_d.di_atime.t_nsec = (__int32_t)vp->i_atime.tv_nsec;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110071 }
72}
73
74/*
David Chinner5d51eff2007-11-23 16:29:18 +110075 * If the linux inode exists, mark it dirty.
76 * Used when commiting a dirty inode into a transaction so that
77 * the inode will get written back by the linux code
78 */
79void
80xfs_mark_inode_dirty_sync(
81 xfs_inode_t *ip)
82{
83 bhv_vnode_t *vp;
84
85 vp = XFS_ITOV_NULL(ip);
86 if (vp)
87 mark_inode_dirty_sync(vn_to_inode(vp));
88}
89
90/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110091 * Change the requested timestamp in the given inode.
92 * We don't lock across timestamp updates, and we don't log them but
93 * we do record the fact that there is dirty information in core.
94 *
95 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
96 * with XFS_ICHGTIME_ACC to be sure that access time
97 * update will take. Calling first with XFS_ICHGTIME_ACC
98 * and then XFS_ICHGTIME_MOD may fail to modify the access
99 * timestamp if the filesystem is mounted noacctm.
100 */
101void
102xfs_ichgtime(
103 xfs_inode_t *ip,
104 int flags)
105{
Nathan Scottec86dc02006-03-17 17:25:36 +1100106 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Nathan Scott4aeb6642005-11-02 11:43:58 +1100107 timespec_t tv;
108
Nathan Scott4aeb6642005-11-02 11:43:58 +1100109 nanotime(&tv);
110 if (flags & XFS_ICHGTIME_MOD) {
111 inode->i_mtime = tv;
112 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
113 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
114 }
115 if (flags & XFS_ICHGTIME_ACC) {
116 inode->i_atime = tv;
117 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
118 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
119 }
120 if (flags & XFS_ICHGTIME_CHG) {
121 inode->i_ctime = tv;
122 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
123 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
124 }
125
126 /*
127 * We update the i_update_core field _after_ changing
128 * the timestamps in order to coordinate properly with
129 * xfs_iflush() so that we don't lose timestamp updates.
130 * This keeps us from having to hold the inode lock
131 * while doing this. We use the SYNCHRONIZE macro to
132 * ensure that the compiler does not reorder the update
133 * of i_update_core above the timestamp updates above.
134 */
135 SYNCHRONIZE();
136 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100137 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100138 mark_inode_dirty_sync(inode);
139}
140
141/*
142 * Variant on the above which avoids querying the system clock
143 * in situations where we know the Linux inode timestamps have
144 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100145 */
146void
147xfs_ichgtime_fast(
148 xfs_inode_t *ip,
149 struct inode *inode,
150 int flags)
151{
152 timespec_t *tvp;
153
154 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100155 * Atime updates for read() & friends are handled lazily now, and
156 * explicit updates must go through xfs_ichgtime()
157 */
158 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
159
160 /*
Nathan Scott4aeb6642005-11-02 11:43:58 +1100161 * We're not supposed to change timestamps in readonly-mounted
162 * filesystems. Throw it away if anyone asks us.
163 */
164 if (unlikely(IS_RDONLY(inode)))
165 return;
166
Nathan Scott4aeb6642005-11-02 11:43:58 +1100167 if (flags & XFS_ICHGTIME_MOD) {
168 tvp = &inode->i_mtime;
169 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
170 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
171 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100172 if (flags & XFS_ICHGTIME_CHG) {
173 tvp = &inode->i_ctime;
174 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
175 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
176 }
177
178 /*
179 * We update the i_update_core field _after_ changing
180 * the timestamps in order to coordinate properly with
181 * xfs_iflush() so that we don't lose timestamp updates.
182 * This keeps us from having to hold the inode lock
183 * while doing this. We use the SYNCHRONIZE macro to
184 * ensure that the compiler does not reorder the update
185 * of i_update_core above the timestamp updates above.
186 */
187 SYNCHRONIZE();
188 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100189 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100190 mark_inode_dirty_sync(inode);
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/*
195 * Pull the link count and size up from the xfs inode to the linux inode
196 */
197STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100198xfs_validate_fields(
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000199 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000201 struct xfs_inode *ip = XFS_I(inode);
202 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000204 /* we're under i_sem so i_size can't change under us */
205 size = XFS_ISIZE(ip);
206 if (i_size_read(inode) != size)
207 i_size_write(inode, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/*
Nathan Scott446ada42006-01-11 15:35:44 +1100211 * Hook in SELinux. This is not quite correct yet, what we really need
212 * here (as we do for default ACLs) is a mechanism by which creation of
213 * these attrs can be journalled at inode creation time (along with the
214 * inode, of course, such that log replay can't cause these to be lost).
215 */
216STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100217xfs_init_security(
Nathan Scott67fcaa72006-06-09 17:00:52 +1000218 bhv_vnode_t *vp,
Nathan Scott446ada42006-01-11 15:35:44 +1100219 struct inode *dir)
220{
Nathan Scottec86dc02006-03-17 17:25:36 +1100221 struct inode *ip = vn_to_inode(vp);
Nathan Scott446ada42006-01-11 15:35:44 +1100222 size_t length;
223 void *value;
224 char *name;
225 int error;
226
227 error = security_inode_init_security(ip, dir, &name, &value, &length);
228 if (error) {
229 if (error == -EOPNOTSUPP)
230 return 0;
231 return -error;
232 }
233
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000234 error = xfs_attr_set(XFS_I(ip), name, value,
235 length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100236 if (!error)
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000237 xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100238
239 kfree(name);
240 kfree(value);
241 return error;
242}
243
David Chinner7989cb82007-02-10 18:34:56 +1100244STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100245xfs_cleanup_inode(
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000246 struct inode *dir,
Nathan Scott67fcaa72006-06-09 17:00:52 +1000247 bhv_vnode_t *vp,
Nathan Scott220b5282006-03-14 13:33:36 +1100248 struct dentry *dentry,
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100249 int mode)
250{
251 struct dentry teardown = {};
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100252
253 /* Oh, the horror.
Nathan Scott220b5282006-03-14 13:33:36 +1100254 * If we can't add the ACL or we fail in
Nathan Scott416c6d52006-03-14 14:00:51 +1100255 * xfs_init_security we must back out.
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100256 * ENOSPC can hit here, among other things.
257 */
Nathan Scottec86dc02006-03-17 17:25:36 +1100258 teardown.d_inode = vn_to_inode(vp);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100259 teardown.d_name = dentry->d_name;
260
261 if (S_ISDIR(mode))
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000262 xfs_rmdir(XFS_I(dir), &teardown);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100263 else
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000264 xfs_remove(XFS_I(dir), &teardown);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100265 VN_RELE(vp);
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100269xfs_vn_mknod(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct inode *dir,
271 struct dentry *dentry,
272 int mode,
273 dev_t rdev)
274{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100275 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100276 struct xfs_inode *ip = NULL;
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
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100288 if (test_default_acl && test_default_acl(dir)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100289 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100291 }
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100292 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 _ACL_FREE(default_acl);
294 default_acl = NULL;
295 }
296 }
297
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100298 if (IS_POSIXACL(dir) && !default_acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 mode &= ~current->fs->umask;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 switch (mode & S_IFMT) {
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100302 case S_IFCHR:
303 case S_IFBLK:
304 case S_IFIFO:
305 case S_IFSOCK:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000306 rdev = sysv_encode_dev(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 case S_IFREG:
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100308 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 break;
310 case S_IFDIR:
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100311 error = xfs_mkdir(XFS_I(dir), dentry, mode, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
313 default:
314 error = EINVAL;
315 break;
316 }
317
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100318 if (unlikely(error))
319 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100320
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100321 inode = ip->i_vnode;
322
323 error = xfs_init_security(inode, dir);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100324 if (unlikely(error))
325 goto out_cleanup_inode;
326
327 if (default_acl) {
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100328 error = _ACL_INHERIT(inode, mode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100329 if (unlikely(error))
330 goto out_cleanup_inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100331 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 _ACL_FREE(default_acl);
333 }
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100336 if (S_ISDIR(mode))
337 xfs_validate_fields(inode);
338 d_instantiate(dentry, inode);
339 xfs_validate_fields(dir);
340 return -error;
341
342 out_cleanup_inode:
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100343 xfs_cleanup_inode(dir, inode, dentry, mode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100344 out_free_acl:
345 if (default_acl)
346 _ACL_FREE(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -error;
348}
349
350STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100351xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct inode *dir,
353 struct dentry *dentry,
354 int mode,
355 struct nameidata *nd)
356{
Nathan Scott416c6d52006-03-14 14:00:51 +1100357 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100361xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct inode *dir,
363 struct dentry *dentry,
364 int mode)
365{
Nathan Scott416c6d52006-03-14 14:00:51 +1100366 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100370xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct inode *dir,
372 struct dentry *dentry,
373 struct nameidata *nd)
374{
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000375 bhv_vnode_t *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int error;
377
378 if (dentry->d_name.len >= MAXNAMELEN)
379 return ERR_PTR(-ENAMETOOLONG);
380
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000381 error = xfs_lookup(XFS_I(dir), dentry, &cvp);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000382 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (unlikely(error != ENOENT))
384 return ERR_PTR(-error);
385 d_add(dentry, NULL);
386 return NULL;
387 }
388
Nathan Scottec86dc02006-03-17 17:25:36 +1100389 return d_splice_alias(vn_to_inode(cvp), dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100393xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct dentry *old_dentry,
395 struct inode *dir,
396 struct dentry *dentry)
397{
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100398 struct inode *inode; /* inode of guy being linked to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int error;
400
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100401 inode = old_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100403 igrab(inode);
404 error = xfs_link(XFS_I(dir), XFS_I(inode), dentry);
Nathan Scott97dfd702006-06-27 16:13:46 +1000405 if (unlikely(error)) {
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100406 iput(inode);
407 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100409
410 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
411 xfs_validate_fields(inode);
412 d_instantiate(dentry, inode);
413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
416STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100417xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct inode *dir,
419 struct dentry *dentry)
420{
421 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 int error;
423
424 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000426 error = xfs_remove(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100427 if (likely(!error)) {
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000428 xfs_validate_fields(dir); /* size needs update */
429 xfs_validate_fields(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -error;
432}
433
434STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100435xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct inode *dir,
437 struct dentry *dentry,
438 const char *symname)
439{
440 struct inode *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000441 bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000443 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 cvp = NULL;
446
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000447 mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000448 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000450 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
451 &cvp, NULL);
Nathan Scott54245702006-01-16 15:54:05 +1100452 if (likely(!error && cvp)) {
Nathan Scott416c6d52006-03-14 14:00:51 +1100453 error = xfs_init_security(cvp, dir);
Nathan Scott54245702006-01-16 15:54:05 +1100454 if (likely(!error)) {
Nathan Scottec86dc02006-03-17 17:25:36 +1100455 ip = vn_to_inode(cvp);
Nathan Scott54245702006-01-16 15:54:05 +1100456 d_instantiate(dentry, ip);
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000457 xfs_validate_fields(dir);
458 xfs_validate_fields(ip);
Nathan Scottce9d37c2006-03-17 17:26:34 +1100459 } else {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000460 xfs_cleanup_inode(dir, cvp, dentry, 0);
Nathan Scott54245702006-01-16 15:54:05 +1100461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 return -error;
464}
465
466STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100467xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct inode *dir,
469 struct dentry *dentry)
470{
471 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int error;
473
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000474 error = xfs_rmdir(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100475 if (likely(!error)) {
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000476 xfs_validate_fields(inode);
477 xfs_validate_fields(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479 return -error;
480}
481
482STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100483xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct inode *odir,
485 struct dentry *odentry,
486 struct inode *ndir,
487 struct dentry *ndentry)
488{
489 struct inode *new_inode = ndentry->d_inode;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000490 bhv_vnode_t *tvp; /* target directory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int error;
492
Nathan Scottec86dc02006-03-17 17:25:36 +1100493 tvp = vn_from_inode(ndir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000495 error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100496 if (likely(!error)) {
497 if (new_inode)
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000498 xfs_validate_fields(new_inode);
499 xfs_validate_fields(odir);
Nathan Scott220b5282006-03-14 13:33:36 +1100500 if (ndir != odir)
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000501 xfs_validate_fields(ndir);
Nathan Scott220b5282006-03-14 13:33:36 +1100502 }
Nathan Scott220b5282006-03-14 13:33:36 +1100503 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506/*
507 * careful here - this function can get called recursively, so
508 * we need to be very careful about how much stack we use.
509 * uio is kmalloced for this reason...
510 */
Al Viro008b1502005-08-20 00:17:39 +0100511STATIC void *
Nathan Scott416c6d52006-03-14 14:00:51 +1100512xfs_vn_follow_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct dentry *dentry,
514 struct nameidata *nd)
515{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 char *link;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000517 int error = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700519 link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000520 if (!link)
521 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000523 error = -xfs_readlink(XFS_I(dentry->d_inode), link);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000524 if (unlikely(error))
525 goto out_kfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 nd_set_link(nd, link);
Al Viro008b1502005-08-20 00:17:39 +0100528 return NULL;
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000529
530 out_kfree:
531 kfree(link);
532 out_err:
533 nd_set_link(nd, ERR_PTR(error));
534 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Nathan Scottcde410a2005-09-05 11:47:01 +1000537STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100538xfs_vn_put_link(
Nathan Scottcde410a2005-09-05 11:47:01 +1000539 struct dentry *dentry,
540 struct nameidata *nd,
541 void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Nathan Scottcde410a2005-09-05 11:47:01 +1000543 char *s = nd_get_link(nd);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (!IS_ERR(s))
546 kfree(s);
547}
548
549#ifdef CONFIG_XFS_POSIX_ACL
550STATIC int
Christoph Hellwig45767582008-02-05 12:13:24 +1100551xfs_check_acl(
552 struct inode *inode,
553 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Christoph Hellwig45767582008-02-05 12:13:24 +1100555 struct xfs_inode *ip = XFS_I(inode);
556 int error;
557
558 xfs_itrace_entry(ip);
559
560 if (XFS_IFORK_Q(ip)) {
561 error = xfs_acl_iaccess(ip, mask, NULL);
562 if (error != -1)
563 return -error;
564 }
565
566 return -EAGAIN;
567}
568
569STATIC int
570xfs_vn_permission(
571 struct inode *inode,
572 int mask,
573 struct nameidata *nd)
574{
575 return generic_permission(inode, mask, xfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100578#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#endif
580
581STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100582xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000583 struct vfsmount *mnt,
584 struct dentry *dentry,
585 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000587 struct inode *inode = dentry->d_inode;
588 struct xfs_inode *ip = XFS_I(inode);
589 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000591 xfs_itrace_entry(ip);
592
593 if (XFS_FORCED_SHUTDOWN(mp))
594 return XFS_ERROR(EIO);
595
596 stat->size = XFS_ISIZE(ip);
597 stat->dev = inode->i_sb->s_dev;
598 stat->mode = ip->i_d.di_mode;
599 stat->nlink = ip->i_d.di_nlink;
600 stat->uid = ip->i_d.di_uid;
601 stat->gid = ip->i_d.di_gid;
602 stat->ino = ip->i_ino;
603#if XFS_BIG_INUMS
604 stat->ino += mp->m_inoadd;
605#endif
606 stat->atime = inode->i_atime;
607 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
608 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
609 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
610 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
611 stat->blocks =
612 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
613
614
615 switch (inode->i_mode & S_IFMT) {
616 case S_IFBLK:
617 case S_IFCHR:
618 stat->blksize = BLKDEV_IOSIZE;
619 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
620 sysv_minor(ip->i_df.if_u2.if_rdev));
621 break;
622 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100623 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000624 /*
625 * If the file blocks are being allocated from a
626 * realtime volume, then return the inode's realtime
627 * extent size or the realtime volume's extent size.
628 */
629 stat->blksize =
630 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
631 } else
632 stat->blksize = xfs_preferred_iosize(mp);
633 stat->rdev = 0;
634 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000635 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000636
637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
640STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100641xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct dentry *dentry,
643 struct iattr *attr)
644{
645 struct inode *inode = dentry->d_inode;
646 unsigned int ia_valid = attr->ia_valid;
Nathan Scott8285fb52006-06-09 17:07:12 +1000647 bhv_vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 int flags = 0;
649 int error;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (ia_valid & ATTR_UID) {
652 vattr.va_mask |= XFS_AT_UID;
653 vattr.va_uid = attr->ia_uid;
654 }
655 if (ia_valid & ATTR_GID) {
656 vattr.va_mask |= XFS_AT_GID;
657 vattr.va_gid = attr->ia_gid;
658 }
659 if (ia_valid & ATTR_SIZE) {
660 vattr.va_mask |= XFS_AT_SIZE;
661 vattr.va_size = attr->ia_size;
662 }
663 if (ia_valid & ATTR_ATIME) {
664 vattr.va_mask |= XFS_AT_ATIME;
665 vattr.va_atime = attr->ia_atime;
Nathan Scott8c0b5112006-04-11 15:12:45 +1000666 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 if (ia_valid & ATTR_MTIME) {
669 vattr.va_mask |= XFS_AT_MTIME;
670 vattr.va_mtime = attr->ia_mtime;
671 }
672 if (ia_valid & ATTR_CTIME) {
673 vattr.va_mask |= XFS_AT_CTIME;
674 vattr.va_ctime = attr->ia_ctime;
675 }
676 if (ia_valid & ATTR_MODE) {
677 vattr.va_mask |= XFS_AT_MODE;
678 vattr.va_mode = attr->ia_mode;
679 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
680 inode->i_mode &= ~S_ISGID;
681 }
682
683 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
684 flags |= ATTR_UTIME;
685#ifdef ATTR_NO_BLOCK
686 if ((ia_valid & ATTR_NO_BLOCK))
687 flags |= ATTR_NONBLOCK;
688#endif
689
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000690 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100691 if (likely(!error))
Christoph Hellwig21a62542007-09-19 15:27:49 +1000692 vn_revalidate(vn_from_inode(inode));
Nathan Scott220b5282006-03-14 13:33:36 +1100693 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100697xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct inode *inode)
699{
Nathan Scottc2536662006-03-29 10:44:40 +1000700 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100704xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct dentry *dentry,
706 const char *name,
707 const void *data,
708 size_t size,
709 int flags)
710{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000711 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 char *attr = (char *)name;
713 attrnames_t *namesp;
714 int xflags = 0;
715 int error;
716
717 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
718 if (!namesp)
719 return -EOPNOTSUPP;
720 attr += namesp->attr_namelen;
721 error = namesp->attr_capable(vp, NULL);
722 if (error)
723 return error;
724
725 /* Convert Linux syscall to XFS internal ATTR flags */
726 if (flags & XATTR_CREATE)
727 xflags |= ATTR_CREATE;
728 if (flags & XATTR_REPLACE)
729 xflags |= ATTR_REPLACE;
730 xflags |= namesp->attr_flag;
731 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
732}
733
734STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100735xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct dentry *dentry,
737 const char *name,
738 void *data,
739 size_t size)
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 ssize_t 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
755 /* Convert Linux syscall to XFS internal ATTR flags */
756 if (!size) {
757 xflags |= ATTR_KERNOVAL;
758 data = NULL;
759 }
760 xflags |= namesp->attr_flag;
761 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
762}
763
764STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100765xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct dentry *dentry,
767 char *data,
768 size_t size)
769{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000770 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 int error, xflags = ATTR_KERNAMELS;
772 ssize_t result;
773
774 if (!size)
775 xflags |= ATTR_KERNOVAL;
776 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
777
778 error = attr_generic_list(vp, data, size, xflags, &result);
779 if (error < 0)
780 return error;
781 return result;
782}
783
784STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100785xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct dentry *dentry,
787 const char *name)
788{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000789 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 char *attr = (char *)name;
791 attrnames_t *namesp;
792 int xflags = 0;
793 int error;
794
795 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
796 if (!namesp)
797 return -EOPNOTSUPP;
798 attr += namesp->attr_namelen;
799 error = namesp->attr_capable(vp, NULL);
800 if (error)
801 return error;
802 xflags |= namesp->attr_flag;
803 return namesp->attr_remove(vp, attr, xflags);
804}
805
David Chinner3ed65262007-11-23 16:29:25 +1100806STATIC long
807xfs_vn_fallocate(
808 struct inode *inode,
809 int mode,
810 loff_t offset,
811 loff_t len)
812{
813 long error;
814 loff_t new_size = 0;
815 xfs_flock64_t bf;
816 xfs_inode_t *ip = XFS_I(inode);
817
818 /* preallocation on directories not yet supported */
819 error = -ENODEV;
820 if (S_ISDIR(inode->i_mode))
821 goto out_error;
822
823 bf.l_whence = 0;
824 bf.l_start = offset;
825 bf.l_len = len;
826
827 xfs_ilock(ip, XFS_IOLOCK_EXCL);
828 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
829 0, NULL, ATTR_NOLOCK);
830 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
831 offset + len > i_size_read(inode))
832 new_size = offset + len;
833
834 /* Change file size if needed */
835 if (new_size) {
836 bhv_vattr_t va;
837
838 va.va_mask = XFS_AT_SIZE;
839 va.va_size = new_size;
840 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
841 }
842
843 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
844out_error:
845 return error;
846}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800848const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100849 .permission = xfs_vn_permission,
850 .truncate = xfs_vn_truncate,
851 .getattr = xfs_vn_getattr,
852 .setattr = xfs_vn_setattr,
853 .setxattr = xfs_vn_setxattr,
854 .getxattr = xfs_vn_getxattr,
855 .listxattr = xfs_vn_listxattr,
856 .removexattr = xfs_vn_removexattr,
David Chinner3ed65262007-11-23 16:29:25 +1100857 .fallocate = xfs_vn_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858};
859
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800860const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100861 .create = xfs_vn_create,
862 .lookup = xfs_vn_lookup,
863 .link = xfs_vn_link,
864 .unlink = xfs_vn_unlink,
865 .symlink = xfs_vn_symlink,
866 .mkdir = xfs_vn_mkdir,
867 .rmdir = xfs_vn_rmdir,
868 .mknod = xfs_vn_mknod,
869 .rename = xfs_vn_rename,
870 .permission = xfs_vn_permission,
871 .getattr = xfs_vn_getattr,
872 .setattr = xfs_vn_setattr,
873 .setxattr = xfs_vn_setxattr,
874 .getxattr = xfs_vn_getxattr,
875 .listxattr = xfs_vn_listxattr,
876 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877};
878
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800879const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100881 .follow_link = xfs_vn_follow_link,
882 .put_link = xfs_vn_put_link,
883 .permission = xfs_vn_permission,
884 .getattr = xfs_vn_getattr,
885 .setattr = xfs_vn_setattr,
886 .setxattr = xfs_vn_setxattr,
887 .getxattr = xfs_vn_getxattr,
888 .listxattr = xfs_vn_listxattr,
889 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890};