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