Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. | |
3a8a9a10 | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
b3b94faa DT |
4 | * |
5 | * This copyrighted material is made available to anyone wishing to use, | |
6 | * modify, copy, or redistribute it subject to the terms and conditions | |
e9fc2aa0 | 7 | * of the GNU General Public License version 2. |
b3b94faa DT |
8 | */ |
9 | ||
b3b94faa DT |
10 | #include <linux/slab.h> |
11 | #include <linux/spinlock.h> | |
12 | #include <linux/completion.h> | |
13 | #include <linux/buffer_head.h> | |
14 | #include <linux/namei.h> | |
b3b94faa DT |
15 | #include <linux/mm.h> |
16 | #include <linux/xattr.h> | |
17 | #include <linux/posix_acl.h> | |
5c676f6d | 18 | #include <linux/gfs2_ondisk.h> |
71b86f56 | 19 | #include <linux/crc32.h> |
e9079cce | 20 | #include <linux/fiemap.h> |
b3b94faa DT |
21 | #include <asm/uaccess.h> |
22 | ||
23 | #include "gfs2.h" | |
5c676f6d | 24 | #include "incore.h" |
b3b94faa DT |
25 | #include "acl.h" |
26 | #include "bmap.h" | |
27 | #include "dir.h" | |
307cf6e6 | 28 | #include "xattr.h" |
b3b94faa DT |
29 | #include "glock.h" |
30 | #include "inode.h" | |
31 | #include "meta_io.h" | |
b3b94faa DT |
32 | #include "quota.h" |
33 | #include "rgrp.h" | |
34 | #include "trans.h" | |
5c676f6d | 35 | #include "util.h" |
b2760583 | 36 | #include "super.h" |
b3b94faa DT |
37 | |
38 | /** | |
39 | * gfs2_create - Create a file | |
40 | * @dir: The directory in which to create the file | |
41 | * @dentry: The dentry of the new file | |
42 | * @mode: The mode of the new file | |
43 | * | |
44 | * Returns: errno | |
45 | */ | |
46 | ||
47 | static int gfs2_create(struct inode *dir, struct dentry *dentry, | |
48 | int mode, struct nameidata *nd) | |
49 | { | |
feaa7bba SW |
50 | struct gfs2_inode *dip = GFS2_I(dir); |
51 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
b3b94faa DT |
52 | struct gfs2_holder ghs[2]; |
53 | struct inode *inode; | |
b3b94faa | 54 | |
b3b94faa DT |
55 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
56 | ||
57 | for (;;) { | |
e7f14f4d | 58 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0); |
7359a19c | 59 | if (!IS_ERR(inode)) { |
b3b94faa | 60 | gfs2_trans_end(sdp); |
6dbd8224 | 61 | if (dip->i_alloc->al_rgd) |
b3b94faa DT |
62 | gfs2_inplace_release(dip); |
63 | gfs2_quota_unlock(dip); | |
64 | gfs2_alloc_put(dip); | |
65 | gfs2_glock_dq_uninit_m(2, ghs); | |
3a8476dd | 66 | mark_inode_dirty(inode); |
b3b94faa | 67 | break; |
7359a19c | 68 | } else if (PTR_ERR(inode) != -EEXIST || |
3516586a | 69 | (nd && nd->flags & LOOKUP_EXCL)) { |
b3b94faa | 70 | gfs2_holder_uninit(ghs); |
7359a19c | 71 | return PTR_ERR(inode); |
b3b94faa DT |
72 | } |
73 | ||
a569c711 | 74 | inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
c752666c SW |
75 | if (inode) { |
76 | if (!IS_ERR(inode)) { | |
c752666c SW |
77 | gfs2_holder_uninit(ghs); |
78 | break; | |
79 | } else { | |
80 | gfs2_holder_uninit(ghs); | |
81 | return PTR_ERR(inode); | |
82 | } | |
b3b94faa DT |
83 | } |
84 | } | |
85 | ||
b3b94faa | 86 | d_instantiate(dentry, inode); |
b3b94faa DT |
87 | |
88 | return 0; | |
89 | } | |
90 | ||
91 | /** | |
92 | * gfs2_lookup - Look up a filename in a directory and return its inode | |
93 | * @dir: The directory inode | |
94 | * @dentry: The dentry of the new inode | |
95 | * @nd: passed from Linux VFS, ignored by us | |
96 | * | |
97 | * Called by the VFS layer. Lock dir and call gfs2_lookupi() | |
98 | * | |
99 | * Returns: errno | |
100 | */ | |
101 | ||
102 | static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry, | |
103 | struct nameidata *nd) | |
104 | { | |
b3b94faa | 105 | struct inode *inode = NULL; |
b3b94faa | 106 | |
a569c711 | 107 | inode = gfs2_lookupi(dir, &dentry->d_name, 0); |
c752666c | 108 | if (inode && IS_ERR(inode)) |
e231c2ee | 109 | return ERR_CAST(inode); |
b3b94faa | 110 | |
9656b2c1 SW |
111 | if (inode) { |
112 | struct gfs2_glock *gl = GFS2_I(inode)->i_gl; | |
113 | struct gfs2_holder gh; | |
114 | int error; | |
115 | error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); | |
116 | if (error) { | |
117 | iput(inode); | |
118 | return ERR_PTR(error); | |
119 | } | |
120 | gfs2_glock_dq_uninit(&gh); | |
b3b94faa | 121 | return d_splice_alias(inode, dentry); |
9656b2c1 | 122 | } |
b3b94faa DT |
123 | d_add(dentry, inode); |
124 | ||
125 | return NULL; | |
126 | } | |
127 | ||
128 | /** | |
129 | * gfs2_link - Link to a file | |
130 | * @old_dentry: The inode to link | |
131 | * @dir: Add link to this directory | |
132 | * @dentry: The name of the link | |
133 | * | |
134 | * Link the inode in "old_dentry" into the directory "dir" with the | |
135 | * name in "dentry". | |
136 | * | |
137 | * Returns: errno | |
138 | */ | |
139 | ||
140 | static int gfs2_link(struct dentry *old_dentry, struct inode *dir, | |
141 | struct dentry *dentry) | |
142 | { | |
feaa7bba SW |
143 | struct gfs2_inode *dip = GFS2_I(dir); |
144 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
b3b94faa | 145 | struct inode *inode = old_dentry->d_inode; |
feaa7bba | 146 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa | 147 | struct gfs2_holder ghs[2]; |
2baee03f | 148 | struct buffer_head *dibh; |
b3b94faa DT |
149 | int alloc_required; |
150 | int error; | |
151 | ||
b60623c2 | 152 | if (S_ISDIR(inode->i_mode)) |
b3b94faa DT |
153 | return -EPERM; |
154 | ||
155 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); | |
156 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); | |
157 | ||
72dbf479 BP |
158 | error = gfs2_glock_nq(ghs); /* parent */ |
159 | if (error) | |
160 | goto out_parent; | |
161 | ||
162 | error = gfs2_glock_nq(ghs + 1); /* child */ | |
b3b94faa | 163 | if (error) |
72dbf479 | 164 | goto out_child; |
b3b94faa | 165 | |
d192a8e5 SW |
166 | error = -ENOENT; |
167 | if (inode->i_nlink == 0) | |
168 | goto out_gunlock; | |
169 | ||
b74c79e9 | 170 | error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0); |
b3b94faa DT |
171 | if (error) |
172 | goto out_gunlock; | |
173 | ||
dbb7cae2 | 174 | error = gfs2_dir_check(dir, &dentry->d_name, NULL); |
b3b94faa DT |
175 | switch (error) { |
176 | case -ENOENT: | |
177 | break; | |
178 | case 0: | |
179 | error = -EEXIST; | |
180 | default: | |
181 | goto out_gunlock; | |
182 | } | |
183 | ||
184 | error = -EINVAL; | |
4f56110a | 185 | if (!dip->i_inode.i_nlink) |
b3b94faa DT |
186 | goto out_gunlock; |
187 | error = -EFBIG; | |
ad6203f2 | 188 | if (dip->i_entries == (u32)-1) |
b3b94faa DT |
189 | goto out_gunlock; |
190 | error = -EPERM; | |
191 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) | |
192 | goto out_gunlock; | |
193 | error = -EINVAL; | |
4f56110a | 194 | if (!ip->i_inode.i_nlink) |
b3b94faa DT |
195 | goto out_gunlock; |
196 | error = -EMLINK; | |
4f56110a | 197 | if (ip->i_inode.i_nlink == (u32)-1) |
b3b94faa DT |
198 | goto out_gunlock; |
199 | ||
c752666c SW |
200 | alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name); |
201 | if (error < 0) | |
b3b94faa | 202 | goto out_gunlock; |
c752666c | 203 | error = 0; |
b3b94faa DT |
204 | |
205 | if (alloc_required) { | |
206 | struct gfs2_alloc *al = gfs2_alloc_get(dip); | |
182fe5ab CG |
207 | if (!al) { |
208 | error = -ENOMEM; | |
209 | goto out_gunlock; | |
210 | } | |
b3b94faa | 211 | |
d82661d9 | 212 | error = gfs2_quota_lock_check(dip); |
b3b94faa DT |
213 | if (error) |
214 | goto out_alloc; | |
215 | ||
b3b94faa DT |
216 | al->al_requested = sdp->sd_max_dirres; |
217 | ||
218 | error = gfs2_inplace_reserve(dip); | |
219 | if (error) | |
220 | goto out_gunlock_q; | |
221 | ||
1b50259b | 222 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
bf97b673 | 223 | gfs2_rg_blocks(al) + |
b3b94faa DT |
224 | 2 * RES_DINODE + RES_STATFS + |
225 | RES_QUOTA, 0); | |
226 | if (error) | |
227 | goto out_ipres; | |
228 | } else { | |
229 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0); | |
230 | if (error) | |
231 | goto out_ipres; | |
232 | } | |
233 | ||
2baee03f | 234 | error = gfs2_meta_inode_buffer(ip, &dibh); |
b3b94faa DT |
235 | if (error) |
236 | goto out_end_trans; | |
237 | ||
2baee03f SW |
238 | error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode)); |
239 | if (error) | |
240 | goto out_brelse; | |
241 | ||
242 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); | |
243 | inc_nlink(&ip->i_inode); | |
244 | ip->i_inode.i_ctime = CURRENT_TIME; | |
245 | gfs2_dinode_out(ip, dibh->b_data); | |
246 | mark_inode_dirty(&ip->i_inode); | |
b3b94faa | 247 | |
2baee03f SW |
248 | out_brelse: |
249 | brelse(dibh); | |
feaa7bba | 250 | out_end_trans: |
b3b94faa | 251 | gfs2_trans_end(sdp); |
feaa7bba | 252 | out_ipres: |
b3b94faa DT |
253 | if (alloc_required) |
254 | gfs2_inplace_release(dip); | |
feaa7bba | 255 | out_gunlock_q: |
b3b94faa DT |
256 | if (alloc_required) |
257 | gfs2_quota_unlock(dip); | |
feaa7bba | 258 | out_alloc: |
b3b94faa DT |
259 | if (alloc_required) |
260 | gfs2_alloc_put(dip); | |
feaa7bba | 261 | out_gunlock: |
72dbf479 BP |
262 | gfs2_glock_dq(ghs + 1); |
263 | out_child: | |
264 | gfs2_glock_dq(ghs); | |
265 | out_parent: | |
b3b94faa DT |
266 | gfs2_holder_uninit(ghs); |
267 | gfs2_holder_uninit(ghs + 1); | |
b3b94faa | 268 | if (!error) { |
7de9c6ee | 269 | ihold(inode); |
b3b94faa DT |
270 | d_instantiate(dentry, inode); |
271 | mark_inode_dirty(inode); | |
272 | } | |
b3b94faa DT |
273 | return error; |
274 | } | |
275 | ||
87ec2174 SW |
276 | /* |
277 | * gfs2_unlink_ok - check to see that a inode is still in a directory | |
278 | * @dip: the directory | |
279 | * @name: the name of the file | |
280 | * @ip: the inode | |
281 | * | |
282 | * Assumes that the lock on (at least) @dip is held. | |
283 | * | |
284 | * Returns: 0 if the parent/child relationship is correct, errno if it isn't | |
285 | */ | |
286 | ||
287 | static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, | |
288 | const struct gfs2_inode *ip) | |
289 | { | |
290 | int error; | |
291 | ||
292 | if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) | |
293 | return -EPERM; | |
294 | ||
295 | if ((dip->i_inode.i_mode & S_ISVTX) && | |
296 | dip->i_inode.i_uid != current_fsuid() && | |
297 | ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER)) | |
298 | return -EPERM; | |
299 | ||
300 | if (IS_APPEND(&dip->i_inode)) | |
301 | return -EPERM; | |
302 | ||
b74c79e9 | 303 | error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0); |
87ec2174 SW |
304 | if (error) |
305 | return error; | |
306 | ||
307 | error = gfs2_dir_check(&dip->i_inode, name, ip); | |
308 | if (error) | |
309 | return error; | |
310 | ||
311 | return 0; | |
312 | } | |
313 | ||
b3b94faa DT |
314 | /** |
315 | * gfs2_unlink - Unlink a file | |
316 | * @dir: The inode of the directory containing the file to unlink | |
317 | * @dentry: The file itself | |
318 | * | |
319 | * Unlink a file. Call gfs2_unlinki() | |
320 | * | |
321 | * Returns: errno | |
322 | */ | |
323 | ||
324 | static int gfs2_unlink(struct inode *dir, struct dentry *dentry) | |
325 | { | |
feaa7bba SW |
326 | struct gfs2_inode *dip = GFS2_I(dir); |
327 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
328 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); | |
ddee7608 RC |
329 | struct gfs2_holder ghs[3]; |
330 | struct gfs2_rgrpd *rgd; | |
331 | struct gfs2_holder ri_gh; | |
b3b94faa DT |
332 | int error; |
333 | ||
ddee7608 RC |
334 | error = gfs2_rindex_hold(sdp, &ri_gh); |
335 | if (error) | |
336 | return error; | |
337 | ||
b3b94faa | 338 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
ddee7608 | 339 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); |
b3b94faa | 340 | |
dbb7cae2 | 341 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
ddee7608 RC |
342 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
343 | ||
344 | ||
8497a46e | 345 | error = gfs2_glock_nq(ghs); /* parent */ |
b3b94faa | 346 | if (error) |
8497a46e SW |
347 | goto out_parent; |
348 | ||
349 | error = gfs2_glock_nq(ghs + 1); /* child */ | |
350 | if (error) | |
351 | goto out_child; | |
352 | ||
d192a8e5 SW |
353 | error = -ENOENT; |
354 | if (ip->i_inode.i_nlink == 0) | |
355 | goto out_rgrp; | |
356 | ||
8497a46e SW |
357 | error = gfs2_glock_nq(ghs + 2); /* rgrp */ |
358 | if (error) | |
359 | goto out_rgrp; | |
b3b94faa DT |
360 | |
361 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); | |
362 | if (error) | |
72dbf479 | 363 | goto out_gunlock; |
b3b94faa | 364 | |
feaa7bba | 365 | error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0); |
b3b94faa | 366 | if (error) |
cd012075 | 367 | goto out_gunlock; |
b3b94faa | 368 | |
feaa7bba SW |
369 | error = gfs2_dir_del(dip, &dentry->d_name); |
370 | if (error) | |
371 | goto out_end_trans; | |
b3b94faa | 372 | |
feaa7bba | 373 | error = gfs2_change_nlink(ip, -1); |
b3b94faa | 374 | |
feaa7bba SW |
375 | out_end_trans: |
376 | gfs2_trans_end(sdp); | |
72dbf479 | 377 | out_gunlock: |
8497a46e SW |
378 | gfs2_glock_dq(ghs + 2); |
379 | out_rgrp: | |
ddee7608 | 380 | gfs2_holder_uninit(ghs + 2); |
8497a46e SW |
381 | gfs2_glock_dq(ghs + 1); |
382 | out_child: | |
383 | gfs2_holder_uninit(ghs + 1); | |
384 | gfs2_glock_dq(ghs); | |
385 | out_parent: | |
386 | gfs2_holder_uninit(ghs); | |
ddee7608 | 387 | gfs2_glock_dq_uninit(&ri_gh); |
b3b94faa DT |
388 | return error; |
389 | } | |
390 | ||
391 | /** | |
392 | * gfs2_symlink - Create a symlink | |
393 | * @dir: The directory to create the symlink in | |
394 | * @dentry: The dentry to put the symlink in | |
395 | * @symname: The thing which the link points to | |
396 | * | |
397 | * Returns: errno | |
398 | */ | |
399 | ||
400 | static int gfs2_symlink(struct inode *dir, struct dentry *dentry, | |
401 | const char *symname) | |
402 | { | |
feaa7bba SW |
403 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
404 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
b3b94faa DT |
405 | struct gfs2_holder ghs[2]; |
406 | struct inode *inode; | |
407 | struct buffer_head *dibh; | |
408 | int size; | |
409 | int error; | |
410 | ||
b3b94faa DT |
411 | /* Must be stuffed with a null terminator for gfs2_follow_link() */ |
412 | size = strlen(symname); | |
413 | if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1) | |
414 | return -ENAMETOOLONG; | |
415 | ||
416 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); | |
417 | ||
e7f14f4d | 418 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0); |
7359a19c | 419 | if (IS_ERR(inode)) { |
b3b94faa | 420 | gfs2_holder_uninit(ghs); |
7359a19c | 421 | return PTR_ERR(inode); |
b3b94faa DT |
422 | } |
423 | ||
5c676f6d | 424 | ip = ghs[1].gh_gl->gl_object; |
b3b94faa | 425 | |
5cf32524 | 426 | i_size_write(inode, size); |
b3b94faa DT |
427 | |
428 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
429 | ||
430 | if (!gfs2_assert_withdraw(sdp, !error)) { | |
539e5d6b | 431 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
432 | memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, |
433 | size); | |
434 | brelse(dibh); | |
435 | } | |
436 | ||
437 | gfs2_trans_end(sdp); | |
6dbd8224 | 438 | if (dip->i_alloc->al_rgd) |
b3b94faa DT |
439 | gfs2_inplace_release(dip); |
440 | gfs2_quota_unlock(dip); | |
441 | gfs2_alloc_put(dip); | |
442 | ||
443 | gfs2_glock_dq_uninit_m(2, ghs); | |
444 | ||
b3b94faa DT |
445 | d_instantiate(dentry, inode); |
446 | mark_inode_dirty(inode); | |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
451 | /** | |
452 | * gfs2_mkdir - Make a directory | |
453 | * @dir: The parent directory of the new one | |
454 | * @dentry: The dentry of the new directory | |
455 | * @mode: The mode of the new directory | |
456 | * | |
457 | * Returns: errno | |
458 | */ | |
459 | ||
460 | static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
461 | { | |
feaa7bba SW |
462 | struct gfs2_inode *dip = GFS2_I(dir), *ip; |
463 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
b3b94faa DT |
464 | struct gfs2_holder ghs[2]; |
465 | struct inode *inode; | |
466 | struct buffer_head *dibh; | |
467 | int error; | |
468 | ||
b3b94faa DT |
469 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); |
470 | ||
e7f14f4d | 471 | inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0); |
7359a19c | 472 | if (IS_ERR(inode)) { |
b3b94faa | 473 | gfs2_holder_uninit(ghs); |
7359a19c | 474 | return PTR_ERR(inode); |
b3b94faa DT |
475 | } |
476 | ||
5c676f6d | 477 | ip = ghs[1].gh_gl->gl_object; |
b3b94faa | 478 | |
4f56110a | 479 | ip->i_inode.i_nlink = 2; |
a2e0f799 | 480 | i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)); |
383f01fb | 481 | ip->i_diskflags |= GFS2_DIF_JDATA; |
ad6203f2 | 482 | ip->i_entries = 2; |
b3b94faa DT |
483 | |
484 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
485 | ||
486 | if (!gfs2_assert_withdraw(sdp, !error)) { | |
487 | struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; | |
c752666c | 488 | struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1); |
b3b94faa | 489 | |
c752666c | 490 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
8d123585 | 491 | gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent); |
b3b94faa | 492 | dent->de_inum = di->di_num; /* already GFS2 endian */ |
7ecdb70a | 493 | dent->de_type = cpu_to_be16(DT_DIR); |
b3b94faa DT |
494 | di->di_entries = cpu_to_be32(1); |
495 | ||
c752666c | 496 | dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1)); |
8d123585 | 497 | gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent); |
b3b94faa | 498 | |
dbb7cae2 | 499 | gfs2_inum_out(dip, dent); |
7ecdb70a | 500 | dent->de_type = cpu_to_be16(DT_DIR); |
b3b94faa | 501 | |
539e5d6b | 502 | gfs2_dinode_out(ip, di); |
b3b94faa DT |
503 | |
504 | brelse(dibh); | |
505 | } | |
506 | ||
507 | error = gfs2_change_nlink(dip, +1); | |
508 | gfs2_assert_withdraw(sdp, !error); /* dip already pinned */ | |
509 | ||
510 | gfs2_trans_end(sdp); | |
6dbd8224 | 511 | if (dip->i_alloc->al_rgd) |
b3b94faa DT |
512 | gfs2_inplace_release(dip); |
513 | gfs2_quota_unlock(dip); | |
514 | gfs2_alloc_put(dip); | |
515 | ||
516 | gfs2_glock_dq_uninit_m(2, ghs); | |
517 | ||
b3b94faa DT |
518 | d_instantiate(dentry, inode); |
519 | mark_inode_dirty(inode); | |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
2286dbfa SW |
524 | /** |
525 | * gfs2_rmdiri - Remove a directory | |
526 | * @dip: The parent directory of the directory to be removed | |
527 | * @name: The name of the directory to be removed | |
528 | * @ip: The GFS2 inode of the directory to be removed | |
529 | * | |
530 | * Assumes Glocks on dip and ip are held | |
531 | * | |
532 | * Returns: errno | |
533 | */ | |
534 | ||
535 | static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name, | |
536 | struct gfs2_inode *ip) | |
537 | { | |
2286dbfa SW |
538 | int error; |
539 | ||
540 | if (ip->i_entries != 2) { | |
541 | if (gfs2_consist_inode(ip)) | |
542 | gfs2_dinode_print(ip); | |
543 | return -EIO; | |
544 | } | |
545 | ||
546 | error = gfs2_dir_del(dip, name); | |
547 | if (error) | |
548 | return error; | |
549 | ||
550 | error = gfs2_change_nlink(dip, -1); | |
551 | if (error) | |
552 | return error; | |
553 | ||
8d123585 | 554 | error = gfs2_dir_del(ip, &gfs2_qdot); |
2286dbfa SW |
555 | if (error) |
556 | return error; | |
557 | ||
8d123585 | 558 | error = gfs2_dir_del(ip, &gfs2_qdotdot); |
2286dbfa SW |
559 | if (error) |
560 | return error; | |
561 | ||
562 | /* It looks odd, but it really should be done twice */ | |
563 | error = gfs2_change_nlink(ip, -1); | |
564 | if (error) | |
565 | return error; | |
566 | ||
567 | error = gfs2_change_nlink(ip, -1); | |
568 | if (error) | |
569 | return error; | |
570 | ||
571 | return error; | |
572 | } | |
573 | ||
b3b94faa DT |
574 | /** |
575 | * gfs2_rmdir - Remove a directory | |
576 | * @dir: The parent directory of the directory to be removed | |
577 | * @dentry: The dentry of the directory to remove | |
578 | * | |
579 | * Remove a directory. Call gfs2_rmdiri() | |
580 | * | |
581 | * Returns: errno | |
582 | */ | |
583 | ||
584 | static int gfs2_rmdir(struct inode *dir, struct dentry *dentry) | |
585 | { | |
feaa7bba SW |
586 | struct gfs2_inode *dip = GFS2_I(dir); |
587 | struct gfs2_sbd *sdp = GFS2_SB(dir); | |
588 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); | |
ddee7608 RC |
589 | struct gfs2_holder ghs[3]; |
590 | struct gfs2_rgrpd *rgd; | |
591 | struct gfs2_holder ri_gh; | |
b3b94faa DT |
592 | int error; |
593 | ||
ddee7608 RC |
594 | error = gfs2_rindex_hold(sdp, &ri_gh); |
595 | if (error) | |
596 | return error; | |
b3b94faa DT |
597 | gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
598 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1); | |
599 | ||
dbb7cae2 | 600 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); |
ddee7608 RC |
601 | gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2); |
602 | ||
72dbf479 BP |
603 | error = gfs2_glock_nq(ghs); /* parent */ |
604 | if (error) | |
605 | goto out_parent; | |
606 | ||
607 | error = gfs2_glock_nq(ghs + 1); /* child */ | |
b3b94faa | 608 | if (error) |
72dbf479 BP |
609 | goto out_child; |
610 | ||
d192a8e5 SW |
611 | error = -ENOENT; |
612 | if (ip->i_inode.i_nlink == 0) | |
613 | goto out_rgrp; | |
614 | ||
72dbf479 BP |
615 | error = gfs2_glock_nq(ghs + 2); /* rgrp */ |
616 | if (error) | |
617 | goto out_rgrp; | |
b3b94faa DT |
618 | |
619 | error = gfs2_unlink_ok(dip, &dentry->d_name, ip); | |
620 | if (error) | |
621 | goto out_gunlock; | |
622 | ||
ad6203f2 | 623 | if (ip->i_entries < 2) { |
b3b94faa | 624 | if (gfs2_consist_inode(ip)) |
4cc14f0b | 625 | gfs2_dinode_print(ip); |
b3b94faa DT |
626 | error = -EIO; |
627 | goto out_gunlock; | |
628 | } | |
ad6203f2 | 629 | if (ip->i_entries > 2) { |
b3b94faa DT |
630 | error = -ENOTEMPTY; |
631 | goto out_gunlock; | |
632 | } | |
633 | ||
feaa7bba | 634 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0); |
b3b94faa DT |
635 | if (error) |
636 | goto out_gunlock; | |
637 | ||
feaa7bba | 638 | error = gfs2_rmdiri(dip, &dentry->d_name, ip); |
b3b94faa DT |
639 | |
640 | gfs2_trans_end(sdp); | |
641 | ||
a91ea69f | 642 | out_gunlock: |
72dbf479 BP |
643 | gfs2_glock_dq(ghs + 2); |
644 | out_rgrp: | |
ddee7608 | 645 | gfs2_holder_uninit(ghs + 2); |
72dbf479 BP |
646 | gfs2_glock_dq(ghs + 1); |
647 | out_child: | |
648 | gfs2_holder_uninit(ghs + 1); | |
649 | gfs2_glock_dq(ghs); | |
650 | out_parent: | |
651 | gfs2_holder_uninit(ghs); | |
ddee7608 | 652 | gfs2_glock_dq_uninit(&ri_gh); |
b3b94faa DT |
653 | return error; |
654 | } | |
655 | ||
656 | /** | |
657 | * gfs2_mknod - Make a special file | |
658 | * @dir: The directory in which the special file will reside | |
659 | * @dentry: The dentry of the special file | |
660 | * @mode: The mode of the special file | |
661 | * @rdev: The device specification of the special file | |
662 | * | |
663 | */ | |
664 | ||
665 | static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode, | |
666 | dev_t dev) | |
667 | { | |
e7f14f4d | 668 | struct gfs2_inode *dip = GFS2_I(dir); |
feaa7bba | 669 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
b3b94faa DT |
670 | struct gfs2_holder ghs[2]; |
671 | struct inode *inode; | |
b3b94faa DT |
672 | |
673 | gfs2_holder_init(dip->i_gl, 0, 0, ghs); | |
674 | ||
e7f14f4d | 675 | inode = gfs2_createi(ghs, &dentry->d_name, mode, dev); |
7359a19c | 676 | if (IS_ERR(inode)) { |
b3b94faa | 677 | gfs2_holder_uninit(ghs); |
7359a19c | 678 | return PTR_ERR(inode); |
b3b94faa DT |
679 | } |
680 | ||
b3b94faa | 681 | gfs2_trans_end(sdp); |
6dbd8224 | 682 | if (dip->i_alloc->al_rgd) |
b3b94faa DT |
683 | gfs2_inplace_release(dip); |
684 | gfs2_quota_unlock(dip); | |
685 | gfs2_alloc_put(dip); | |
686 | ||
687 | gfs2_glock_dq_uninit_m(2, ghs); | |
688 | ||
b3b94faa DT |
689 | d_instantiate(dentry, inode); |
690 | mark_inode_dirty(inode); | |
691 | ||
692 | return 0; | |
693 | } | |
694 | ||
0188d6c5 SW |
695 | /* |
696 | * gfs2_ok_to_move - check if it's ok to move a directory to another directory | |
697 | * @this: move this | |
698 | * @to: to here | |
699 | * | |
700 | * Follow @to back to the root and make sure we don't encounter @this | |
701 | * Assumes we already hold the rename lock. | |
702 | * | |
703 | * Returns: errno | |
704 | */ | |
705 | ||
706 | static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) | |
707 | { | |
708 | struct inode *dir = &to->i_inode; | |
709 | struct super_block *sb = dir->i_sb; | |
710 | struct inode *tmp; | |
0188d6c5 SW |
711 | int error = 0; |
712 | ||
0188d6c5 SW |
713 | igrab(dir); |
714 | ||
715 | for (;;) { | |
716 | if (dir == &this->i_inode) { | |
717 | error = -EINVAL; | |
718 | break; | |
719 | } | |
720 | if (dir == sb->s_root->d_inode) { | |
721 | error = 0; | |
722 | break; | |
723 | } | |
724 | ||
8d123585 | 725 | tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1); |
0188d6c5 SW |
726 | if (IS_ERR(tmp)) { |
727 | error = PTR_ERR(tmp); | |
728 | break; | |
729 | } | |
730 | ||
731 | iput(dir); | |
732 | dir = tmp; | |
733 | } | |
734 | ||
735 | iput(dir); | |
736 | ||
737 | return error; | |
738 | } | |
739 | ||
b3b94faa DT |
740 | /** |
741 | * gfs2_rename - Rename a file | |
742 | * @odir: Parent directory of old file name | |
743 | * @odentry: The old dentry of the file | |
744 | * @ndir: Parent directory of new file name | |
745 | * @ndentry: The new dentry of the file | |
746 | * | |
747 | * Returns: errno | |
748 | */ | |
749 | ||
750 | static int gfs2_rename(struct inode *odir, struct dentry *odentry, | |
751 | struct inode *ndir, struct dentry *ndentry) | |
752 | { | |
feaa7bba SW |
753 | struct gfs2_inode *odip = GFS2_I(odir); |
754 | struct gfs2_inode *ndip = GFS2_I(ndir); | |
755 | struct gfs2_inode *ip = GFS2_I(odentry->d_inode); | |
b3b94faa | 756 | struct gfs2_inode *nip = NULL; |
feaa7bba | 757 | struct gfs2_sbd *sdp = GFS2_SB(odir); |
46290341 | 758 | struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh; |
ddee7608 | 759 | struct gfs2_rgrpd *nrgd; |
b3b94faa DT |
760 | unsigned int num_gh; |
761 | int dir_rename = 0; | |
24b977b5 | 762 | int alloc_required = 0; |
b3b94faa DT |
763 | unsigned int x; |
764 | int error; | |
765 | ||
b3b94faa | 766 | if (ndentry->d_inode) { |
feaa7bba | 767 | nip = GFS2_I(ndentry->d_inode); |
b3b94faa DT |
768 | if (ip == nip) |
769 | return 0; | |
770 | } | |
771 | ||
46290341 BP |
772 | error = gfs2_rindex_hold(sdp, &ri_gh); |
773 | if (error) | |
774 | return error; | |
b3b94faa | 775 | |
0188d6c5 SW |
776 | if (odip != ndip) { |
777 | error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, | |
778 | 0, &r_gh); | |
b3b94faa DT |
779 | if (error) |
780 | goto out; | |
781 | ||
0188d6c5 SW |
782 | if (S_ISDIR(ip->i_inode.i_mode)) { |
783 | dir_rename = 1; | |
784 | /* don't move a dirctory into it's subdir */ | |
785 | error = gfs2_ok_to_move(ip, ndip); | |
786 | if (error) | |
787 | goto out_gunlock_r; | |
788 | } | |
b3b94faa DT |
789 | } |
790 | ||
d9d1ca30 | 791 | num_gh = 1; |
b3b94faa | 792 | gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs); |
d9d1ca30 SW |
793 | if (odip != ndip) { |
794 | gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); | |
795 | num_gh++; | |
796 | } | |
797 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); | |
798 | num_gh++; | |
b3b94faa | 799 | |
d9d1ca30 SW |
800 | if (nip) { |
801 | gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh); | |
802 | num_gh++; | |
ddee7608 RC |
803 | /* grab the resource lock for unlink flag twiddling |
804 | * this is the case of the target file already existing | |
805 | * so we unlink before doing the rename | |
806 | */ | |
dbb7cae2 | 807 | nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr); |
ddee7608 RC |
808 | if (nrgd) |
809 | gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++); | |
d9d1ca30 | 810 | } |
b3b94faa | 811 | |
72dbf479 BP |
812 | for (x = 0; x < num_gh; x++) { |
813 | error = gfs2_glock_nq(ghs + x); | |
814 | if (error) | |
815 | goto out_gunlock; | |
816 | } | |
b3b94faa | 817 | |
d192a8e5 SW |
818 | error = -ENOENT; |
819 | if (ip->i_inode.i_nlink == 0) | |
820 | goto out_gunlock; | |
821 | ||
b3b94faa DT |
822 | /* Check out the old directory */ |
823 | ||
824 | error = gfs2_unlink_ok(odip, &odentry->d_name, ip); | |
825 | if (error) | |
826 | goto out_gunlock; | |
827 | ||
828 | /* Check out the new directory */ | |
829 | ||
830 | if (nip) { | |
831 | error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); | |
832 | if (error) | |
833 | goto out_gunlock; | |
834 | ||
d192a8e5 SW |
835 | if (nip->i_inode.i_nlink == 0) { |
836 | error = -EAGAIN; | |
837 | goto out_gunlock; | |
838 | } | |
839 | ||
b60623c2 | 840 | if (S_ISDIR(nip->i_inode.i_mode)) { |
ad6203f2 | 841 | if (nip->i_entries < 2) { |
b3b94faa | 842 | if (gfs2_consist_inode(nip)) |
4cc14f0b | 843 | gfs2_dinode_print(nip); |
b3b94faa DT |
844 | error = -EIO; |
845 | goto out_gunlock; | |
846 | } | |
ad6203f2 | 847 | if (nip->i_entries > 2) { |
b3b94faa DT |
848 | error = -ENOTEMPTY; |
849 | goto out_gunlock; | |
850 | } | |
851 | } | |
852 | } else { | |
b74c79e9 | 853 | error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0); |
b3b94faa DT |
854 | if (error) |
855 | goto out_gunlock; | |
856 | ||
dbb7cae2 | 857 | error = gfs2_dir_check(ndir, &ndentry->d_name, NULL); |
b3b94faa DT |
858 | switch (error) { |
859 | case -ENOENT: | |
860 | error = 0; | |
861 | break; | |
862 | case 0: | |
863 | error = -EEXIST; | |
864 | default: | |
865 | goto out_gunlock; | |
866 | }; | |
867 | ||
868 | if (odip != ndip) { | |
4f56110a | 869 | if (!ndip->i_inode.i_nlink) { |
d192a8e5 | 870 | error = -ENOENT; |
b3b94faa DT |
871 | goto out_gunlock; |
872 | } | |
ad6203f2 | 873 | if (ndip->i_entries == (u32)-1) { |
b3b94faa DT |
874 | error = -EFBIG; |
875 | goto out_gunlock; | |
876 | } | |
b60623c2 | 877 | if (S_ISDIR(ip->i_inode.i_mode) && |
4f56110a | 878 | ndip->i_inode.i_nlink == (u32)-1) { |
b3b94faa DT |
879 | error = -EMLINK; |
880 | goto out_gunlock; | |
881 | } | |
882 | } | |
883 | } | |
884 | ||
885 | /* Check out the dir to be renamed */ | |
886 | ||
887 | if (dir_rename) { | |
b74c79e9 | 888 | error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0); |
b3b94faa DT |
889 | if (error) |
890 | goto out_gunlock; | |
891 | } | |
892 | ||
24b977b5 SW |
893 | if (nip == NULL) |
894 | alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name); | |
895 | error = alloc_required; | |
c752666c | 896 | if (error < 0) |
b3b94faa | 897 | goto out_gunlock; |
c752666c | 898 | error = 0; |
b3b94faa DT |
899 | |
900 | if (alloc_required) { | |
901 | struct gfs2_alloc *al = gfs2_alloc_get(ndip); | |
182fe5ab CG |
902 | if (!al) { |
903 | error = -ENOMEM; | |
904 | goto out_gunlock; | |
905 | } | |
b3b94faa | 906 | |
d82661d9 | 907 | error = gfs2_quota_lock_check(ndip); |
b3b94faa DT |
908 | if (error) |
909 | goto out_alloc; | |
910 | ||
b3b94faa DT |
911 | al->al_requested = sdp->sd_max_dirres; |
912 | ||
46290341 | 913 | error = gfs2_inplace_reserve_ri(ndip); |
b3b94faa DT |
914 | if (error) |
915 | goto out_gunlock_q; | |
916 | ||
fe1bdedc | 917 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
bf97b673 | 918 | gfs2_rg_blocks(al) + |
b3b94faa | 919 | 4 * RES_DINODE + 4 * RES_LEAF + |
87d21e07 | 920 | RES_STATFS + RES_QUOTA + 4, 0); |
b3b94faa DT |
921 | if (error) |
922 | goto out_ipreserv; | |
923 | } else { | |
924 | error = gfs2_trans_begin(sdp, 4 * RES_DINODE + | |
87d21e07 | 925 | 5 * RES_LEAF + 4, 0); |
b3b94faa DT |
926 | if (error) |
927 | goto out_gunlock; | |
928 | } | |
929 | ||
930 | /* Remove the target file, if it exists */ | |
931 | ||
932 | if (nip) { | |
b60623c2 | 933 | if (S_ISDIR(nip->i_inode.i_mode)) |
feaa7bba SW |
934 | error = gfs2_rmdiri(ndip, &ndentry->d_name, nip); |
935 | else { | |
936 | error = gfs2_dir_del(ndip, &ndentry->d_name); | |
937 | if (error) | |
938 | goto out_end_trans; | |
87d21e07 | 939 | error = gfs2_change_nlink(nip, -1); |
feaa7bba | 940 | } |
b3b94faa DT |
941 | if (error) |
942 | goto out_end_trans; | |
943 | } | |
944 | ||
945 | if (dir_rename) { | |
b3b94faa DT |
946 | error = gfs2_change_nlink(ndip, +1); |
947 | if (error) | |
948 | goto out_end_trans; | |
949 | error = gfs2_change_nlink(odip, -1); | |
950 | if (error) | |
951 | goto out_end_trans; | |
952 | ||
8d123585 | 953 | error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR); |
b3b94faa DT |
954 | if (error) |
955 | goto out_end_trans; | |
956 | } else { | |
957 | struct buffer_head *dibh; | |
958 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
959 | if (error) | |
960 | goto out_end_trans; | |
4bd91ba1 | 961 | ip->i_inode.i_ctime = CURRENT_TIME; |
d4e9c4c3 | 962 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 963 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
964 | brelse(dibh); |
965 | } | |
966 | ||
967 | error = gfs2_dir_del(odip, &odentry->d_name); | |
968 | if (error) | |
969 | goto out_end_trans; | |
970 | ||
dbb7cae2 | 971 | error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode)); |
b3b94faa DT |
972 | if (error) |
973 | goto out_end_trans; | |
974 | ||
feaa7bba | 975 | out_end_trans: |
b3b94faa | 976 | gfs2_trans_end(sdp); |
feaa7bba | 977 | out_ipreserv: |
b3b94faa DT |
978 | if (alloc_required) |
979 | gfs2_inplace_release(ndip); | |
feaa7bba | 980 | out_gunlock_q: |
b3b94faa DT |
981 | if (alloc_required) |
982 | gfs2_quota_unlock(ndip); | |
feaa7bba | 983 | out_alloc: |
b3b94faa DT |
984 | if (alloc_required) |
985 | gfs2_alloc_put(ndip); | |
feaa7bba | 986 | out_gunlock: |
72dbf479 BP |
987 | while (x--) { |
988 | gfs2_glock_dq(ghs + x); | |
b3b94faa | 989 | gfs2_holder_uninit(ghs + x); |
72dbf479 | 990 | } |
feaa7bba | 991 | out_gunlock_r: |
0188d6c5 | 992 | if (r_gh.gh_gl) |
b3b94faa | 993 | gfs2_glock_dq_uninit(&r_gh); |
feaa7bba | 994 | out: |
46290341 | 995 | gfs2_glock_dq_uninit(&ri_gh); |
b3b94faa DT |
996 | return error; |
997 | } | |
998 | ||
536baf02 | 999 | /** |
c177c2ac AV |
1000 | * gfs2_follow_link - Follow a symbolic link |
1001 | * @dentry: The dentry of the link | |
1002 | * @nd: Data that we pass to vfs_follow_link() | |
536baf02 | 1003 | * |
c177c2ac | 1004 | * This can handle symlinks of any size. |
536baf02 | 1005 | * |
c177c2ac | 1006 | * Returns: 0 on success or error code |
536baf02 SW |
1007 | */ |
1008 | ||
c177c2ac | 1009 | static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd) |
536baf02 | 1010 | { |
c177c2ac | 1011 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
536baf02 SW |
1012 | struct gfs2_holder i_gh; |
1013 | struct buffer_head *dibh; | |
a2e0f799 | 1014 | unsigned int x, size; |
c177c2ac | 1015 | char *buf; |
536baf02 SW |
1016 | int error; |
1017 | ||
1018 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); | |
1019 | error = gfs2_glock_nq(&i_gh); | |
1020 | if (error) { | |
1021 | gfs2_holder_uninit(&i_gh); | |
c177c2ac AV |
1022 | nd_set_link(nd, ERR_PTR(error)); |
1023 | return NULL; | |
536baf02 SW |
1024 | } |
1025 | ||
a2e0f799 SW |
1026 | size = (unsigned int)i_size_read(&ip->i_inode); |
1027 | if (size == 0) { | |
536baf02 | 1028 | gfs2_consist_inode(ip); |
c177c2ac | 1029 | buf = ERR_PTR(-EIO); |
536baf02 SW |
1030 | goto out; |
1031 | } | |
1032 | ||
1033 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
c177c2ac AV |
1034 | if (error) { |
1035 | buf = ERR_PTR(error); | |
536baf02 | 1036 | goto out; |
536baf02 SW |
1037 | } |
1038 | ||
a2e0f799 | 1039 | x = size + 1; |
c177c2ac AV |
1040 | buf = kmalloc(x, GFP_NOFS); |
1041 | if (!buf) | |
1042 | buf = ERR_PTR(-ENOMEM); | |
1043 | else | |
1044 | memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x); | |
536baf02 SW |
1045 | brelse(dibh); |
1046 | out: | |
1047 | gfs2_glock_dq_uninit(&i_gh); | |
c177c2ac AV |
1048 | nd_set_link(nd, buf); |
1049 | return NULL; | |
b3b94faa DT |
1050 | } |
1051 | ||
c177c2ac | 1052 | static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p) |
b3b94faa | 1053 | { |
c177c2ac AV |
1054 | char *s = nd_get_link(nd); |
1055 | if (!IS_ERR(s)) | |
1056 | kfree(s); | |
b3b94faa DT |
1057 | } |
1058 | ||
1059 | /** | |
1060 | * gfs2_permission - | |
75d5cfbe SW |
1061 | * @inode: The inode |
1062 | * @mask: The mask to be tested | |
1063 | * @flags: Indicates whether this is an RCU path walk or not | |
b3b94faa | 1064 | * |
300c7d75 SW |
1065 | * This may be called from the VFS directly, or from within GFS2 with the |
1066 | * inode locked, so we look to see if the glock is already locked and only | |
1067 | * lock the glock if its not already been done. | |
1068 | * | |
b3b94faa DT |
1069 | * Returns: errno |
1070 | */ | |
1071 | ||
b74c79e9 | 1072 | int gfs2_permission(struct inode *inode, int mask, unsigned int flags) |
b3b94faa | 1073 | { |
b74c79e9 | 1074 | struct gfs2_inode *ip; |
b3b94faa DT |
1075 | struct gfs2_holder i_gh; |
1076 | int error; | |
300c7d75 | 1077 | int unlock = 0; |
b3b94faa | 1078 | |
b74c79e9 NP |
1079 | |
1080 | ip = GFS2_I(inode); | |
7afd88d9 | 1081 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
75d5cfbe SW |
1082 | if (flags & IPERM_FLAG_RCU) |
1083 | return -ECHILD; | |
300c7d75 SW |
1084 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); |
1085 | if (error) | |
1086 | return error; | |
1087 | unlock = 1; | |
1088 | } | |
b3b94faa | 1089 | |
f58ba889 MS |
1090 | if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode)) |
1091 | error = -EACCES; | |
1092 | else | |
b74c79e9 | 1093 | error = generic_permission(inode, mask, flags, gfs2_check_acl); |
300c7d75 | 1094 | if (unlock) |
b3b94faa | 1095 | gfs2_glock_dq_uninit(&i_gh); |
b3b94faa DT |
1096 | |
1097 | return error; | |
1098 | } | |
1099 | ||
b3b94faa DT |
1100 | static int setattr_chown(struct inode *inode, struct iattr *attr) |
1101 | { | |
feaa7bba SW |
1102 | struct gfs2_inode *ip = GFS2_I(inode); |
1103 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
cd915493 | 1104 | u32 ouid, ogid, nuid, ngid; |
b3b94faa DT |
1105 | int error; |
1106 | ||
2933f925 SW |
1107 | ouid = inode->i_uid; |
1108 | ogid = inode->i_gid; | |
b3b94faa DT |
1109 | nuid = attr->ia_uid; |
1110 | ngid = attr->ia_gid; | |
1111 | ||
1112 | if (!(attr->ia_valid & ATTR_UID) || ouid == nuid) | |
1113 | ouid = nuid = NO_QUOTA_CHANGE; | |
1114 | if (!(attr->ia_valid & ATTR_GID) || ogid == ngid) | |
1115 | ogid = ngid = NO_QUOTA_CHANGE; | |
1116 | ||
182fe5ab CG |
1117 | if (!gfs2_alloc_get(ip)) |
1118 | return -ENOMEM; | |
b3b94faa DT |
1119 | |
1120 | error = gfs2_quota_lock(ip, nuid, ngid); | |
1121 | if (error) | |
1122 | goto out_alloc; | |
1123 | ||
1124 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { | |
1125 | error = gfs2_quota_check(ip, nuid, ngid); | |
1126 | if (error) | |
1127 | goto out_gunlock_q; | |
1128 | } | |
1129 | ||
1130 | error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0); | |
1131 | if (error) | |
1132 | goto out_gunlock_q; | |
1133 | ||
2ae51ed7 | 1134 | error = gfs2_setattr_simple(ip, attr); |
b3b94faa DT |
1135 | if (error) |
1136 | goto out_end_trans; | |
1137 | ||
b3b94faa | 1138 | if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) { |
77658aad SW |
1139 | u64 blocks = gfs2_get_inode_blocks(&ip->i_inode); |
1140 | gfs2_quota_change(ip, -blocks, ouid, ogid); | |
1141 | gfs2_quota_change(ip, blocks, nuid, ngid); | |
b3b94faa DT |
1142 | } |
1143 | ||
a91ea69f | 1144 | out_end_trans: |
b3b94faa | 1145 | gfs2_trans_end(sdp); |
a91ea69f | 1146 | out_gunlock_q: |
b3b94faa | 1147 | gfs2_quota_unlock(ip); |
a91ea69f | 1148 | out_alloc: |
b3b94faa | 1149 | gfs2_alloc_put(ip); |
b3b94faa DT |
1150 | return error; |
1151 | } | |
1152 | ||
1153 | /** | |
1154 | * gfs2_setattr - Change attributes on an inode | |
1155 | * @dentry: The dentry which is changing | |
1156 | * @attr: The structure describing the change | |
1157 | * | |
1158 | * The VFS layer wants to change one or more of an inodes attributes. Write | |
1159 | * that change out to disk. | |
1160 | * | |
1161 | * Returns: errno | |
1162 | */ | |
1163 | ||
1164 | static int gfs2_setattr(struct dentry *dentry, struct iattr *attr) | |
1165 | { | |
1166 | struct inode *inode = dentry->d_inode; | |
feaa7bba | 1167 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa DT |
1168 | struct gfs2_holder i_gh; |
1169 | int error; | |
1170 | ||
b3b94faa DT |
1171 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); |
1172 | if (error) | |
1173 | return error; | |
1174 | ||
1175 | error = -EPERM; | |
1176 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) | |
1177 | goto out; | |
1178 | ||
1179 | error = inode_change_ok(inode, attr); | |
1180 | if (error) | |
1181 | goto out; | |
1182 | ||
1183 | if (attr->ia_valid & ATTR_SIZE) | |
ff8f33c8 | 1184 | error = gfs2_setattr_size(inode, attr->ia_size); |
b3b94faa DT |
1185 | else if (attr->ia_valid & (ATTR_UID | ATTR_GID)) |
1186 | error = setattr_chown(inode, attr); | |
1187 | else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode)) | |
1188 | error = gfs2_acl_chmod(ip, attr); | |
1189 | else | |
1190 | error = gfs2_setattr_simple(ip, attr); | |
1191 | ||
a91ea69f | 1192 | out: |
b3b94faa | 1193 | gfs2_glock_dq_uninit(&i_gh); |
b3b94faa DT |
1194 | if (!error) |
1195 | mark_inode_dirty(inode); | |
b3b94faa DT |
1196 | return error; |
1197 | } | |
1198 | ||
1199 | /** | |
1200 | * gfs2_getattr - Read out an inode's attributes | |
26c1a574 | 1201 | * @mnt: The vfsmount the inode is being accessed from |
b3b94faa DT |
1202 | * @dentry: The dentry to stat |
1203 | * @stat: The inode's stats | |
1204 | * | |
dcf3dd85 SW |
1205 | * This may be called from the VFS directly, or from within GFS2 with the |
1206 | * inode locked, so we look to see if the glock is already locked and only | |
1207 | * lock the glock if its not already been done. Note that its the NFS | |
1208 | * readdirplus operation which causes this to be called (from filldir) | |
1209 | * with the glock already held. | |
1210 | * | |
b3b94faa DT |
1211 | * Returns: errno |
1212 | */ | |
1213 | ||
1214 | static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
1215 | struct kstat *stat) | |
1216 | { | |
1217 | struct inode *inode = dentry->d_inode; | |
feaa7bba | 1218 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa DT |
1219 | struct gfs2_holder gh; |
1220 | int error; | |
dcf3dd85 | 1221 | int unlock = 0; |
b3b94faa | 1222 | |
7afd88d9 | 1223 | if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { |
dcf3dd85 SW |
1224 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
1225 | if (error) | |
1226 | return error; | |
1227 | unlock = 1; | |
b3b94faa DT |
1228 | } |
1229 | ||
dcf3dd85 | 1230 | generic_fillattr(inode, stat); |
d7c103d0 | 1231 | if (unlock) |
dcf3dd85 SW |
1232 | gfs2_glock_dq_uninit(&gh); |
1233 | ||
1234 | return 0; | |
b3b94faa DT |
1235 | } |
1236 | ||
1237 | static int gfs2_setxattr(struct dentry *dentry, const char *name, | |
1238 | const void *data, size_t size, int flags) | |
1239 | { | |
feaa7bba | 1240 | struct inode *inode = dentry->d_inode; |
40b78a32 SW |
1241 | struct gfs2_inode *ip = GFS2_I(inode); |
1242 | struct gfs2_holder gh; | |
1243 | int ret; | |
b3b94faa | 1244 | |
40b78a32 SW |
1245 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
1246 | ret = gfs2_glock_nq(&gh); | |
1247 | if (ret == 0) { | |
1248 | ret = generic_setxattr(dentry, name, data, size, flags); | |
1249 | gfs2_glock_dq(&gh); | |
1250 | } | |
1251 | gfs2_holder_uninit(&gh); | |
1252 | return ret; | |
b3b94faa DT |
1253 | } |
1254 | ||
1255 | static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name, | |
1256 | void *data, size_t size) | |
1257 | { | |
40b78a32 SW |
1258 | struct inode *inode = dentry->d_inode; |
1259 | struct gfs2_inode *ip = GFS2_I(inode); | |
1260 | struct gfs2_holder gh; | |
1261 | int ret; | |
b3b94faa | 1262 | |
40b78a32 SW |
1263 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); |
1264 | ret = gfs2_glock_nq(&gh); | |
1265 | if (ret == 0) { | |
1266 | ret = generic_getxattr(dentry, name, data, size); | |
1267 | gfs2_glock_dq(&gh); | |
1268 | } | |
1269 | gfs2_holder_uninit(&gh); | |
1270 | return ret; | |
b3b94faa DT |
1271 | } |
1272 | ||
1273 | static int gfs2_removexattr(struct dentry *dentry, const char *name) | |
1274 | { | |
40b78a32 SW |
1275 | struct inode *inode = dentry->d_inode; |
1276 | struct gfs2_inode *ip = GFS2_I(inode); | |
1277 | struct gfs2_holder gh; | |
1278 | int ret; | |
b3b94faa | 1279 | |
40b78a32 SW |
1280 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
1281 | ret = gfs2_glock_nq(&gh); | |
1282 | if (ret == 0) { | |
1283 | ret = generic_removexattr(dentry, name); | |
1284 | gfs2_glock_dq(&gh); | |
1285 | } | |
1286 | gfs2_holder_uninit(&gh); | |
1287 | return ret; | |
b3b94faa DT |
1288 | } |
1289 | ||
e9079cce SW |
1290 | static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
1291 | u64 start, u64 len) | |
1292 | { | |
1293 | struct gfs2_inode *ip = GFS2_I(inode); | |
1294 | struct gfs2_holder gh; | |
1295 | int ret; | |
1296 | ||
1297 | ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC); | |
1298 | if (ret) | |
1299 | return ret; | |
1300 | ||
1301 | mutex_lock(&inode->i_mutex); | |
1302 | ||
1303 | ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); | |
1304 | if (ret) | |
1305 | goto out; | |
1306 | ||
1307 | if (gfs2_is_stuffed(ip)) { | |
1308 | u64 phys = ip->i_no_addr << inode->i_blkbits; | |
1309 | u64 size = i_size_read(inode); | |
1310 | u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED| | |
1311 | FIEMAP_EXTENT_DATA_INLINE; | |
1312 | phys += sizeof(struct gfs2_dinode); | |
1313 | phys += start; | |
1314 | if (start + len > size) | |
1315 | len = size - start; | |
1316 | if (start < size) | |
1317 | ret = fiemap_fill_next_extent(fieinfo, start, phys, | |
1318 | len, flags); | |
1319 | if (ret == 1) | |
1320 | ret = 0; | |
1321 | } else { | |
1322 | ret = __generic_block_fiemap(inode, fieinfo, start, len, | |
1323 | gfs2_block_map); | |
1324 | } | |
1325 | ||
1326 | gfs2_glock_dq_uninit(&gh); | |
1327 | out: | |
1328 | mutex_unlock(&inode->i_mutex); | |
1329 | return ret; | |
1330 | } | |
1331 | ||
92e1d5be | 1332 | const struct inode_operations gfs2_file_iops = { |
e6305c43 | 1333 | .permission = gfs2_permission, |
b3b94faa DT |
1334 | .setattr = gfs2_setattr, |
1335 | .getattr = gfs2_getattr, | |
1336 | .setxattr = gfs2_setxattr, | |
1337 | .getxattr = gfs2_getxattr, | |
1338 | .listxattr = gfs2_listxattr, | |
1339 | .removexattr = gfs2_removexattr, | |
e9079cce | 1340 | .fiemap = gfs2_fiemap, |
b3b94faa DT |
1341 | }; |
1342 | ||
92e1d5be | 1343 | const struct inode_operations gfs2_dir_iops = { |
b3b94faa DT |
1344 | .create = gfs2_create, |
1345 | .lookup = gfs2_lookup, | |
1346 | .link = gfs2_link, | |
1347 | .unlink = gfs2_unlink, | |
1348 | .symlink = gfs2_symlink, | |
1349 | .mkdir = gfs2_mkdir, | |
1350 | .rmdir = gfs2_rmdir, | |
1351 | .mknod = gfs2_mknod, | |
1352 | .rename = gfs2_rename, | |
e6305c43 | 1353 | .permission = gfs2_permission, |
b3b94faa DT |
1354 | .setattr = gfs2_setattr, |
1355 | .getattr = gfs2_getattr, | |
1356 | .setxattr = gfs2_setxattr, | |
1357 | .getxattr = gfs2_getxattr, | |
1358 | .listxattr = gfs2_listxattr, | |
1359 | .removexattr = gfs2_removexattr, | |
e9079cce | 1360 | .fiemap = gfs2_fiemap, |
b3b94faa DT |
1361 | }; |
1362 | ||
92e1d5be | 1363 | const struct inode_operations gfs2_symlink_iops = { |
c177c2ac | 1364 | .readlink = generic_readlink, |
b3b94faa | 1365 | .follow_link = gfs2_follow_link, |
c177c2ac | 1366 | .put_link = gfs2_put_link, |
e6305c43 | 1367 | .permission = gfs2_permission, |
b3b94faa DT |
1368 | .setattr = gfs2_setattr, |
1369 | .getattr = gfs2_getattr, | |
1370 | .setxattr = gfs2_setxattr, | |
1371 | .getxattr = gfs2_getxattr, | |
1372 | .listxattr = gfs2_listxattr, | |
1373 | .removexattr = gfs2_removexattr, | |
e9079cce | 1374 | .fiemap = gfs2_fiemap, |
b3b94faa DT |
1375 | }; |
1376 |