Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * 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. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * 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 | |
1da177e4 | 17 | */ |
1da177e4 LT |
18 | #include "xfs.h" |
19 | #include "xfs_fs.h" | |
a844f451 | 20 | #include "xfs_bit.h" |
1da177e4 | 21 | #include "xfs_log.h" |
a844f451 | 22 | #include "xfs_inum.h" |
1da177e4 LT |
23 | #include "xfs_trans.h" |
24 | #include "xfs_sb.h" | |
25 | #include "xfs_ag.h" | |
1da177e4 LT |
26 | #include "xfs_dir2.h" |
27 | #include "xfs_alloc.h" | |
28 | #include "xfs_dmapi.h" | |
29 | #include "xfs_quota.h" | |
30 | #include "xfs_mount.h" | |
1da177e4 | 31 | #include "xfs_bmap_btree.h" |
a844f451 | 32 | #include "xfs_alloc_btree.h" |
1da177e4 | 33 | #include "xfs_ialloc_btree.h" |
1da177e4 | 34 | #include "xfs_dir2_sf.h" |
a844f451 | 35 | #include "xfs_attr_sf.h" |
1da177e4 LT |
36 | #include "xfs_dinode.h" |
37 | #include "xfs_inode.h" | |
38 | #include "xfs_bmap.h" | |
a844f451 NS |
39 | #include "xfs_btree.h" |
40 | #include "xfs_ialloc.h" | |
1da177e4 LT |
41 | #include "xfs_rtalloc.h" |
42 | #include "xfs_error.h" | |
43 | #include "xfs_itable.h" | |
44 | #include "xfs_rw.h" | |
45 | #include "xfs_acl.h" | |
1da177e4 LT |
46 | #include "xfs_attr.h" |
47 | #include "xfs_buf_item.h" | |
48 | #include "xfs_utils.h" | |
739bfb2a | 49 | #include "xfs_vnodeops.h" |
1da177e4 | 50 | |
16f7e0fe | 51 | #include <linux/capability.h> |
1da177e4 LT |
52 | #include <linux/xattr.h> |
53 | #include <linux/namei.h> | |
446ada4a | 54 | #include <linux/security.h> |
3ed65264 | 55 | #include <linux/falloc.h> |
1da177e4 | 56 | |
42fe2b1f CH |
57 | /* |
58 | * Bring the atime in the XFS inode uptodate. | |
59 | * Used before logging the inode to disk or when the Linux inode goes away. | |
60 | */ | |
61 | void | |
62 | xfs_synchronize_atime( | |
63 | xfs_inode_t *ip) | |
64 | { | |
67fcaa73 | 65 | bhv_vnode_t *vp; |
42fe2b1f CH |
66 | |
67 | vp = XFS_ITOV_NULL(ip); | |
68 | if (vp) { | |
0a74cd19 CH |
69 | 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; | |
42fe2b1f CH |
71 | } |
72 | } | |
73 | ||
5d51eff4 DC |
74 | /* |
75 | * 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 | */ | |
79 | void | |
80 | xfs_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 | ||
4aeb664c NS |
90 | /* |
91 | * 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 | */ | |
101 | void | |
102 | xfs_ichgtime( | |
103 | xfs_inode_t *ip, | |
104 | int flags) | |
105 | { | |
ec86dc02 | 106 | struct inode *inode = vn_to_inode(XFS_ITOV(ip)); |
4aeb664c NS |
107 | timespec_t tv; |
108 | ||
4aeb664c NS |
109 | 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; | |
cf10e82b | 137 | if (!(inode->i_state & I_NEW)) |
4aeb664c NS |
138 | 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). | |
4aeb664c NS |
145 | */ |
146 | void | |
147 | xfs_ichgtime_fast( | |
148 | xfs_inode_t *ip, | |
149 | struct inode *inode, | |
150 | int flags) | |
151 | { | |
152 | timespec_t *tvp; | |
153 | ||
154 | /* | |
42fe2b1f CH |
155 | * Atime updates for read() & friends are handled lazily now, and |
156 | * explicit updates must go through xfs_ichgtime() | |
4aeb664c | 157 | */ |
42fe2b1f | 158 | ASSERT((flags & XFS_ICHGTIME_ACC) == 0); |
4aeb664c NS |
159 | |
160 | /* | |
42fe2b1f CH |
161 | * We're not supposed to change timestamps in readonly-mounted |
162 | * filesystems. Throw it away if anyone asks us. | |
4aeb664c | 163 | */ |
42fe2b1f | 164 | if (unlikely(IS_RDONLY(inode))) |
4aeb664c NS |
165 | return; |
166 | ||
167 | 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 | } | |
4aeb664c NS |
172 | 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; | |
cf10e82b | 189 | if (!(inode->i_state & I_NEW)) |
4aeb664c NS |
190 | mark_inode_dirty_sync(inode); |
191 | } | |
192 | ||
1da177e4 LT |
193 | |
194 | /* | |
195 | * Pull the link count and size up from the xfs inode to the linux inode | |
196 | */ | |
197 | STATIC void | |
416c6d5b | 198 | xfs_validate_fields( |
6572bc28 | 199 | struct inode *inode) |
1da177e4 | 200 | { |
6572bc28 CH |
201 | struct xfs_inode *ip = XFS_I(inode); |
202 | loff_t size; | |
203 | ||
204 | inode->i_nlink = ip->i_d.di_nlink; | |
205 | inode->i_blocks = | |
206 | XFS_FSB_TO_BB(ip->i_mount, ip->i_d.di_nblocks + | |
207 | ip->i_delayed_blks); | |
208 | /* we're under i_sem so i_size can't change under us */ | |
209 | size = XFS_ISIZE(ip); | |
210 | if (i_size_read(inode) != size) | |
211 | i_size_write(inode, size); | |
1da177e4 LT |
212 | } |
213 | ||
446ada4a NS |
214 | /* |
215 | * Hook in SELinux. This is not quite correct yet, what we really need | |
216 | * here (as we do for default ACLs) is a mechanism by which creation of | |
217 | * these attrs can be journalled at inode creation time (along with the | |
218 | * inode, of course, such that log replay can't cause these to be lost). | |
219 | */ | |
220 | STATIC int | |
416c6d5b | 221 | xfs_init_security( |
67fcaa73 | 222 | bhv_vnode_t *vp, |
446ada4a NS |
223 | struct inode *dir) |
224 | { | |
ec86dc02 | 225 | struct inode *ip = vn_to_inode(vp); |
446ada4a NS |
226 | size_t length; |
227 | void *value; | |
228 | char *name; | |
229 | int error; | |
230 | ||
231 | error = security_inode_init_security(ip, dir, &name, &value, &length); | |
232 | if (error) { | |
233 | if (error == -EOPNOTSUPP) | |
234 | return 0; | |
235 | return -error; | |
236 | } | |
237 | ||
739bfb2a CH |
238 | error = xfs_attr_set(XFS_I(ip), name, value, |
239 | length, ATTR_SECURE); | |
446ada4a | 240 | if (!error) |
b3aea4ed | 241 | xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED); |
446ada4a NS |
242 | |
243 | kfree(name); | |
244 | kfree(value); | |
245 | return error; | |
246 | } | |
247 | ||
1da177e4 LT |
248 | /* |
249 | * Determine whether a process has a valid fs_struct (kernel daemons | |
250 | * like knfsd don't have an fs_struct). | |
251 | * | |
252 | * XXX(hch): nfsd is broken, better fix it instead. | |
253 | */ | |
7989cb8e | 254 | STATIC_INLINE int |
416c6d5b | 255 | xfs_has_fs_struct(struct task_struct *task) |
1da177e4 LT |
256 | { |
257 | return (task->fs != init_task.fs); | |
258 | } | |
259 | ||
7989cb8e | 260 | STATIC void |
416c6d5b | 261 | xfs_cleanup_inode( |
739bfb2a | 262 | struct inode *dir, |
67fcaa73 | 263 | bhv_vnode_t *vp, |
220b5284 | 264 | struct dentry *dentry, |
3a69c7dc YL |
265 | int mode) |
266 | { | |
267 | struct dentry teardown = {}; | |
3a69c7dc YL |
268 | |
269 | /* Oh, the horror. | |
220b5284 | 270 | * If we can't add the ACL or we fail in |
416c6d5b | 271 | * xfs_init_security we must back out. |
3a69c7dc YL |
272 | * ENOSPC can hit here, among other things. |
273 | */ | |
ec86dc02 | 274 | teardown.d_inode = vn_to_inode(vp); |
3a69c7dc YL |
275 | teardown.d_name = dentry->d_name; |
276 | ||
277 | if (S_ISDIR(mode)) | |
739bfb2a | 278 | xfs_rmdir(XFS_I(dir), &teardown); |
3a69c7dc | 279 | else |
739bfb2a | 280 | xfs_remove(XFS_I(dir), &teardown); |
3a69c7dc YL |
281 | VN_RELE(vp); |
282 | } | |
283 | ||
1da177e4 | 284 | STATIC int |
416c6d5b | 285 | xfs_vn_mknod( |
1da177e4 LT |
286 | struct inode *dir, |
287 | struct dentry *dentry, | |
288 | int mode, | |
289 | dev_t rdev) | |
290 | { | |
291 | struct inode *ip; | |
67fcaa73 | 292 | bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir); |
1da177e4 LT |
293 | xfs_acl_t *default_acl = NULL; |
294 | attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS; | |
295 | int error; | |
296 | ||
297 | /* | |
298 | * Irix uses Missed'em'V split, but doesn't want to see | |
299 | * the upper 5 bits of (14bit) major. | |
300 | */ | |
220b5284 | 301 | if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) |
1da177e4 LT |
302 | return -EINVAL; |
303 | ||
220b5284 NS |
304 | if (unlikely(test_default_acl && test_default_acl(dvp))) { |
305 | if (!_ACL_ALLOC(default_acl)) { | |
1da177e4 | 306 | return -ENOMEM; |
220b5284 | 307 | } |
1da177e4 LT |
308 | if (!_ACL_GET_DEFAULT(dvp, default_acl)) { |
309 | _ACL_FREE(default_acl); | |
310 | default_acl = NULL; | |
311 | } | |
312 | } | |
313 | ||
416c6d5b | 314 | if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current)) |
1da177e4 LT |
315 | mode &= ~current->fs->umask; |
316 | ||
1da177e4 LT |
317 | switch (mode & S_IFMT) { |
318 | case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK: | |
3e5daf05 | 319 | rdev = sysv_encode_dev(rdev); |
1da177e4 | 320 | case S_IFREG: |
3e5daf05 | 321 | error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL); |
1da177e4 LT |
322 | break; |
323 | case S_IFDIR: | |
3e5daf05 | 324 | error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL); |
1da177e4 LT |
325 | break; |
326 | default: | |
327 | error = EINVAL; | |
328 | break; | |
329 | } | |
330 | ||
220b5284 | 331 | if (unlikely(!error)) { |
416c6d5b | 332 | error = xfs_init_security(vp, dir); |
3a69c7dc | 333 | if (error) |
739bfb2a | 334 | xfs_cleanup_inode(dir, vp, dentry, mode); |
3a69c7dc | 335 | } |
446ada4a | 336 | |
220b5284 | 337 | if (unlikely(default_acl)) { |
1da177e4 | 338 | if (!error) { |
3e5daf05 | 339 | error = _ACL_INHERIT(vp, mode, default_acl); |
220b5284 | 340 | if (!error) |
0a74cd19 | 341 | xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED); |
3a69c7dc | 342 | else |
739bfb2a | 343 | xfs_cleanup_inode(dir, vp, dentry, mode); |
1da177e4 LT |
344 | } |
345 | _ACL_FREE(default_acl); | |
346 | } | |
347 | ||
220b5284 | 348 | if (likely(!error)) { |
1da177e4 | 349 | ASSERT(vp); |
ec86dc02 | 350 | ip = vn_to_inode(vp); |
1da177e4 | 351 | |
bad60fdd | 352 | if (S_ISDIR(mode)) |
6572bc28 | 353 | xfs_validate_fields(ip); |
1da177e4 | 354 | d_instantiate(dentry, ip); |
6572bc28 | 355 | xfs_validate_fields(dir); |
1da177e4 LT |
356 | } |
357 | return -error; | |
358 | } | |
359 | ||
360 | STATIC int | |
416c6d5b | 361 | xfs_vn_create( |
1da177e4 LT |
362 | struct inode *dir, |
363 | struct dentry *dentry, | |
364 | int mode, | |
365 | struct nameidata *nd) | |
366 | { | |
416c6d5b | 367 | return xfs_vn_mknod(dir, dentry, mode, 0); |
1da177e4 LT |
368 | } |
369 | ||
370 | STATIC int | |
416c6d5b | 371 | xfs_vn_mkdir( |
1da177e4 LT |
372 | struct inode *dir, |
373 | struct dentry *dentry, | |
374 | int mode) | |
375 | { | |
416c6d5b | 376 | return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0); |
1da177e4 LT |
377 | } |
378 | ||
379 | STATIC struct dentry * | |
416c6d5b | 380 | xfs_vn_lookup( |
1da177e4 LT |
381 | struct inode *dir, |
382 | struct dentry *dentry, | |
383 | struct nameidata *nd) | |
384 | { | |
739bfb2a | 385 | bhv_vnode_t *cvp; |
1da177e4 LT |
386 | int error; |
387 | ||
388 | if (dentry->d_name.len >= MAXNAMELEN) | |
389 | return ERR_PTR(-ENAMETOOLONG); | |
390 | ||
739bfb2a | 391 | error = xfs_lookup(XFS_I(dir), dentry, &cvp); |
67fcaa73 | 392 | if (unlikely(error)) { |
1da177e4 LT |
393 | if (unlikely(error != ENOENT)) |
394 | return ERR_PTR(-error); | |
395 | d_add(dentry, NULL); | |
396 | return NULL; | |
397 | } | |
398 | ||
ec86dc02 | 399 | return d_splice_alias(vn_to_inode(cvp), dentry); |
1da177e4 LT |
400 | } |
401 | ||
402 | STATIC int | |
416c6d5b | 403 | xfs_vn_link( |
1da177e4 LT |
404 | struct dentry *old_dentry, |
405 | struct inode *dir, | |
406 | struct dentry *dentry) | |
407 | { | |
408 | struct inode *ip; /* inode of guy being linked to */ | |
67fcaa73 | 409 | bhv_vnode_t *vp; /* vp of name being linked */ |
1da177e4 LT |
410 | int error; |
411 | ||
412 | ip = old_dentry->d_inode; /* inode being linked to */ | |
ec86dc02 | 413 | vp = vn_from_inode(ip); |
1da177e4 | 414 | |
97dfd70c | 415 | VN_HOLD(vp); |
739bfb2a | 416 | error = xfs_link(XFS_I(dir), vp, dentry); |
97dfd70c NS |
417 | if (unlikely(error)) { |
418 | VN_RELE(vp); | |
419 | } else { | |
b3aea4ed | 420 | xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED); |
6572bc28 | 421 | xfs_validate_fields(ip); |
1da177e4 LT |
422 | d_instantiate(dentry, ip); |
423 | } | |
424 | return -error; | |
425 | } | |
426 | ||
427 | STATIC int | |
416c6d5b | 428 | xfs_vn_unlink( |
1da177e4 LT |
429 | struct inode *dir, |
430 | struct dentry *dentry) | |
431 | { | |
432 | struct inode *inode; | |
1da177e4 LT |
433 | int error; |
434 | ||
435 | inode = dentry->d_inode; | |
1da177e4 | 436 | |
739bfb2a | 437 | error = xfs_remove(XFS_I(dir), dentry); |
220b5284 | 438 | if (likely(!error)) { |
6572bc28 CH |
439 | xfs_validate_fields(dir); /* size needs update */ |
440 | xfs_validate_fields(inode); | |
1da177e4 | 441 | } |
1da177e4 LT |
442 | return -error; |
443 | } | |
444 | ||
445 | STATIC int | |
416c6d5b | 446 | xfs_vn_symlink( |
1da177e4 LT |
447 | struct inode *dir, |
448 | struct dentry *dentry, | |
449 | const char *symname) | |
450 | { | |
451 | struct inode *ip; | |
67fcaa73 | 452 | bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */ |
1da177e4 | 453 | int error; |
3e5daf05 | 454 | mode_t mode; |
1da177e4 | 455 | |
1da177e4 LT |
456 | cvp = NULL; |
457 | ||
3e5daf05 | 458 | mode = S_IFLNK | |
0432dab2 | 459 | (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO); |
1da177e4 | 460 | |
3e5daf05 CH |
461 | error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode, |
462 | &cvp, NULL); | |
54245702 | 463 | if (likely(!error && cvp)) { |
416c6d5b | 464 | error = xfs_init_security(cvp, dir); |
54245702 | 465 | if (likely(!error)) { |
ec86dc02 | 466 | ip = vn_to_inode(cvp); |
54245702 | 467 | d_instantiate(dentry, ip); |
6572bc28 CH |
468 | xfs_validate_fields(dir); |
469 | xfs_validate_fields(ip); | |
ce9d37c2 | 470 | } else { |
739bfb2a | 471 | xfs_cleanup_inode(dir, cvp, dentry, 0); |
54245702 | 472 | } |
1da177e4 LT |
473 | } |
474 | return -error; | |
475 | } | |
476 | ||
477 | STATIC int | |
416c6d5b | 478 | xfs_vn_rmdir( |
1da177e4 LT |
479 | struct inode *dir, |
480 | struct dentry *dentry) | |
481 | { | |
482 | struct inode *inode = dentry->d_inode; | |
1da177e4 LT |
483 | int error; |
484 | ||
739bfb2a | 485 | error = xfs_rmdir(XFS_I(dir), dentry); |
220b5284 | 486 | if (likely(!error)) { |
6572bc28 CH |
487 | xfs_validate_fields(inode); |
488 | xfs_validate_fields(dir); | |
1da177e4 LT |
489 | } |
490 | return -error; | |
491 | } | |
492 | ||
493 | STATIC int | |
416c6d5b | 494 | xfs_vn_rename( |
1da177e4 LT |
495 | struct inode *odir, |
496 | struct dentry *odentry, | |
497 | struct inode *ndir, | |
498 | struct dentry *ndentry) | |
499 | { | |
500 | struct inode *new_inode = ndentry->d_inode; | |
67fcaa73 | 501 | bhv_vnode_t *tvp; /* target directory */ |
1da177e4 LT |
502 | int error; |
503 | ||
ec86dc02 | 504 | tvp = vn_from_inode(ndir); |
1da177e4 | 505 | |
739bfb2a | 506 | error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry); |
220b5284 NS |
507 | if (likely(!error)) { |
508 | if (new_inode) | |
6572bc28 CH |
509 | xfs_validate_fields(new_inode); |
510 | xfs_validate_fields(odir); | |
220b5284 | 511 | if (ndir != odir) |
6572bc28 | 512 | xfs_validate_fields(ndir); |
220b5284 | 513 | } |
220b5284 | 514 | return -error; |
1da177e4 LT |
515 | } |
516 | ||
517 | /* | |
518 | * careful here - this function can get called recursively, so | |
519 | * we need to be very careful about how much stack we use. | |
520 | * uio is kmalloced for this reason... | |
521 | */ | |
008b150a | 522 | STATIC void * |
416c6d5b | 523 | xfs_vn_follow_link( |
1da177e4 LT |
524 | struct dentry *dentry, |
525 | struct nameidata *nd) | |
526 | { | |
1da177e4 | 527 | char *link; |
804c83c3 | 528 | int error = -ENOMEM; |
1da177e4 | 529 | |
f52720ca | 530 | link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
804c83c3 CH |
531 | if (!link) |
532 | goto out_err; | |
1da177e4 | 533 | |
739bfb2a | 534 | error = -xfs_readlink(XFS_I(dentry->d_inode), link); |
804c83c3 CH |
535 | if (unlikely(error)) |
536 | goto out_kfree; | |
1da177e4 LT |
537 | |
538 | nd_set_link(nd, link); | |
008b150a | 539 | return NULL; |
804c83c3 CH |
540 | |
541 | out_kfree: | |
542 | kfree(link); | |
543 | out_err: | |
544 | nd_set_link(nd, ERR_PTR(error)); | |
545 | return NULL; | |
1da177e4 LT |
546 | } |
547 | ||
cde410a9 | 548 | STATIC void |
416c6d5b | 549 | xfs_vn_put_link( |
cde410a9 NS |
550 | struct dentry *dentry, |
551 | struct nameidata *nd, | |
552 | void *p) | |
1da177e4 | 553 | { |
cde410a9 NS |
554 | char *s = nd_get_link(nd); |
555 | ||
1da177e4 LT |
556 | if (!IS_ERR(s)) |
557 | kfree(s); | |
558 | } | |
559 | ||
560 | #ifdef CONFIG_XFS_POSIX_ACL | |
561 | STATIC int | |
416c6d5b | 562 | xfs_vn_permission( |
1da177e4 LT |
563 | struct inode *inode, |
564 | int mode, | |
565 | struct nameidata *nd) | |
566 | { | |
739bfb2a | 567 | return -xfs_access(XFS_I(inode), mode << 6, NULL); |
1da177e4 LT |
568 | } |
569 | #else | |
416c6d5b | 570 | #define xfs_vn_permission NULL |
1da177e4 LT |
571 | #endif |
572 | ||
573 | STATIC int | |
416c6d5b | 574 | xfs_vn_getattr( |
c43f4087 CH |
575 | struct vfsmount *mnt, |
576 | struct dentry *dentry, | |
577 | struct kstat *stat) | |
1da177e4 | 578 | { |
c43f4087 CH |
579 | struct inode *inode = dentry->d_inode; |
580 | struct xfs_inode *ip = XFS_I(inode); | |
581 | struct xfs_mount *mp = ip->i_mount; | |
582 | ||
583 | xfs_itrace_entry(ip); | |
584 | ||
585 | if (XFS_FORCED_SHUTDOWN(mp)) | |
586 | return XFS_ERROR(EIO); | |
587 | ||
588 | stat->size = XFS_ISIZE(ip); | |
589 | stat->dev = inode->i_sb->s_dev; | |
590 | stat->mode = ip->i_d.di_mode; | |
591 | stat->nlink = ip->i_d.di_nlink; | |
592 | stat->uid = ip->i_d.di_uid; | |
593 | stat->gid = ip->i_d.di_gid; | |
594 | stat->ino = ip->i_ino; | |
595 | #if XFS_BIG_INUMS | |
596 | stat->ino += mp->m_inoadd; | |
597 | #endif | |
598 | stat->atime = inode->i_atime; | |
599 | stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec; | |
600 | stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; | |
601 | stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec; | |
602 | stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; | |
603 | stat->blocks = | |
604 | XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); | |
605 | ||
606 | ||
607 | switch (inode->i_mode & S_IFMT) { | |
608 | case S_IFBLK: | |
609 | case S_IFCHR: | |
610 | stat->blksize = BLKDEV_IOSIZE; | |
611 | stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, | |
612 | sysv_minor(ip->i_df.if_u2.if_rdev)); | |
613 | break; | |
614 | default: | |
615 | if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) { | |
616 | /* | |
617 | * If the file blocks are being allocated from a | |
618 | * realtime volume, then return the inode's realtime | |
619 | * extent size or the realtime volume's extent size. | |
620 | */ | |
621 | stat->blksize = | |
622 | xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog; | |
623 | } else | |
624 | stat->blksize = xfs_preferred_iosize(mp); | |
625 | stat->rdev = 0; | |
626 | break; | |
69e23b9a | 627 | } |
c43f4087 CH |
628 | |
629 | return 0; | |
1da177e4 LT |
630 | } |
631 | ||
632 | STATIC int | |
416c6d5b | 633 | xfs_vn_setattr( |
1da177e4 LT |
634 | struct dentry *dentry, |
635 | struct iattr *attr) | |
636 | { | |
637 | struct inode *inode = dentry->d_inode; | |
638 | unsigned int ia_valid = attr->ia_valid; | |
8285fb58 | 639 | bhv_vattr_t vattr = { 0 }; |
1da177e4 LT |
640 | int flags = 0; |
641 | int error; | |
642 | ||
1da177e4 LT |
643 | if (ia_valid & ATTR_UID) { |
644 | vattr.va_mask |= XFS_AT_UID; | |
645 | vattr.va_uid = attr->ia_uid; | |
646 | } | |
647 | if (ia_valid & ATTR_GID) { | |
648 | vattr.va_mask |= XFS_AT_GID; | |
649 | vattr.va_gid = attr->ia_gid; | |
650 | } | |
651 | if (ia_valid & ATTR_SIZE) { | |
652 | vattr.va_mask |= XFS_AT_SIZE; | |
653 | vattr.va_size = attr->ia_size; | |
654 | } | |
655 | if (ia_valid & ATTR_ATIME) { | |
656 | vattr.va_mask |= XFS_AT_ATIME; | |
657 | vattr.va_atime = attr->ia_atime; | |
8c0b5113 | 658 | inode->i_atime = attr->ia_atime; |
1da177e4 LT |
659 | } |
660 | if (ia_valid & ATTR_MTIME) { | |
661 | vattr.va_mask |= XFS_AT_MTIME; | |
662 | vattr.va_mtime = attr->ia_mtime; | |
663 | } | |
664 | if (ia_valid & ATTR_CTIME) { | |
665 | vattr.va_mask |= XFS_AT_CTIME; | |
666 | vattr.va_ctime = attr->ia_ctime; | |
667 | } | |
668 | if (ia_valid & ATTR_MODE) { | |
669 | vattr.va_mask |= XFS_AT_MODE; | |
670 | vattr.va_mode = attr->ia_mode; | |
671 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | |
672 | inode->i_mode &= ~S_ISGID; | |
673 | } | |
674 | ||
675 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) | |
676 | flags |= ATTR_UTIME; | |
677 | #ifdef ATTR_NO_BLOCK | |
678 | if ((ia_valid & ATTR_NO_BLOCK)) | |
679 | flags |= ATTR_NONBLOCK; | |
680 | #endif | |
681 | ||
739bfb2a | 682 | error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL); |
220b5284 | 683 | if (likely(!error)) |
21a62542 | 684 | vn_revalidate(vn_from_inode(inode)); |
220b5284 | 685 | return -error; |
1da177e4 LT |
686 | } |
687 | ||
688 | STATIC void | |
416c6d5b | 689 | xfs_vn_truncate( |
1da177e4 LT |
690 | struct inode *inode) |
691 | { | |
c2536668 | 692 | block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks); |
1da177e4 LT |
693 | } |
694 | ||
695 | STATIC int | |
416c6d5b | 696 | xfs_vn_setxattr( |
1da177e4 LT |
697 | struct dentry *dentry, |
698 | const char *name, | |
699 | const void *data, | |
700 | size_t size, | |
701 | int flags) | |
702 | { | |
67fcaa73 | 703 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
704 | char *attr = (char *)name; |
705 | attrnames_t *namesp; | |
706 | int xflags = 0; | |
707 | int error; | |
708 | ||
709 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
710 | if (!namesp) | |
711 | return -EOPNOTSUPP; | |
712 | attr += namesp->attr_namelen; | |
713 | error = namesp->attr_capable(vp, NULL); | |
714 | if (error) | |
715 | return error; | |
716 | ||
717 | /* Convert Linux syscall to XFS internal ATTR flags */ | |
718 | if (flags & XATTR_CREATE) | |
719 | xflags |= ATTR_CREATE; | |
720 | if (flags & XATTR_REPLACE) | |
721 | xflags |= ATTR_REPLACE; | |
722 | xflags |= namesp->attr_flag; | |
723 | return namesp->attr_set(vp, attr, (void *)data, size, xflags); | |
724 | } | |
725 | ||
726 | STATIC ssize_t | |
416c6d5b | 727 | xfs_vn_getxattr( |
1da177e4 LT |
728 | struct dentry *dentry, |
729 | const char *name, | |
730 | void *data, | |
731 | size_t size) | |
732 | { | |
67fcaa73 | 733 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
734 | char *attr = (char *)name; |
735 | attrnames_t *namesp; | |
736 | int xflags = 0; | |
737 | ssize_t error; | |
738 | ||
739 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
740 | if (!namesp) | |
741 | return -EOPNOTSUPP; | |
742 | attr += namesp->attr_namelen; | |
743 | error = namesp->attr_capable(vp, NULL); | |
744 | if (error) | |
745 | return error; | |
746 | ||
747 | /* Convert Linux syscall to XFS internal ATTR flags */ | |
748 | if (!size) { | |
749 | xflags |= ATTR_KERNOVAL; | |
750 | data = NULL; | |
751 | } | |
752 | xflags |= namesp->attr_flag; | |
753 | return namesp->attr_get(vp, attr, (void *)data, size, xflags); | |
754 | } | |
755 | ||
756 | STATIC ssize_t | |
416c6d5b | 757 | xfs_vn_listxattr( |
1da177e4 LT |
758 | struct dentry *dentry, |
759 | char *data, | |
760 | size_t size) | |
761 | { | |
67fcaa73 | 762 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
763 | int error, xflags = ATTR_KERNAMELS; |
764 | ssize_t result; | |
765 | ||
766 | if (!size) | |
767 | xflags |= ATTR_KERNOVAL; | |
768 | xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS; | |
769 | ||
770 | error = attr_generic_list(vp, data, size, xflags, &result); | |
771 | if (error < 0) | |
772 | return error; | |
773 | return result; | |
774 | } | |
775 | ||
776 | STATIC int | |
416c6d5b | 777 | xfs_vn_removexattr( |
1da177e4 LT |
778 | struct dentry *dentry, |
779 | const char *name) | |
780 | { | |
67fcaa73 | 781 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
782 | char *attr = (char *)name; |
783 | attrnames_t *namesp; | |
784 | int xflags = 0; | |
785 | int error; | |
786 | ||
787 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
788 | if (!namesp) | |
789 | return -EOPNOTSUPP; | |
790 | attr += namesp->attr_namelen; | |
791 | error = namesp->attr_capable(vp, NULL); | |
792 | if (error) | |
793 | return error; | |
794 | xflags |= namesp->attr_flag; | |
795 | return namesp->attr_remove(vp, attr, xflags); | |
796 | } | |
797 | ||
3ed65264 DC |
798 | STATIC long |
799 | xfs_vn_fallocate( | |
800 | struct inode *inode, | |
801 | int mode, | |
802 | loff_t offset, | |
803 | loff_t len) | |
804 | { | |
805 | long error; | |
806 | loff_t new_size = 0; | |
807 | xfs_flock64_t bf; | |
808 | xfs_inode_t *ip = XFS_I(inode); | |
809 | ||
810 | /* preallocation on directories not yet supported */ | |
811 | error = -ENODEV; | |
812 | if (S_ISDIR(inode->i_mode)) | |
813 | goto out_error; | |
814 | ||
815 | bf.l_whence = 0; | |
816 | bf.l_start = offset; | |
817 | bf.l_len = len; | |
818 | ||
819 | xfs_ilock(ip, XFS_IOLOCK_EXCL); | |
820 | error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf, | |
821 | 0, NULL, ATTR_NOLOCK); | |
822 | if (!error && !(mode & FALLOC_FL_KEEP_SIZE) && | |
823 | offset + len > i_size_read(inode)) | |
824 | new_size = offset + len; | |
825 | ||
826 | /* Change file size if needed */ | |
827 | if (new_size) { | |
828 | bhv_vattr_t va; | |
829 | ||
830 | va.va_mask = XFS_AT_SIZE; | |
831 | va.va_size = new_size; | |
832 | error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL); | |
833 | } | |
834 | ||
835 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); | |
836 | out_error: | |
837 | return error; | |
838 | } | |
1da177e4 | 839 | |
c5ef1c42 | 840 | const struct inode_operations xfs_inode_operations = { |
416c6d5b NS |
841 | .permission = xfs_vn_permission, |
842 | .truncate = xfs_vn_truncate, | |
843 | .getattr = xfs_vn_getattr, | |
844 | .setattr = xfs_vn_setattr, | |
845 | .setxattr = xfs_vn_setxattr, | |
846 | .getxattr = xfs_vn_getxattr, | |
847 | .listxattr = xfs_vn_listxattr, | |
848 | .removexattr = xfs_vn_removexattr, | |
3ed65264 | 849 | .fallocate = xfs_vn_fallocate, |
1da177e4 LT |
850 | }; |
851 | ||
c5ef1c42 | 852 | const struct inode_operations xfs_dir_inode_operations = { |
416c6d5b NS |
853 | .create = xfs_vn_create, |
854 | .lookup = xfs_vn_lookup, | |
855 | .link = xfs_vn_link, | |
856 | .unlink = xfs_vn_unlink, | |
857 | .symlink = xfs_vn_symlink, | |
858 | .mkdir = xfs_vn_mkdir, | |
859 | .rmdir = xfs_vn_rmdir, | |
860 | .mknod = xfs_vn_mknod, | |
861 | .rename = xfs_vn_rename, | |
862 | .permission = xfs_vn_permission, | |
863 | .getattr = xfs_vn_getattr, | |
864 | .setattr = xfs_vn_setattr, | |
865 | .setxattr = xfs_vn_setxattr, | |
866 | .getxattr = xfs_vn_getxattr, | |
867 | .listxattr = xfs_vn_listxattr, | |
868 | .removexattr = xfs_vn_removexattr, | |
1da177e4 LT |
869 | }; |
870 | ||
c5ef1c42 | 871 | const struct inode_operations xfs_symlink_inode_operations = { |
1da177e4 | 872 | .readlink = generic_readlink, |
416c6d5b NS |
873 | .follow_link = xfs_vn_follow_link, |
874 | .put_link = xfs_vn_put_link, | |
875 | .permission = xfs_vn_permission, | |
876 | .getattr = xfs_vn_getattr, | |
877 | .setattr = xfs_vn_setattr, | |
878 | .setxattr = xfs_vn_setxattr, | |
879 | .getxattr = xfs_vn_getxattr, | |
880 | .listxattr = xfs_vn_listxattr, | |
881 | .removexattr = xfs_vn_removexattr, | |
1da177e4 | 882 | }; |