blob: 53f8feb28e585219e7be707c61907a530ed29f33 [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{
Christoph Hellwigaf048192008-03-06 13:46:43 +110065 struct inode *inode = ip->i_vnode;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110066
Christoph Hellwigaf048192008-03-06 13:46:43 +110067 if (inode) {
68 ip->i_d.di_atime.t_sec = (__int32_t)inode->i_atime.tv_sec;
69 ip->i_d.di_atime.t_nsec = (__int32_t)inode->i_atime.tv_nsec;
Christoph Hellwig42fe2b12006-01-11 15:35:17 +110070 }
71}
72
73/*
David Chinner5d51eff2007-11-23 16:29:18 +110074 * If the linux inode exists, mark it dirty.
75 * Used when commiting a dirty inode into a transaction so that
76 * the inode will get written back by the linux code
77 */
78void
79xfs_mark_inode_dirty_sync(
80 xfs_inode_t *ip)
81{
Christoph Hellwigaf048192008-03-06 13:46:43 +110082 struct inode *inode = ip->i_vnode;
David Chinner5d51eff2007-11-23 16:29:18 +110083
Christoph Hellwigaf048192008-03-06 13:46:43 +110084 if (inode)
85 mark_inode_dirty_sync(inode);
David Chinner5d51eff2007-11-23 16:29:18 +110086}
87
88/*
Nathan Scott4aeb6642005-11-02 11:43:58 +110089 * Change the requested timestamp in the given inode.
90 * We don't lock across timestamp updates, and we don't log them but
91 * we do record the fact that there is dirty information in core.
92 *
93 * NOTE -- callers MUST combine XFS_ICHGTIME_MOD or XFS_ICHGTIME_CHG
94 * with XFS_ICHGTIME_ACC to be sure that access time
95 * update will take. Calling first with XFS_ICHGTIME_ACC
96 * and then XFS_ICHGTIME_MOD may fail to modify the access
97 * timestamp if the filesystem is mounted noacctm.
98 */
99void
100xfs_ichgtime(
101 xfs_inode_t *ip,
102 int flags)
103{
Nathan Scottec86dc02006-03-17 17:25:36 +1100104 struct inode *inode = vn_to_inode(XFS_ITOV(ip));
Nathan Scott4aeb6642005-11-02 11:43:58 +1100105 timespec_t tv;
106
Nathan Scott4aeb6642005-11-02 11:43:58 +1100107 nanotime(&tv);
108 if (flags & XFS_ICHGTIME_MOD) {
109 inode->i_mtime = tv;
110 ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
111 ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
112 }
113 if (flags & XFS_ICHGTIME_ACC) {
114 inode->i_atime = tv;
115 ip->i_d.di_atime.t_sec = (__int32_t)tv.tv_sec;
116 ip->i_d.di_atime.t_nsec = (__int32_t)tv.tv_nsec;
117 }
118 if (flags & XFS_ICHGTIME_CHG) {
119 inode->i_ctime = tv;
120 ip->i_d.di_ctime.t_sec = (__int32_t)tv.tv_sec;
121 ip->i_d.di_ctime.t_nsec = (__int32_t)tv.tv_nsec;
122 }
123
124 /*
125 * We update the i_update_core field _after_ changing
126 * the timestamps in order to coordinate properly with
127 * xfs_iflush() so that we don't lose timestamp updates.
128 * This keeps us from having to hold the inode lock
129 * while doing this. We use the SYNCHRONIZE macro to
130 * ensure that the compiler does not reorder the update
131 * of i_update_core above the timestamp updates above.
132 */
133 SYNCHRONIZE();
134 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100135 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100136 mark_inode_dirty_sync(inode);
137}
138
139/*
140 * Variant on the above which avoids querying the system clock
141 * in situations where we know the Linux inode timestamps have
142 * just been updated (and so we can update our inode cheaply).
Nathan Scott4aeb6642005-11-02 11:43:58 +1100143 */
144void
145xfs_ichgtime_fast(
146 xfs_inode_t *ip,
147 struct inode *inode,
148 int flags)
149{
150 timespec_t *tvp;
151
152 /*
Christoph Hellwig42fe2b12006-01-11 15:35:17 +1100153 * Atime updates for read() & friends are handled lazily now, and
154 * explicit updates must go through xfs_ichgtime()
155 */
156 ASSERT((flags & XFS_ICHGTIME_ACC) == 0);
157
158 /*
Nathan Scott4aeb6642005-11-02 11:43:58 +1100159 * We're not supposed to change timestamps in readonly-mounted
160 * filesystems. Throw it away if anyone asks us.
161 */
162 if (unlikely(IS_RDONLY(inode)))
163 return;
164
Nathan Scott4aeb6642005-11-02 11:43:58 +1100165 if (flags & XFS_ICHGTIME_MOD) {
166 tvp = &inode->i_mtime;
167 ip->i_d.di_mtime.t_sec = (__int32_t)tvp->tv_sec;
168 ip->i_d.di_mtime.t_nsec = (__int32_t)tvp->tv_nsec;
169 }
Nathan Scott4aeb6642005-11-02 11:43:58 +1100170 if (flags & XFS_ICHGTIME_CHG) {
171 tvp = &inode->i_ctime;
172 ip->i_d.di_ctime.t_sec = (__int32_t)tvp->tv_sec;
173 ip->i_d.di_ctime.t_nsec = (__int32_t)tvp->tv_nsec;
174 }
175
176 /*
177 * We update the i_update_core field _after_ changing
178 * the timestamps in order to coordinate properly with
179 * xfs_iflush() so that we don't lose timestamp updates.
180 * This keeps us from having to hold the inode lock
181 * while doing this. We use the SYNCHRONIZE macro to
182 * ensure that the compiler does not reorder the update
183 * of i_update_core above the timestamp updates above.
184 */
185 SYNCHRONIZE();
186 ip->i_update_core = 1;
David Chinnercf10e822007-12-07 14:09:11 +1100187 if (!(inode->i_state & I_NEW))
Nathan Scott4aeb6642005-11-02 11:43:58 +1100188 mark_inode_dirty_sync(inode);
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/*
193 * Pull the link count and size up from the xfs inode to the linux inode
194 */
195STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100196xfs_validate_fields(
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000197 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000199 struct xfs_inode *ip = XFS_I(inode);
200 loff_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000202 /* we're under i_sem so i_size can't change under us */
203 size = XFS_ISIZE(ip);
204 if (i_size_read(inode) != size)
205 i_size_write(inode, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208/*
Nathan Scott446ada42006-01-11 15:35:44 +1100209 * Hook in SELinux. This is not quite correct yet, what we really need
210 * here (as we do for default ACLs) is a mechanism by which creation of
211 * these attrs can be journalled at inode creation time (along with the
212 * inode, of course, such that log replay can't cause these to be lost).
213 */
214STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100215xfs_init_security(
Christoph Hellwigaf048192008-03-06 13:46:43 +1100216 struct inode *inode,
Nathan Scott446ada42006-01-11 15:35:44 +1100217 struct inode *dir)
218{
Christoph Hellwigaf048192008-03-06 13:46:43 +1100219 struct xfs_inode *ip = XFS_I(inode);
Nathan Scott446ada42006-01-11 15:35:44 +1100220 size_t length;
221 void *value;
222 char *name;
223 int error;
224
Christoph Hellwigaf048192008-03-06 13:46:43 +1100225 error = security_inode_init_security(inode, dir, &name,
226 &value, &length);
Nathan Scott446ada42006-01-11 15:35:44 +1100227 if (error) {
228 if (error == -EOPNOTSUPP)
229 return 0;
230 return -error;
231 }
232
Christoph Hellwigaf048192008-03-06 13:46:43 +1100233 error = xfs_attr_set(ip, name, value, length, ATTR_SECURE);
Nathan Scott446ada42006-01-11 15:35:44 +1100234 if (!error)
Christoph Hellwigaf048192008-03-06 13:46:43 +1100235 xfs_iflags_set(ip, XFS_IMODIFIED);
Nathan Scott446ada42006-01-11 15:35:44 +1100236
237 kfree(name);
238 kfree(value);
239 return error;
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,
Christoph Hellwigaf048192008-03-06 13:46:43 +1100245 struct inode *inode,
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 */
Christoph Hellwigaf048192008-03-06 13:46:43 +1100256 teardown.d_inode = inode;
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);
Christoph Hellwigaf048192008-03-06 13:46:43 +1100263 iput(inode);
Yingping Lu3a69c7d2006-02-01 12:14:34 +1100264}
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{
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100273 struct inode *inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100274 struct xfs_inode *ip = NULL;
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
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100286 if (test_default_acl && test_default_acl(dir)) {
Nathan Scott220b5282006-03-14 13:33:36 +1100287 if (!_ACL_ALLOC(default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -ENOMEM;
Nathan Scott220b5282006-03-14 13:33:36 +1100289 }
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100290 if (!_ACL_GET_DEFAULT(dir, default_acl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 _ACL_FREE(default_acl);
292 default_acl = NULL;
293 }
294 }
295
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100296 if (IS_POSIXACL(dir) && !default_acl)
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) {
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100300 case S_IFCHR:
301 case S_IFBLK:
302 case S_IFIFO:
303 case S_IFSOCK:
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000304 rdev = sysv_encode_dev(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 case S_IFREG:
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100306 error = xfs_create(XFS_I(dir), dentry, mode, rdev, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 case S_IFDIR:
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100309 error = xfs_mkdir(XFS_I(dir), dentry, mode, &ip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 default:
312 error = EINVAL;
313 break;
314 }
315
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100316 if (unlikely(error))
317 goto out_free_acl;
Nathan Scott446ada42006-01-11 15:35:44 +1100318
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100319 inode = ip->i_vnode;
320
321 error = xfs_init_security(inode, dir);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100322 if (unlikely(error))
323 goto out_cleanup_inode;
324
325 if (default_acl) {
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100326 error = _ACL_INHERIT(inode, mode, default_acl);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100327 if (unlikely(error))
328 goto out_cleanup_inode;
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100329 xfs_iflags_set(ip, XFS_IMODIFIED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 _ACL_FREE(default_acl);
331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100334 if (S_ISDIR(mode))
335 xfs_validate_fields(inode);
336 d_instantiate(dentry, inode);
337 xfs_validate_fields(dir);
338 return -error;
339
340 out_cleanup_inode:
Christoph Hellwig979ebab2008-03-06 13:46:05 +1100341 xfs_cleanup_inode(dir, inode, dentry, mode);
Christoph Hellwigdb0bb7b2008-03-06 13:44:35 +1100342 out_free_acl:
343 if (default_acl)
344 _ACL_FREE(default_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return -error;
346}
347
348STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100349xfs_vn_create(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct inode *dir,
351 struct dentry *dentry,
352 int mode,
353 struct nameidata *nd)
354{
Nathan Scott416c6d52006-03-14 14:00:51 +1100355 return xfs_vn_mknod(dir, dentry, mode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100359xfs_vn_mkdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct inode *dir,
361 struct dentry *dentry,
362 int mode)
363{
Nathan Scott416c6d52006-03-14 14:00:51 +1100364 return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367STATIC struct dentry *
Nathan Scott416c6d52006-03-14 14:00:51 +1100368xfs_vn_lookup(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct inode *dir,
370 struct dentry *dentry,
371 struct nameidata *nd)
372{
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100373 struct xfs_inode *cip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int error;
375
376 if (dentry->d_name.len >= MAXNAMELEN)
377 return ERR_PTR(-ENAMETOOLONG);
378
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100379 error = xfs_lookup(XFS_I(dir), dentry, &cip);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000380 if (unlikely(error)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (unlikely(error != ENOENT))
382 return ERR_PTR(-error);
383 d_add(dentry, NULL);
384 return NULL;
385 }
386
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100387 return d_splice_alias(cip->i_vnode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100391xfs_vn_link(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct dentry *old_dentry,
393 struct inode *dir,
394 struct dentry *dentry)
395{
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100396 struct inode *inode; /* inode of guy being linked to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int error;
398
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100399 inode = old_dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100401 igrab(inode);
402 error = xfs_link(XFS_I(dir), XFS_I(inode), dentry);
Nathan Scott97dfd702006-06-27 16:13:46 +1000403 if (unlikely(error)) {
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100404 iput(inode);
405 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Christoph Hellwiga3da7892008-03-06 13:46:12 +1100407
408 xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED);
409 xfs_validate_fields(inode);
410 d_instantiate(dentry, inode);
411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100415xfs_vn_unlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct inode *dir,
417 struct dentry *dentry)
418{
419 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int error;
421
422 inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000424 error = xfs_remove(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100425 if (likely(!error)) {
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000426 xfs_validate_fields(dir); /* size needs update */
427 xfs_validate_fields(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -error;
430}
431
432STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100433xfs_vn_symlink(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct inode *dir,
435 struct dentry *dentry,
436 const char *symname)
437{
Christoph Hellwig3937be52008-03-06 13:46:19 +1100438 struct inode *inode;
439 struct xfs_inode *cip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int error;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000441 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000443 mode = S_IFLNK |
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000444 (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Christoph Hellwig3e5daf02007-10-11 18:09:12 +1000446 error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +1100447 &cip, NULL);
448 if (unlikely(error))
449 goto out;
450
451 inode = cip->i_vnode;
452
453 error = xfs_init_security(inode, dir);
454 if (unlikely(error))
455 goto out_cleanup_inode;
456
457 d_instantiate(dentry, inode);
458 xfs_validate_fields(dir);
459 xfs_validate_fields(inode);
460 return 0;
461
462 out_cleanup_inode:
463 xfs_cleanup_inode(dir, inode, dentry, 0);
464 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return -error;
466}
467
468STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100469xfs_vn_rmdir(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 struct inode *dir,
471 struct dentry *dentry)
472{
473 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int error;
475
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000476 error = xfs_rmdir(XFS_I(dir), dentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100477 if (likely(!error)) {
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000478 xfs_validate_fields(inode);
479 xfs_validate_fields(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 return -error;
482}
483
484STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100485xfs_vn_rename(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 struct inode *odir,
487 struct dentry *odentry,
488 struct inode *ndir,
489 struct dentry *ndentry)
490{
491 struct inode *new_inode = ndentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int error;
493
Christoph Hellwig3937be52008-03-06 13:46:19 +1100494 error = xfs_rename(XFS_I(odir), odentry, XFS_I(ndir), ndentry);
Nathan Scott220b5282006-03-14 13:33:36 +1100495 if (likely(!error)) {
496 if (new_inode)
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000497 xfs_validate_fields(new_inode);
498 xfs_validate_fields(odir);
Nathan Scott220b5282006-03-14 13:33:36 +1100499 if (ndir != odir)
Christoph Hellwig6572bc22007-09-19 15:27:39 +1000500 xfs_validate_fields(ndir);
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
Christoph Hellwig45767582008-02-05 12:13:24 +1100550xfs_check_acl(
551 struct inode *inode,
552 int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Christoph Hellwig45767582008-02-05 12:13:24 +1100554 struct xfs_inode *ip = XFS_I(inode);
555 int error;
556
557 xfs_itrace_entry(ip);
558
559 if (XFS_IFORK_Q(ip)) {
560 error = xfs_acl_iaccess(ip, mask, NULL);
561 if (error != -1)
562 return -error;
563 }
564
565 return -EAGAIN;
566}
567
568STATIC int
569xfs_vn_permission(
570 struct inode *inode,
571 int mask,
572 struct nameidata *nd)
573{
574 return generic_permission(inode, mask, xfs_check_acl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576#else
Nathan Scott416c6d52006-03-14 14:00:51 +1100577#define xfs_vn_permission NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579
580STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100581xfs_vn_getattr(
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000582 struct vfsmount *mnt,
583 struct dentry *dentry,
584 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000586 struct inode *inode = dentry->d_inode;
587 struct xfs_inode *ip = XFS_I(inode);
588 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000590 xfs_itrace_entry(ip);
591
592 if (XFS_FORCED_SHUTDOWN(mp))
593 return XFS_ERROR(EIO);
594
595 stat->size = XFS_ISIZE(ip);
596 stat->dev = inode->i_sb->s_dev;
597 stat->mode = ip->i_d.di_mode;
598 stat->nlink = ip->i_d.di_nlink;
599 stat->uid = ip->i_d.di_uid;
600 stat->gid = ip->i_d.di_gid;
601 stat->ino = ip->i_ino;
602#if XFS_BIG_INUMS
603 stat->ino += mp->m_inoadd;
604#endif
605 stat->atime = inode->i_atime;
606 stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec;
607 stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
608 stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec;
609 stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
610 stat->blocks =
611 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
612
613
614 switch (inode->i_mode & S_IFMT) {
615 case S_IFBLK:
616 case S_IFCHR:
617 stat->blksize = BLKDEV_IOSIZE;
618 stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff,
619 sysv_minor(ip->i_df.if_u2.if_rdev));
620 break;
621 default:
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100622 if (XFS_IS_REALTIME_INODE(ip)) {
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000623 /*
624 * If the file blocks are being allocated from a
625 * realtime volume, then return the inode's realtime
626 * extent size or the realtime volume's extent size.
627 */
628 stat->blksize =
629 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
630 } else
631 stat->blksize = xfs_preferred_iosize(mp);
632 stat->rdev = 0;
633 break;
Nathan Scott69e23b92006-09-28 11:01:22 +1000634 }
Christoph Hellwigc43f4082007-10-11 17:46:39 +1000635
636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100640xfs_vn_setattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct dentry *dentry,
642 struct iattr *attr)
643{
644 struct inode *inode = dentry->d_inode;
645 unsigned int ia_valid = attr->ia_valid;
Nathan Scott8285fb52006-06-09 17:07:12 +1000646 bhv_vattr_t vattr = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int flags = 0;
648 int error;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (ia_valid & ATTR_UID) {
651 vattr.va_mask |= XFS_AT_UID;
652 vattr.va_uid = attr->ia_uid;
653 }
654 if (ia_valid & ATTR_GID) {
655 vattr.va_mask |= XFS_AT_GID;
656 vattr.va_gid = attr->ia_gid;
657 }
658 if (ia_valid & ATTR_SIZE) {
659 vattr.va_mask |= XFS_AT_SIZE;
660 vattr.va_size = attr->ia_size;
661 }
662 if (ia_valid & ATTR_ATIME) {
663 vattr.va_mask |= XFS_AT_ATIME;
664 vattr.va_atime = attr->ia_atime;
Nathan Scott8c0b5112006-04-11 15:12:45 +1000665 inode->i_atime = attr->ia_atime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 if (ia_valid & ATTR_MTIME) {
668 vattr.va_mask |= XFS_AT_MTIME;
669 vattr.va_mtime = attr->ia_mtime;
670 }
671 if (ia_valid & ATTR_CTIME) {
672 vattr.va_mask |= XFS_AT_CTIME;
673 vattr.va_ctime = attr->ia_ctime;
674 }
675 if (ia_valid & ATTR_MODE) {
676 vattr.va_mask |= XFS_AT_MODE;
677 vattr.va_mode = attr->ia_mode;
678 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
679 inode->i_mode &= ~S_ISGID;
680 }
681
682 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))
683 flags |= ATTR_UTIME;
684#ifdef ATTR_NO_BLOCK
685 if ((ia_valid & ATTR_NO_BLOCK))
686 flags |= ATTR_NONBLOCK;
687#endif
688
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000689 error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL);
Nathan Scott220b5282006-03-14 13:33:36 +1100690 if (likely(!error))
Christoph Hellwig21a62542007-09-19 15:27:49 +1000691 vn_revalidate(vn_from_inode(inode));
Nathan Scott220b5282006-03-14 13:33:36 +1100692 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695STATIC void
Nathan Scott416c6d52006-03-14 14:00:51 +1100696xfs_vn_truncate(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct inode *inode)
698{
Nathan Scottc2536662006-03-29 10:44:40 +1000699 block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100703xfs_vn_setxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct dentry *dentry,
705 const char *name,
706 const void *data,
707 size_t size,
708 int flags)
709{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000710 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 char *attr = (char *)name;
712 attrnames_t *namesp;
713 int xflags = 0;
714 int error;
715
716 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
717 if (!namesp)
718 return -EOPNOTSUPP;
719 attr += namesp->attr_namelen;
720 error = namesp->attr_capable(vp, NULL);
721 if (error)
722 return error;
723
724 /* Convert Linux syscall to XFS internal ATTR flags */
725 if (flags & XATTR_CREATE)
726 xflags |= ATTR_CREATE;
727 if (flags & XATTR_REPLACE)
728 xflags |= ATTR_REPLACE;
729 xflags |= namesp->attr_flag;
730 return namesp->attr_set(vp, attr, (void *)data, size, xflags);
731}
732
733STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100734xfs_vn_getxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct dentry *dentry,
736 const char *name,
737 void *data,
738 size_t size)
739{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000740 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 char *attr = (char *)name;
742 attrnames_t *namesp;
743 int xflags = 0;
744 ssize_t error;
745
746 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
747 if (!namesp)
748 return -EOPNOTSUPP;
749 attr += namesp->attr_namelen;
750 error = namesp->attr_capable(vp, NULL);
751 if (error)
752 return error;
753
754 /* Convert Linux syscall to XFS internal ATTR flags */
755 if (!size) {
756 xflags |= ATTR_KERNOVAL;
757 data = NULL;
758 }
759 xflags |= namesp->attr_flag;
760 return namesp->attr_get(vp, attr, (void *)data, size, xflags);
761}
762
763STATIC ssize_t
Nathan Scott416c6d52006-03-14 14:00:51 +1100764xfs_vn_listxattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct dentry *dentry,
766 char *data,
767 size_t size)
768{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000769 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int error, xflags = ATTR_KERNAMELS;
771 ssize_t result;
772
773 if (!size)
774 xflags |= ATTR_KERNOVAL;
775 xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS;
776
777 error = attr_generic_list(vp, data, size, xflags, &result);
778 if (error < 0)
779 return error;
780 return result;
781}
782
783STATIC int
Nathan Scott416c6d52006-03-14 14:00:51 +1100784xfs_vn_removexattr(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct dentry *dentry,
786 const char *name)
787{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000788 bhv_vnode_t *vp = vn_from_inode(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 char *attr = (char *)name;
790 attrnames_t *namesp;
791 int xflags = 0;
792 int error;
793
794 namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT);
795 if (!namesp)
796 return -EOPNOTSUPP;
797 attr += namesp->attr_namelen;
798 error = namesp->attr_capable(vp, NULL);
799 if (error)
800 return error;
801 xflags |= namesp->attr_flag;
802 return namesp->attr_remove(vp, attr, xflags);
803}
804
David Chinner3ed65262007-11-23 16:29:25 +1100805STATIC long
806xfs_vn_fallocate(
807 struct inode *inode,
808 int mode,
809 loff_t offset,
810 loff_t len)
811{
812 long error;
813 loff_t new_size = 0;
814 xfs_flock64_t bf;
815 xfs_inode_t *ip = XFS_I(inode);
816
817 /* preallocation on directories not yet supported */
818 error = -ENODEV;
819 if (S_ISDIR(inode->i_mode))
820 goto out_error;
821
822 bf.l_whence = 0;
823 bf.l_start = offset;
824 bf.l_len = len;
825
826 xfs_ilock(ip, XFS_IOLOCK_EXCL);
827 error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf,
828 0, NULL, ATTR_NOLOCK);
829 if (!error && !(mode & FALLOC_FL_KEEP_SIZE) &&
830 offset + len > i_size_read(inode))
831 new_size = offset + len;
832
833 /* Change file size if needed */
834 if (new_size) {
835 bhv_vattr_t va;
836
837 va.va_mask = XFS_AT_SIZE;
838 va.va_size = new_size;
839 error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL);
840 }
841
842 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
843out_error:
844 return error;
845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800847const struct inode_operations xfs_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100848 .permission = xfs_vn_permission,
849 .truncate = xfs_vn_truncate,
850 .getattr = xfs_vn_getattr,
851 .setattr = xfs_vn_setattr,
852 .setxattr = xfs_vn_setxattr,
853 .getxattr = xfs_vn_getxattr,
854 .listxattr = xfs_vn_listxattr,
855 .removexattr = xfs_vn_removexattr,
David Chinner3ed65262007-11-23 16:29:25 +1100856 .fallocate = xfs_vn_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};
858
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800859const struct inode_operations xfs_dir_inode_operations = {
Nathan Scott416c6d52006-03-14 14:00:51 +1100860 .create = xfs_vn_create,
861 .lookup = xfs_vn_lookup,
862 .link = xfs_vn_link,
863 .unlink = xfs_vn_unlink,
864 .symlink = xfs_vn_symlink,
865 .mkdir = xfs_vn_mkdir,
866 .rmdir = xfs_vn_rmdir,
867 .mknod = xfs_vn_mknod,
868 .rename = xfs_vn_rename,
869 .permission = xfs_vn_permission,
870 .getattr = xfs_vn_getattr,
871 .setattr = xfs_vn_setattr,
872 .setxattr = xfs_vn_setxattr,
873 .getxattr = xfs_vn_getxattr,
874 .listxattr = xfs_vn_listxattr,
875 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876};
877
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800878const struct inode_operations xfs_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 .readlink = generic_readlink,
Nathan Scott416c6d52006-03-14 14:00:51 +1100880 .follow_link = xfs_vn_follow_link,
881 .put_link = xfs_vn_put_link,
882 .permission = xfs_vn_permission,
883 .getattr = xfs_vn_getattr,
884 .setattr = xfs_vn_setattr,
885 .setxattr = xfs_vn_setxattr,
886 .getxattr = xfs_vn_getxattr,
887 .listxattr = xfs_vn_listxattr,
888 .removexattr = xfs_vn_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889};