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