]>
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 | ||
6572bc28 CH |
204 | /* 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); | |
1da177e4 LT |
208 | } |
209 | ||
446ada4a NS |
210 | /* |
211 | * 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 | */ | |
216 | STATIC int | |
416c6d5b | 217 | xfs_init_security( |
67fcaa73 | 218 | bhv_vnode_t *vp, |
446ada4a NS |
219 | struct inode *dir) |
220 | { | |
ec86dc02 | 221 | struct inode *ip = vn_to_inode(vp); |
446ada4a NS |
222 | 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 | ||
739bfb2a CH |
234 | error = xfs_attr_set(XFS_I(ip), name, value, |
235 | length, ATTR_SECURE); | |
446ada4a | 236 | if (!error) |
b3aea4ed | 237 | xfs_iflags_set(XFS_I(ip), XFS_IMODIFIED); |
446ada4a NS |
238 | |
239 | kfree(name); | |
240 | kfree(value); | |
241 | return error; | |
242 | } | |
243 | ||
1da177e4 LT |
244 | /* |
245 | * Determine whether a process has a valid fs_struct (kernel daemons | |
246 | * like knfsd don't have an fs_struct). | |
247 | * | |
248 | * XXX(hch): nfsd is broken, better fix it instead. | |
249 | */ | |
7989cb8e | 250 | STATIC_INLINE int |
416c6d5b | 251 | xfs_has_fs_struct(struct task_struct *task) |
1da177e4 LT |
252 | { |
253 | return (task->fs != init_task.fs); | |
254 | } | |
255 | ||
7989cb8e | 256 | STATIC void |
416c6d5b | 257 | xfs_cleanup_inode( |
739bfb2a | 258 | struct inode *dir, |
67fcaa73 | 259 | bhv_vnode_t *vp, |
220b5284 | 260 | struct dentry *dentry, |
3a69c7dc YL |
261 | int mode) |
262 | { | |
263 | struct dentry teardown = {}; | |
3a69c7dc YL |
264 | |
265 | /* Oh, the horror. | |
220b5284 | 266 | * If we can't add the ACL or we fail in |
416c6d5b | 267 | * xfs_init_security we must back out. |
3a69c7dc YL |
268 | * ENOSPC can hit here, among other things. |
269 | */ | |
ec86dc02 | 270 | teardown.d_inode = vn_to_inode(vp); |
3a69c7dc YL |
271 | teardown.d_name = dentry->d_name; |
272 | ||
273 | if (S_ISDIR(mode)) | |
739bfb2a | 274 | xfs_rmdir(XFS_I(dir), &teardown); |
3a69c7dc | 275 | else |
739bfb2a | 276 | xfs_remove(XFS_I(dir), &teardown); |
3a69c7dc YL |
277 | VN_RELE(vp); |
278 | } | |
279 | ||
1da177e4 | 280 | STATIC int |
416c6d5b | 281 | xfs_vn_mknod( |
1da177e4 LT |
282 | struct inode *dir, |
283 | struct dentry *dentry, | |
284 | int mode, | |
285 | dev_t rdev) | |
286 | { | |
287 | struct inode *ip; | |
67fcaa73 | 288 | bhv_vnode_t *vp = NULL, *dvp = vn_from_inode(dir); |
1da177e4 LT |
289 | xfs_acl_t *default_acl = NULL; |
290 | attrexists_t test_default_acl = _ACL_DEFAULT_EXISTS; | |
291 | int error; | |
292 | ||
293 | /* | |
294 | * Irix uses Missed'em'V split, but doesn't want to see | |
295 | * the upper 5 bits of (14bit) major. | |
296 | */ | |
220b5284 | 297 | if (unlikely(!sysv_valid_dev(rdev) || MAJOR(rdev) & ~0x1ff)) |
1da177e4 LT |
298 | return -EINVAL; |
299 | ||
220b5284 NS |
300 | if (unlikely(test_default_acl && test_default_acl(dvp))) { |
301 | if (!_ACL_ALLOC(default_acl)) { | |
1da177e4 | 302 | return -ENOMEM; |
220b5284 | 303 | } |
1da177e4 LT |
304 | if (!_ACL_GET_DEFAULT(dvp, default_acl)) { |
305 | _ACL_FREE(default_acl); | |
306 | default_acl = NULL; | |
307 | } | |
308 | } | |
309 | ||
416c6d5b | 310 | if (IS_POSIXACL(dir) && !default_acl && xfs_has_fs_struct(current)) |
1da177e4 LT |
311 | mode &= ~current->fs->umask; |
312 | ||
1da177e4 LT |
313 | switch (mode & S_IFMT) { |
314 | case S_IFCHR: case S_IFBLK: case S_IFIFO: case S_IFSOCK: | |
3e5daf05 | 315 | rdev = sysv_encode_dev(rdev); |
1da177e4 | 316 | case S_IFREG: |
3e5daf05 | 317 | error = xfs_create(XFS_I(dir), dentry, mode, rdev, &vp, NULL); |
1da177e4 LT |
318 | break; |
319 | case S_IFDIR: | |
3e5daf05 | 320 | error = xfs_mkdir(XFS_I(dir), dentry, mode, &vp, NULL); |
1da177e4 LT |
321 | break; |
322 | default: | |
323 | error = EINVAL; | |
324 | break; | |
325 | } | |
326 | ||
220b5284 | 327 | if (unlikely(!error)) { |
416c6d5b | 328 | error = xfs_init_security(vp, dir); |
3a69c7dc | 329 | if (error) |
739bfb2a | 330 | xfs_cleanup_inode(dir, vp, dentry, mode); |
3a69c7dc | 331 | } |
446ada4a | 332 | |
220b5284 | 333 | if (unlikely(default_acl)) { |
1da177e4 | 334 | if (!error) { |
3e5daf05 | 335 | error = _ACL_INHERIT(vp, mode, default_acl); |
220b5284 | 336 | if (!error) |
0a74cd19 | 337 | xfs_iflags_set(XFS_I(vp), XFS_IMODIFIED); |
3a69c7dc | 338 | else |
739bfb2a | 339 | xfs_cleanup_inode(dir, vp, dentry, mode); |
1da177e4 LT |
340 | } |
341 | _ACL_FREE(default_acl); | |
342 | } | |
343 | ||
220b5284 | 344 | if (likely(!error)) { |
1da177e4 | 345 | ASSERT(vp); |
ec86dc02 | 346 | ip = vn_to_inode(vp); |
1da177e4 | 347 | |
bad60fdd | 348 | if (S_ISDIR(mode)) |
6572bc28 | 349 | xfs_validate_fields(ip); |
1da177e4 | 350 | d_instantiate(dentry, ip); |
6572bc28 | 351 | xfs_validate_fields(dir); |
1da177e4 LT |
352 | } |
353 | return -error; | |
354 | } | |
355 | ||
356 | STATIC int | |
416c6d5b | 357 | xfs_vn_create( |
1da177e4 LT |
358 | struct inode *dir, |
359 | struct dentry *dentry, | |
360 | int mode, | |
361 | struct nameidata *nd) | |
362 | { | |
416c6d5b | 363 | return xfs_vn_mknod(dir, dentry, mode, 0); |
1da177e4 LT |
364 | } |
365 | ||
366 | STATIC int | |
416c6d5b | 367 | xfs_vn_mkdir( |
1da177e4 LT |
368 | struct inode *dir, |
369 | struct dentry *dentry, | |
370 | int mode) | |
371 | { | |
416c6d5b | 372 | return xfs_vn_mknod(dir, dentry, mode|S_IFDIR, 0); |
1da177e4 LT |
373 | } |
374 | ||
375 | STATIC struct dentry * | |
416c6d5b | 376 | xfs_vn_lookup( |
1da177e4 LT |
377 | struct inode *dir, |
378 | struct dentry *dentry, | |
379 | struct nameidata *nd) | |
380 | { | |
739bfb2a | 381 | bhv_vnode_t *cvp; |
1da177e4 LT |
382 | int error; |
383 | ||
384 | if (dentry->d_name.len >= MAXNAMELEN) | |
385 | return ERR_PTR(-ENAMETOOLONG); | |
386 | ||
739bfb2a | 387 | error = xfs_lookup(XFS_I(dir), dentry, &cvp); |
67fcaa73 | 388 | if (unlikely(error)) { |
1da177e4 LT |
389 | if (unlikely(error != ENOENT)) |
390 | return ERR_PTR(-error); | |
391 | d_add(dentry, NULL); | |
392 | return NULL; | |
393 | } | |
394 | ||
ec86dc02 | 395 | return d_splice_alias(vn_to_inode(cvp), dentry); |
1da177e4 LT |
396 | } |
397 | ||
398 | STATIC int | |
416c6d5b | 399 | xfs_vn_link( |
1da177e4 LT |
400 | struct dentry *old_dentry, |
401 | struct inode *dir, | |
402 | struct dentry *dentry) | |
403 | { | |
404 | struct inode *ip; /* inode of guy being linked to */ | |
67fcaa73 | 405 | bhv_vnode_t *vp; /* vp of name being linked */ |
1da177e4 LT |
406 | int error; |
407 | ||
408 | ip = old_dentry->d_inode; /* inode being linked to */ | |
ec86dc02 | 409 | vp = vn_from_inode(ip); |
1da177e4 | 410 | |
97dfd70c | 411 | VN_HOLD(vp); |
739bfb2a | 412 | error = xfs_link(XFS_I(dir), vp, dentry); |
97dfd70c NS |
413 | if (unlikely(error)) { |
414 | VN_RELE(vp); | |
415 | } else { | |
b3aea4ed | 416 | xfs_iflags_set(XFS_I(dir), XFS_IMODIFIED); |
6572bc28 | 417 | xfs_validate_fields(ip); |
1da177e4 LT |
418 | d_instantiate(dentry, ip); |
419 | } | |
420 | return -error; | |
421 | } | |
422 | ||
423 | STATIC int | |
416c6d5b | 424 | xfs_vn_unlink( |
1da177e4 LT |
425 | struct inode *dir, |
426 | struct dentry *dentry) | |
427 | { | |
428 | struct inode *inode; | |
1da177e4 LT |
429 | int error; |
430 | ||
431 | inode = dentry->d_inode; | |
1da177e4 | 432 | |
739bfb2a | 433 | error = xfs_remove(XFS_I(dir), dentry); |
220b5284 | 434 | if (likely(!error)) { |
6572bc28 CH |
435 | xfs_validate_fields(dir); /* size needs update */ |
436 | xfs_validate_fields(inode); | |
1da177e4 | 437 | } |
1da177e4 LT |
438 | return -error; |
439 | } | |
440 | ||
441 | STATIC int | |
416c6d5b | 442 | xfs_vn_symlink( |
1da177e4 LT |
443 | struct inode *dir, |
444 | struct dentry *dentry, | |
445 | const char *symname) | |
446 | { | |
447 | struct inode *ip; | |
67fcaa73 | 448 | bhv_vnode_t *cvp; /* used to lookup symlink to put in dentry */ |
1da177e4 | 449 | int error; |
3e5daf05 | 450 | mode_t mode; |
1da177e4 | 451 | |
1da177e4 LT |
452 | cvp = NULL; |
453 | ||
3e5daf05 | 454 | mode = S_IFLNK | |
0432dab2 | 455 | (irix_symlink_mode ? 0777 & ~current->fs->umask : S_IRWXUGO); |
1da177e4 | 456 | |
3e5daf05 CH |
457 | error = xfs_symlink(XFS_I(dir), dentry, (char *)symname, mode, |
458 | &cvp, NULL); | |
54245702 | 459 | if (likely(!error && cvp)) { |
416c6d5b | 460 | error = xfs_init_security(cvp, dir); |
54245702 | 461 | if (likely(!error)) { |
ec86dc02 | 462 | ip = vn_to_inode(cvp); |
54245702 | 463 | d_instantiate(dentry, ip); |
6572bc28 CH |
464 | xfs_validate_fields(dir); |
465 | xfs_validate_fields(ip); | |
ce9d37c2 | 466 | } else { |
739bfb2a | 467 | xfs_cleanup_inode(dir, cvp, dentry, 0); |
54245702 | 468 | } |
1da177e4 LT |
469 | } |
470 | return -error; | |
471 | } | |
472 | ||
473 | STATIC int | |
416c6d5b | 474 | xfs_vn_rmdir( |
1da177e4 LT |
475 | struct inode *dir, |
476 | struct dentry *dentry) | |
477 | { | |
478 | struct inode *inode = dentry->d_inode; | |
1da177e4 LT |
479 | int error; |
480 | ||
739bfb2a | 481 | error = xfs_rmdir(XFS_I(dir), dentry); |
220b5284 | 482 | if (likely(!error)) { |
6572bc28 CH |
483 | xfs_validate_fields(inode); |
484 | xfs_validate_fields(dir); | |
1da177e4 LT |
485 | } |
486 | return -error; | |
487 | } | |
488 | ||
489 | STATIC int | |
416c6d5b | 490 | xfs_vn_rename( |
1da177e4 LT |
491 | struct inode *odir, |
492 | struct dentry *odentry, | |
493 | struct inode *ndir, | |
494 | struct dentry *ndentry) | |
495 | { | |
496 | struct inode *new_inode = ndentry->d_inode; | |
67fcaa73 | 497 | bhv_vnode_t *tvp; /* target directory */ |
1da177e4 LT |
498 | int error; |
499 | ||
ec86dc02 | 500 | tvp = vn_from_inode(ndir); |
1da177e4 | 501 | |
739bfb2a | 502 | error = xfs_rename(XFS_I(odir), odentry, tvp, ndentry); |
220b5284 NS |
503 | if (likely(!error)) { |
504 | if (new_inode) | |
6572bc28 CH |
505 | xfs_validate_fields(new_inode); |
506 | xfs_validate_fields(odir); | |
220b5284 | 507 | if (ndir != odir) |
6572bc28 | 508 | xfs_validate_fields(ndir); |
220b5284 | 509 | } |
220b5284 | 510 | return -error; |
1da177e4 LT |
511 | } |
512 | ||
513 | /* | |
514 | * careful here - this function can get called recursively, so | |
515 | * we need to be very careful about how much stack we use. | |
516 | * uio is kmalloced for this reason... | |
517 | */ | |
008b150a | 518 | STATIC void * |
416c6d5b | 519 | xfs_vn_follow_link( |
1da177e4 LT |
520 | struct dentry *dentry, |
521 | struct nameidata *nd) | |
522 | { | |
1da177e4 | 523 | char *link; |
804c83c3 | 524 | int error = -ENOMEM; |
1da177e4 | 525 | |
f52720ca | 526 | link = kmalloc(MAXPATHLEN+1, GFP_KERNEL); |
804c83c3 CH |
527 | if (!link) |
528 | goto out_err; | |
1da177e4 | 529 | |
739bfb2a | 530 | error = -xfs_readlink(XFS_I(dentry->d_inode), link); |
804c83c3 CH |
531 | if (unlikely(error)) |
532 | goto out_kfree; | |
1da177e4 LT |
533 | |
534 | nd_set_link(nd, link); | |
008b150a | 535 | return NULL; |
804c83c3 CH |
536 | |
537 | out_kfree: | |
538 | kfree(link); | |
539 | out_err: | |
540 | nd_set_link(nd, ERR_PTR(error)); | |
541 | return NULL; | |
1da177e4 LT |
542 | } |
543 | ||
cde410a9 | 544 | STATIC void |
416c6d5b | 545 | xfs_vn_put_link( |
cde410a9 NS |
546 | struct dentry *dentry, |
547 | struct nameidata *nd, | |
548 | void *p) | |
1da177e4 | 549 | { |
cde410a9 NS |
550 | char *s = nd_get_link(nd); |
551 | ||
1da177e4 LT |
552 | if (!IS_ERR(s)) |
553 | kfree(s); | |
554 | } | |
555 | ||
556 | #ifdef CONFIG_XFS_POSIX_ACL | |
4576758d CH |
557 | STATIC int |
558 | xfs_check_acl( | |
559 | struct inode *inode, | |
560 | int mask) | |
561 | { | |
562 | struct xfs_inode *ip = XFS_I(inode); | |
563 | int error; | |
564 | ||
565 | xfs_itrace_entry(ip); | |
566 | ||
567 | if (XFS_IFORK_Q(ip)) { | |
568 | error = xfs_acl_iaccess(ip, mask, NULL); | |
569 | if (error != -1) | |
570 | return -error; | |
571 | } | |
572 | ||
573 | return -EAGAIN; | |
574 | } | |
575 | ||
1da177e4 | 576 | STATIC int |
416c6d5b | 577 | xfs_vn_permission( |
4576758d CH |
578 | struct inode *inode, |
579 | int mask, | |
580 | struct nameidata *nd) | |
1da177e4 | 581 | { |
4576758d | 582 | return generic_permission(inode, mask, xfs_check_acl); |
1da177e4 LT |
583 | } |
584 | #else | |
416c6d5b | 585 | #define xfs_vn_permission NULL |
1da177e4 LT |
586 | #endif |
587 | ||
588 | STATIC int | |
416c6d5b | 589 | xfs_vn_getattr( |
c43f4087 CH |
590 | struct vfsmount *mnt, |
591 | struct dentry *dentry, | |
592 | struct kstat *stat) | |
1da177e4 | 593 | { |
c43f4087 CH |
594 | struct inode *inode = dentry->d_inode; |
595 | struct xfs_inode *ip = XFS_I(inode); | |
596 | struct xfs_mount *mp = ip->i_mount; | |
597 | ||
598 | xfs_itrace_entry(ip); | |
599 | ||
600 | if (XFS_FORCED_SHUTDOWN(mp)) | |
601 | return XFS_ERROR(EIO); | |
602 | ||
603 | stat->size = XFS_ISIZE(ip); | |
604 | stat->dev = inode->i_sb->s_dev; | |
605 | stat->mode = ip->i_d.di_mode; | |
606 | stat->nlink = ip->i_d.di_nlink; | |
607 | stat->uid = ip->i_d.di_uid; | |
608 | stat->gid = ip->i_d.di_gid; | |
609 | stat->ino = ip->i_ino; | |
610 | #if XFS_BIG_INUMS | |
611 | stat->ino += mp->m_inoadd; | |
612 | #endif | |
613 | stat->atime = inode->i_atime; | |
614 | stat->mtime.tv_sec = ip->i_d.di_mtime.t_sec; | |
615 | stat->mtime.tv_nsec = ip->i_d.di_mtime.t_nsec; | |
616 | stat->ctime.tv_sec = ip->i_d.di_ctime.t_sec; | |
617 | stat->ctime.tv_nsec = ip->i_d.di_ctime.t_nsec; | |
618 | stat->blocks = | |
619 | XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks); | |
620 | ||
621 | ||
622 | switch (inode->i_mode & S_IFMT) { | |
623 | case S_IFBLK: | |
624 | case S_IFCHR: | |
625 | stat->blksize = BLKDEV_IOSIZE; | |
626 | stat->rdev = MKDEV(sysv_major(ip->i_df.if_u2.if_rdev) & 0x1ff, | |
627 | sysv_minor(ip->i_df.if_u2.if_rdev)); | |
628 | break; | |
629 | default: | |
71ddabb9 | 630 | if (XFS_IS_REALTIME_INODE(ip)) { |
c43f4087 CH |
631 | /* |
632 | * If the file blocks are being allocated from a | |
633 | * realtime volume, then return the inode's realtime | |
634 | * extent size or the realtime volume's extent size. | |
635 | */ | |
636 | stat->blksize = | |
637 | xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog; | |
638 | } else | |
639 | stat->blksize = xfs_preferred_iosize(mp); | |
640 | stat->rdev = 0; | |
641 | break; | |
69e23b9a | 642 | } |
c43f4087 CH |
643 | |
644 | return 0; | |
1da177e4 LT |
645 | } |
646 | ||
647 | STATIC int | |
416c6d5b | 648 | xfs_vn_setattr( |
1da177e4 LT |
649 | struct dentry *dentry, |
650 | struct iattr *attr) | |
651 | { | |
652 | struct inode *inode = dentry->d_inode; | |
653 | unsigned int ia_valid = attr->ia_valid; | |
8285fb58 | 654 | bhv_vattr_t vattr = { 0 }; |
1da177e4 LT |
655 | int flags = 0; |
656 | int error; | |
657 | ||
1da177e4 LT |
658 | if (ia_valid & ATTR_UID) { |
659 | vattr.va_mask |= XFS_AT_UID; | |
660 | vattr.va_uid = attr->ia_uid; | |
661 | } | |
662 | if (ia_valid & ATTR_GID) { | |
663 | vattr.va_mask |= XFS_AT_GID; | |
664 | vattr.va_gid = attr->ia_gid; | |
665 | } | |
666 | if (ia_valid & ATTR_SIZE) { | |
667 | vattr.va_mask |= XFS_AT_SIZE; | |
668 | vattr.va_size = attr->ia_size; | |
669 | } | |
670 | if (ia_valid & ATTR_ATIME) { | |
671 | vattr.va_mask |= XFS_AT_ATIME; | |
672 | vattr.va_atime = attr->ia_atime; | |
8c0b5113 | 673 | inode->i_atime = attr->ia_atime; |
1da177e4 LT |
674 | } |
675 | if (ia_valid & ATTR_MTIME) { | |
676 | vattr.va_mask |= XFS_AT_MTIME; | |
677 | vattr.va_mtime = attr->ia_mtime; | |
678 | } | |
679 | if (ia_valid & ATTR_CTIME) { | |
680 | vattr.va_mask |= XFS_AT_CTIME; | |
681 | vattr.va_ctime = attr->ia_ctime; | |
682 | } | |
683 | if (ia_valid & ATTR_MODE) { | |
684 | vattr.va_mask |= XFS_AT_MODE; | |
685 | vattr.va_mode = attr->ia_mode; | |
686 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) | |
687 | inode->i_mode &= ~S_ISGID; | |
688 | } | |
689 | ||
690 | if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) | |
691 | flags |= ATTR_UTIME; | |
692 | #ifdef ATTR_NO_BLOCK | |
693 | if ((ia_valid & ATTR_NO_BLOCK)) | |
694 | flags |= ATTR_NONBLOCK; | |
695 | #endif | |
696 | ||
739bfb2a | 697 | error = xfs_setattr(XFS_I(inode), &vattr, flags, NULL); |
220b5284 | 698 | if (likely(!error)) |
21a62542 | 699 | vn_revalidate(vn_from_inode(inode)); |
220b5284 | 700 | return -error; |
1da177e4 LT |
701 | } |
702 | ||
703 | STATIC void | |
416c6d5b | 704 | xfs_vn_truncate( |
1da177e4 LT |
705 | struct inode *inode) |
706 | { | |
c2536668 | 707 | block_truncate_page(inode->i_mapping, inode->i_size, xfs_get_blocks); |
1da177e4 LT |
708 | } |
709 | ||
710 | STATIC int | |
416c6d5b | 711 | xfs_vn_setxattr( |
1da177e4 LT |
712 | struct dentry *dentry, |
713 | const char *name, | |
714 | const void *data, | |
715 | size_t size, | |
716 | int flags) | |
717 | { | |
67fcaa73 | 718 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
719 | char *attr = (char *)name; |
720 | attrnames_t *namesp; | |
721 | int xflags = 0; | |
722 | int error; | |
723 | ||
724 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
725 | if (!namesp) | |
726 | return -EOPNOTSUPP; | |
727 | attr += namesp->attr_namelen; | |
728 | error = namesp->attr_capable(vp, NULL); | |
729 | if (error) | |
730 | return error; | |
731 | ||
732 | /* Convert Linux syscall to XFS internal ATTR flags */ | |
733 | if (flags & XATTR_CREATE) | |
734 | xflags |= ATTR_CREATE; | |
735 | if (flags & XATTR_REPLACE) | |
736 | xflags |= ATTR_REPLACE; | |
737 | xflags |= namesp->attr_flag; | |
738 | return namesp->attr_set(vp, attr, (void *)data, size, xflags); | |
739 | } | |
740 | ||
741 | STATIC ssize_t | |
416c6d5b | 742 | xfs_vn_getxattr( |
1da177e4 LT |
743 | struct dentry *dentry, |
744 | const char *name, | |
745 | void *data, | |
746 | size_t size) | |
747 | { | |
67fcaa73 | 748 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
749 | char *attr = (char *)name; |
750 | attrnames_t *namesp; | |
751 | int xflags = 0; | |
752 | ssize_t error; | |
753 | ||
754 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
755 | if (!namesp) | |
756 | return -EOPNOTSUPP; | |
757 | attr += namesp->attr_namelen; | |
758 | error = namesp->attr_capable(vp, NULL); | |
759 | if (error) | |
760 | return error; | |
761 | ||
762 | /* Convert Linux syscall to XFS internal ATTR flags */ | |
763 | if (!size) { | |
764 | xflags |= ATTR_KERNOVAL; | |
765 | data = NULL; | |
766 | } | |
767 | xflags |= namesp->attr_flag; | |
768 | return namesp->attr_get(vp, attr, (void *)data, size, xflags); | |
769 | } | |
770 | ||
771 | STATIC ssize_t | |
416c6d5b | 772 | xfs_vn_listxattr( |
1da177e4 LT |
773 | struct dentry *dentry, |
774 | char *data, | |
775 | size_t size) | |
776 | { | |
67fcaa73 | 777 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
778 | int error, xflags = ATTR_KERNAMELS; |
779 | ssize_t result; | |
780 | ||
781 | if (!size) | |
782 | xflags |= ATTR_KERNOVAL; | |
783 | xflags |= capable(CAP_SYS_ADMIN) ? ATTR_KERNFULLS : ATTR_KERNORMALS; | |
784 | ||
785 | error = attr_generic_list(vp, data, size, xflags, &result); | |
786 | if (error < 0) | |
787 | return error; | |
788 | return result; | |
789 | } | |
790 | ||
791 | STATIC int | |
416c6d5b | 792 | xfs_vn_removexattr( |
1da177e4 LT |
793 | struct dentry *dentry, |
794 | const char *name) | |
795 | { | |
67fcaa73 | 796 | bhv_vnode_t *vp = vn_from_inode(dentry->d_inode); |
1da177e4 LT |
797 | char *attr = (char *)name; |
798 | attrnames_t *namesp; | |
799 | int xflags = 0; | |
800 | int error; | |
801 | ||
802 | namesp = attr_lookup_namespace(attr, attr_namespaces, ATTR_NAMECOUNT); | |
803 | if (!namesp) | |
804 | return -EOPNOTSUPP; | |
805 | attr += namesp->attr_namelen; | |
806 | error = namesp->attr_capable(vp, NULL); | |
807 | if (error) | |
808 | return error; | |
809 | xflags |= namesp->attr_flag; | |
810 | return namesp->attr_remove(vp, attr, xflags); | |
811 | } | |
812 | ||
3ed65264 DC |
813 | STATIC long |
814 | xfs_vn_fallocate( | |
815 | struct inode *inode, | |
816 | int mode, | |
817 | loff_t offset, | |
818 | loff_t len) | |
819 | { | |
820 | long error; | |
821 | loff_t new_size = 0; | |
822 | xfs_flock64_t bf; | |
823 | xfs_inode_t *ip = XFS_I(inode); | |
824 | ||
825 | /* preallocation on directories not yet supported */ | |
826 | error = -ENODEV; | |
827 | if (S_ISDIR(inode->i_mode)) | |
828 | goto out_error; | |
829 | ||
830 | bf.l_whence = 0; | |
831 | bf.l_start = offset; | |
832 | bf.l_len = len; | |
833 | ||
834 | xfs_ilock(ip, XFS_IOLOCK_EXCL); | |
835 | error = xfs_change_file_space(ip, XFS_IOC_RESVSP, &bf, | |
836 | 0, NULL, ATTR_NOLOCK); | |
837 | if (!error && !(mode & FALLOC_FL_KEEP_SIZE) && | |
838 | offset + len > i_size_read(inode)) | |
839 | new_size = offset + len; | |
840 | ||
841 | /* Change file size if needed */ | |
842 | if (new_size) { | |
843 | bhv_vattr_t va; | |
844 | ||
845 | va.va_mask = XFS_AT_SIZE; | |
846 | va.va_size = new_size; | |
847 | error = xfs_setattr(ip, &va, ATTR_NOLOCK, NULL); | |
848 | } | |
849 | ||
850 | xfs_iunlock(ip, XFS_IOLOCK_EXCL); | |
851 | out_error: | |
852 | return error; | |
853 | } | |
1da177e4 | 854 | |
c5ef1c42 | 855 | const struct inode_operations xfs_inode_operations = { |
416c6d5b NS |
856 | .permission = xfs_vn_permission, |
857 | .truncate = xfs_vn_truncate, | |
858 | .getattr = xfs_vn_getattr, | |
859 | .setattr = xfs_vn_setattr, | |
860 | .setxattr = xfs_vn_setxattr, | |
861 | .getxattr = xfs_vn_getxattr, | |
862 | .listxattr = xfs_vn_listxattr, | |
863 | .removexattr = xfs_vn_removexattr, | |
3ed65264 | 864 | .fallocate = xfs_vn_fallocate, |
1da177e4 LT |
865 | }; |
866 | ||
c5ef1c42 | 867 | const struct inode_operations xfs_dir_inode_operations = { |
416c6d5b NS |
868 | .create = xfs_vn_create, |
869 | .lookup = xfs_vn_lookup, | |
870 | .link = xfs_vn_link, | |
871 | .unlink = xfs_vn_unlink, | |
872 | .symlink = xfs_vn_symlink, | |
873 | .mkdir = xfs_vn_mkdir, | |
874 | .rmdir = xfs_vn_rmdir, | |
875 | .mknod = xfs_vn_mknod, | |
876 | .rename = xfs_vn_rename, | |
877 | .permission = xfs_vn_permission, | |
878 | .getattr = xfs_vn_getattr, | |
879 | .setattr = xfs_vn_setattr, | |
880 | .setxattr = xfs_vn_setxattr, | |
881 | .getxattr = xfs_vn_getxattr, | |
882 | .listxattr = xfs_vn_listxattr, | |
883 | .removexattr = xfs_vn_removexattr, | |
1da177e4 LT |
884 | }; |
885 | ||
c5ef1c42 | 886 | const struct inode_operations xfs_symlink_inode_operations = { |
1da177e4 | 887 | .readlink = generic_readlink, |
416c6d5b NS |
888 | .follow_link = xfs_vn_follow_link, |
889 | .put_link = xfs_vn_put_link, | |
890 | .permission = xfs_vn_permission, | |
891 | .getattr = xfs_vn_getattr, | |
892 | .setattr = xfs_vn_setattr, | |
893 | .setxattr = xfs_vn_setxattr, | |
894 | .getxattr = xfs_vn_getxattr, | |
895 | .listxattr = xfs_vn_listxattr, | |
896 | .removexattr = xfs_vn_removexattr, | |
1da177e4 | 897 | }; |