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