]>
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 | ||
10 | #include <linux/sched.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/completion.h> | |
14 | #include <linux/buffer_head.h> | |
15 | #include <linux/posix_acl.h> | |
16 | #include <linux/sort.h> | |
5c676f6d | 17 | #include <linux/gfs2_ondisk.h> |
71b86f56 | 18 | #include <linux/crc32.h> |
7d308590 | 19 | #include <linux/lm_interface.h> |
fcb47e0b | 20 | #include <linux/security.h> |
b3b94faa DT |
21 | |
22 | #include "gfs2.h" | |
5c676f6d | 23 | #include "incore.h" |
b3b94faa DT |
24 | #include "acl.h" |
25 | #include "bmap.h" | |
26 | #include "dir.h" | |
27 | #include "eattr.h" | |
28 | #include "glock.h" | |
29 | #include "glops.h" | |
30 | #include "inode.h" | |
31 | #include "log.h" | |
32 | #include "meta_io.h" | |
33 | #include "ops_address.h" | |
34 | #include "ops_file.h" | |
35 | #include "ops_inode.h" | |
36 | #include "quota.h" | |
37 | #include "rgrp.h" | |
38 | #include "trans.h" | |
5c676f6d | 39 | #include "util.h" |
b3b94faa | 40 | |
feaa7bba SW |
41 | static int iget_test(struct inode *inode, void *opaque) |
42 | { | |
43 | struct gfs2_inode *ip = GFS2_I(inode); | |
629a21e7 | 44 | struct gfs2_inum_host *inum = opaque; |
feaa7bba | 45 | |
1be38679 SW |
46 | if (ip->i_num.no_addr == inum->no_addr && |
47 | inode->i_private != NULL) | |
feaa7bba | 48 | return 1; |
b3b94faa | 49 | |
feaa7bba SW |
50 | return 0; |
51 | } | |
52 | ||
53 | static int iget_set(struct inode *inode, void *opaque) | |
b3b94faa | 54 | { |
feaa7bba | 55 | struct gfs2_inode *ip = GFS2_I(inode); |
629a21e7 | 56 | struct gfs2_inum_host *inum = opaque; |
b3b94faa | 57 | |
feaa7bba | 58 | ip->i_num = *inum; |
e7c698d7 | 59 | inode->i_ino = inum->no_addr; |
feaa7bba SW |
60 | return 0; |
61 | } | |
b3b94faa | 62 | |
629a21e7 | 63 | struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum) |
feaa7bba | 64 | { |
fb0d3bce | 65 | return ilookup5(sb, (unsigned long)inum->no_addr, |
feaa7bba SW |
66 | iget_test, inum); |
67 | } | |
b3b94faa | 68 | |
629a21e7 | 69 | static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum) |
feaa7bba | 70 | { |
fb0d3bce | 71 | return iget5_locked(sb, (unsigned long)inum->no_addr, |
feaa7bba | 72 | iget_test, iget_set, inum); |
b3b94faa DT |
73 | } |
74 | ||
75 | /** | |
feaa7bba SW |
76 | * gfs2_inode_lookup - Lookup an inode |
77 | * @sb: The super block | |
78 | * @inum: The inode number | |
79 | * @type: The type of the inode | |
b3b94faa | 80 | * |
feaa7bba | 81 | * Returns: A VFS inode, or an error |
b3b94faa DT |
82 | */ |
83 | ||
629a21e7 | 84 | struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type) |
b3b94faa | 85 | { |
feaa7bba SW |
86 | struct inode *inode = gfs2_iget(sb, inum); |
87 | struct gfs2_inode *ip = GFS2_I(inode); | |
88 | struct gfs2_glock *io_gl; | |
89 | int error; | |
b3b94faa | 90 | |
26d83ded SW |
91 | if (!inode) |
92 | return ERR_PTR(-ENOBUFS); | |
93 | ||
feaa7bba SW |
94 | if (inode->i_state & I_NEW) { |
95 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
96 | umode_t mode = DT2IF(type); | |
bba9dfd8 | 97 | inode->i_private = ip; |
feaa7bba SW |
98 | inode->i_mode = mode; |
99 | ||
100 | if (S_ISREG(mode)) { | |
101 | inode->i_op = &gfs2_file_iops; | |
102 | inode->i_fop = &gfs2_file_fops; | |
103 | inode->i_mapping->a_ops = &gfs2_file_aops; | |
104 | } else if (S_ISDIR(mode)) { | |
105 | inode->i_op = &gfs2_dir_iops; | |
106 | inode->i_fop = &gfs2_dir_fops; | |
107 | } else if (S_ISLNK(mode)) { | |
108 | inode->i_op = &gfs2_symlink_iops; | |
109 | } else { | |
110 | inode->i_op = &gfs2_dev_iops; | |
b3b94faa | 111 | } |
b3b94faa | 112 | |
feaa7bba SW |
113 | error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl); |
114 | if (unlikely(error)) | |
115 | goto fail; | |
116 | ip->i_gl->gl_object = ip; | |
b3b94faa | 117 | |
feaa7bba SW |
118 | error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl); |
119 | if (unlikely(error)) | |
120 | goto fail_put; | |
b3b94faa | 121 | |
bfded27b | 122 | set_bit(GIF_INVALID, &ip->i_flags); |
feaa7bba SW |
123 | error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh); |
124 | if (unlikely(error)) | |
125 | goto fail_iopen; | |
b3b94faa | 126 | |
feaa7bba SW |
127 | gfs2_glock_put(io_gl); |
128 | unlock_new_inode(inode); | |
129 | } | |
b3b94faa DT |
130 | |
131 | return inode; | |
feaa7bba SW |
132 | fail_iopen: |
133 | gfs2_glock_put(io_gl); | |
134 | fail_put: | |
135 | ip->i_gl->gl_object = NULL; | |
136 | gfs2_glock_put(ip->i_gl); | |
137 | fail: | |
138 | iput(inode); | |
139 | return ERR_PTR(error); | |
b3b94faa DT |
140 | } |
141 | ||
af339c02 | 142 | static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf) |
ea744d01 SW |
143 | { |
144 | struct gfs2_dinode_host *di = &ip->i_di; | |
145 | const struct gfs2_dinode *str = buf; | |
146 | ||
af339c02 SW |
147 | if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) { |
148 | if (gfs2_consist_inode(ip)) | |
149 | gfs2_dinode_print(ip); | |
150 | return -EIO; | |
151 | } | |
152 | if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino)) | |
153 | return -ESTALE; | |
ea744d01 | 154 | |
b60623c2 | 155 | ip->i_inode.i_mode = be32_to_cpu(str->di_mode); |
e7f14f4d | 156 | ip->i_inode.i_rdev = 0; |
b60623c2 | 157 | switch (ip->i_inode.i_mode & S_IFMT) { |
e7f14f4d SW |
158 | case S_IFBLK: |
159 | case S_IFCHR: | |
160 | ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major), | |
161 | be32_to_cpu(str->di_minor)); | |
162 | break; | |
163 | }; | |
164 | ||
2933f925 SW |
165 | ip->i_inode.i_uid = be32_to_cpu(str->di_uid); |
166 | ip->i_inode.i_gid = be32_to_cpu(str->di_gid); | |
4f56110a SW |
167 | /* |
168 | * We will need to review setting the nlink count here in the | |
169 | * light of the forthcoming ro bind mount work. This is a reminder | |
170 | * to do that. | |
171 | */ | |
172 | ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink); | |
ea744d01 | 173 | di->di_size = be64_to_cpu(str->di_size); |
9e2dbdac | 174 | i_size_write(&ip->i_inode, di->di_size); |
ea744d01 | 175 | di->di_blocks = be64_to_cpu(str->di_blocks); |
9e2dbdac | 176 | gfs2_set_inode_blocks(&ip->i_inode); |
1a7b1eed SW |
177 | ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime); |
178 | ip->i_inode.i_atime.tv_nsec = 0; | |
179 | ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime); | |
180 | ip->i_inode.i_mtime.tv_nsec = 0; | |
181 | ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime); | |
182 | ip->i_inode.i_ctime.tv_nsec = 0; | |
ea744d01 SW |
183 | |
184 | di->di_goal_meta = be64_to_cpu(str->di_goal_meta); | |
185 | di->di_goal_data = be64_to_cpu(str->di_goal_data); | |
186 | di->di_generation = be64_to_cpu(str->di_generation); | |
187 | ||
188 | di->di_flags = be32_to_cpu(str->di_flags); | |
6b124d8d | 189 | gfs2_set_inode_flags(&ip->i_inode); |
ea744d01 SW |
190 | di->di_height = be16_to_cpu(str->di_height); |
191 | ||
192 | di->di_depth = be16_to_cpu(str->di_depth); | |
193 | di->di_entries = be32_to_cpu(str->di_entries); | |
194 | ||
195 | di->di_eattr = be64_to_cpu(str->di_eattr); | |
af339c02 | 196 | return 0; |
ea744d01 SW |
197 | } |
198 | ||
b3b94faa DT |
199 | /** |
200 | * gfs2_inode_refresh - Refresh the incore copy of the dinode | |
201 | * @ip: The GFS2 inode | |
202 | * | |
203 | * Returns: errno | |
204 | */ | |
205 | ||
206 | int gfs2_inode_refresh(struct gfs2_inode *ip) | |
207 | { | |
208 | struct buffer_head *dibh; | |
209 | int error; | |
210 | ||
211 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
212 | if (error) | |
213 | return error; | |
214 | ||
feaa7bba | 215 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) { |
b3b94faa DT |
216 | brelse(dibh); |
217 | return -EIO; | |
218 | } | |
219 | ||
af339c02 | 220 | error = gfs2_dinode_in(ip, dibh->b_data); |
b3b94faa | 221 | brelse(dibh); |
bfded27b | 222 | clear_bit(GIF_INVALID, &ip->i_flags); |
b3b94faa | 223 | |
af339c02 | 224 | return error; |
b3b94faa DT |
225 | } |
226 | ||
feaa7bba | 227 | int gfs2_dinode_dealloc(struct gfs2_inode *ip) |
b3b94faa | 228 | { |
feaa7bba | 229 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
b3b94faa DT |
230 | struct gfs2_alloc *al; |
231 | struct gfs2_rgrpd *rgd; | |
232 | int error; | |
233 | ||
234 | if (ip->i_di.di_blocks != 1) { | |
235 | if (gfs2_consist_inode(ip)) | |
4cc14f0b | 236 | gfs2_dinode_print(ip); |
b3b94faa DT |
237 | return -EIO; |
238 | } | |
239 | ||
240 | al = gfs2_alloc_get(ip); | |
241 | ||
242 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
243 | if (error) | |
244 | goto out; | |
245 | ||
246 | error = gfs2_rindex_hold(sdp, &al->al_ri_gh); | |
247 | if (error) | |
248 | goto out_qs; | |
249 | ||
250 | rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr); | |
251 | if (!rgd) { | |
252 | gfs2_consist_inode(ip); | |
253 | error = -EIO; | |
254 | goto out_rindex_relse; | |
255 | } | |
256 | ||
257 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, | |
258 | &al->al_rgd_gh); | |
259 | if (error) | |
260 | goto out_rindex_relse; | |
261 | ||
420b9e5e | 262 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1); |
b3b94faa DT |
263 | if (error) |
264 | goto out_rg_gunlock; | |
265 | ||
266 | gfs2_trans_add_gl(ip->i_gl); | |
267 | ||
268 | gfs2_free_di(rgd, ip); | |
269 | ||
b3b94faa DT |
270 | gfs2_trans_end(sdp); |
271 | clear_bit(GLF_STICKY, &ip->i_gl->gl_flags); | |
272 | ||
feaa7bba | 273 | out_rg_gunlock: |
b3b94faa | 274 | gfs2_glock_dq_uninit(&al->al_rgd_gh); |
feaa7bba | 275 | out_rindex_relse: |
b3b94faa | 276 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
feaa7bba | 277 | out_qs: |
b3b94faa | 278 | gfs2_quota_unhold(ip); |
36327521 | 279 | out: |
feaa7bba | 280 | gfs2_alloc_put(ip); |
b3b94faa DT |
281 | return error; |
282 | } | |
283 | ||
b3b94faa | 284 | /** |
87d21e07 | 285 | * gfs2_change_nlink - Change nlink count on inode |
b3b94faa DT |
286 | * @ip: The GFS2 inode |
287 | * @diff: The change in the nlink count required | |
288 | * | |
289 | * Returns: errno | |
290 | */ | |
87d21e07 | 291 | int gfs2_change_nlink(struct gfs2_inode *ip, int diff) |
b3b94faa DT |
292 | { |
293 | struct buffer_head *dibh; | |
cd915493 | 294 | u32 nlink; |
b3b94faa DT |
295 | int error; |
296 | ||
4f56110a SW |
297 | BUG_ON(diff != 1 && diff != -1); |
298 | nlink = ip->i_inode.i_nlink + diff; | |
b3b94faa DT |
299 | |
300 | /* If we are reducing the nlink count, but the new value ends up being | |
301 | bigger than the old one, we must have underflowed. */ | |
4f56110a | 302 | if (diff < 0 && nlink > ip->i_inode.i_nlink) { |
b3b94faa | 303 | if (gfs2_consist_inode(ip)) |
4cc14f0b | 304 | gfs2_dinode_print(ip); |
b3b94faa DT |
305 | return -EIO; |
306 | } | |
307 | ||
308 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
309 | if (error) | |
310 | return error; | |
311 | ||
4f56110a SW |
312 | if (diff > 0) |
313 | inc_nlink(&ip->i_inode); | |
314 | else | |
315 | drop_nlink(&ip->i_inode); | |
316 | ||
ddfe0627 | 317 | ip->i_inode.i_ctime = CURRENT_TIME_SEC; |
b3b94faa | 318 | |
d4e9c4c3 | 319 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 320 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa | 321 | brelse(dibh); |
feaa7bba | 322 | mark_inode_dirty(&ip->i_inode); |
b3b94faa | 323 | |
87d21e07 | 324 | if (ip->i_inode.i_nlink == 0) |
ddee7608 | 325 | gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */ |
87d21e07 | 326 | |
5509826f WC |
327 | return error; |
328 | } | |
329 | ||
c752666c SW |
330 | struct inode *gfs2_lookup_simple(struct inode *dip, const char *name) |
331 | { | |
332 | struct qstr qstr; | |
6c93fd1e | 333 | struct inode *inode; |
71b86f56 | 334 | gfs2_str2qstr(&qstr, name); |
6c93fd1e RC |
335 | inode = gfs2_lookupi(dip, &qstr, 1, NULL); |
336 | /* gfs2_lookupi has inconsistent callers: vfs | |
337 | * related routines expect NULL for no entry found, | |
338 | * gfs2_lookup_simple callers expect ENOENT | |
339 | * and do not check for NULL. | |
340 | */ | |
341 | if (inode == NULL) | |
342 | return ERR_PTR(-ENOENT); | |
343 | else | |
344 | return inode; | |
c752666c SW |
345 | } |
346 | ||
347 | ||
b3b94faa DT |
348 | /** |
349 | * gfs2_lookupi - Look up a filename in a directory and return its inode | |
350 | * @d_gh: An initialized holder for the directory glock | |
351 | * @name: The name of the inode to look for | |
352 | * @is_root: If 1, ignore the caller's permissions | |
353 | * @i_gh: An uninitialized holder for the new inode glock | |
354 | * | |
d7c103d0 SW |
355 | * This can be called via the VFS filldir function when NFS is doing |
356 | * a readdirplus and the inode which its intending to stat isn't | |
357 | * already in cache. In this case we must not take the directory glock | |
358 | * again, since the readdir call will have already taken that lock. | |
b3b94faa DT |
359 | * |
360 | * Returns: errno | |
361 | */ | |
362 | ||
feaa7bba SW |
363 | struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name, |
364 | int is_root, struct nameidata *nd) | |
b3b94faa | 365 | { |
c9fd4307 | 366 | struct super_block *sb = dir->i_sb; |
feaa7bba | 367 | struct gfs2_inode *dip = GFS2_I(dir); |
b3b94faa | 368 | struct gfs2_holder d_gh; |
629a21e7 | 369 | struct gfs2_inum_host inum; |
b3b94faa | 370 | unsigned int type; |
d7c103d0 | 371 | int error; |
c752666c | 372 | struct inode *inode = NULL; |
d7c103d0 | 373 | int unlock = 0; |
b3b94faa DT |
374 | |
375 | if (!name->len || name->len > GFS2_FNAMESIZE) | |
c752666c | 376 | return ERR_PTR(-ENAMETOOLONG); |
b3b94faa | 377 | |
c752666c SW |
378 | if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) || |
379 | (name->len == 2 && memcmp(name->name, "..", 2) == 0 && | |
380 | dir == sb->s_root->d_inode)) { | |
320dd101 SW |
381 | igrab(dir); |
382 | return dir; | |
b3b94faa DT |
383 | } |
384 | ||
d7c103d0 SW |
385 | if (gfs2_glock_is_locked_by_me(dip->i_gl) == 0) { |
386 | error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); | |
387 | if (error) | |
388 | return ERR_PTR(error); | |
389 | unlock = 1; | |
390 | } | |
b3b94faa DT |
391 | |
392 | if (!is_root) { | |
faf450ef | 393 | error = permission(dir, MAY_EXEC, NULL); |
b3b94faa DT |
394 | if (error) |
395 | goto out; | |
396 | } | |
397 | ||
c752666c | 398 | error = gfs2_dir_search(dir, name, &inum, &type); |
b3b94faa DT |
399 | if (error) |
400 | goto out; | |
401 | ||
feaa7bba | 402 | inode = gfs2_inode_lookup(sb, &inum, type); |
b3b94faa | 403 | |
7359a19c | 404 | out: |
d7c103d0 SW |
405 | if (unlock) |
406 | gfs2_glock_dq_uninit(&d_gh); | |
c752666c SW |
407 | if (error == -ENOENT) |
408 | return NULL; | |
d7c103d0 | 409 | return inode ? inode : ERR_PTR(error); |
b3b94faa DT |
410 | } |
411 | ||
cd915493 | 412 | static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino) |
b3b94faa | 413 | { |
feaa7bba | 414 | struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode); |
b3b94faa | 415 | struct buffer_head *bh; |
e6972647 | 416 | struct gfs2_inum_range_host ir; |
b3b94faa DT |
417 | int error; |
418 | ||
419 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
420 | if (error) | |
421 | return error; | |
f55ab26a | 422 | mutex_lock(&sdp->sd_inum_mutex); |
b3b94faa DT |
423 | |
424 | error = gfs2_meta_inode_buffer(ip, &bh); | |
425 | if (error) { | |
f55ab26a | 426 | mutex_unlock(&sdp->sd_inum_mutex); |
b3b94faa DT |
427 | gfs2_trans_end(sdp); |
428 | return error; | |
429 | } | |
430 | ||
431 | gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); | |
432 | ||
433 | if (ir.ir_length) { | |
434 | *formal_ino = ir.ir_start++; | |
435 | ir.ir_length--; | |
d4e9c4c3 | 436 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
b3b94faa DT |
437 | gfs2_inum_range_out(&ir, |
438 | bh->b_data + sizeof(struct gfs2_dinode)); | |
439 | brelse(bh); | |
f55ab26a | 440 | mutex_unlock(&sdp->sd_inum_mutex); |
b3b94faa DT |
441 | gfs2_trans_end(sdp); |
442 | return 0; | |
443 | } | |
444 | ||
445 | brelse(bh); | |
446 | ||
f55ab26a | 447 | mutex_unlock(&sdp->sd_inum_mutex); |
b3b94faa DT |
448 | gfs2_trans_end(sdp); |
449 | ||
450 | return 1; | |
451 | } | |
452 | ||
cd915493 | 453 | static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino) |
b3b94faa | 454 | { |
feaa7bba SW |
455 | struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode); |
456 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode); | |
b3b94faa DT |
457 | struct gfs2_holder gh; |
458 | struct buffer_head *bh; | |
e6972647 | 459 | struct gfs2_inum_range_host ir; |
b3b94faa DT |
460 | int error; |
461 | ||
462 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); | |
463 | if (error) | |
464 | return error; | |
465 | ||
466 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0); | |
467 | if (error) | |
468 | goto out; | |
f55ab26a | 469 | mutex_lock(&sdp->sd_inum_mutex); |
b3b94faa DT |
470 | |
471 | error = gfs2_meta_inode_buffer(ip, &bh); | |
472 | if (error) | |
473 | goto out_end_trans; | |
907b9bce | 474 | |
b3b94faa DT |
475 | gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
476 | ||
477 | if (!ir.ir_length) { | |
478 | struct buffer_head *m_bh; | |
cd915493 | 479 | u64 x, y; |
b44b84d7 | 480 | __be64 z; |
b3b94faa DT |
481 | |
482 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); | |
483 | if (error) | |
484 | goto out_brelse; | |
485 | ||
b44b84d7 AV |
486 | z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)); |
487 | x = y = be64_to_cpu(z); | |
b3b94faa DT |
488 | ir.ir_start = x; |
489 | ir.ir_length = GFS2_INUM_QUANTUM; | |
490 | x += GFS2_INUM_QUANTUM; | |
491 | if (x < y) | |
492 | gfs2_consist_inode(m_ip); | |
b44b84d7 | 493 | z = cpu_to_be64(x); |
d4e9c4c3 | 494 | gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1); |
b44b84d7 | 495 | *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z; |
b3b94faa DT |
496 | |
497 | brelse(m_bh); | |
498 | } | |
499 | ||
500 | *formal_ino = ir.ir_start++; | |
501 | ir.ir_length--; | |
502 | ||
d4e9c4c3 | 503 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
b3b94faa DT |
504 | gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
505 | ||
420b9e5e | 506 | out_brelse: |
b3b94faa | 507 | brelse(bh); |
420b9e5e | 508 | out_end_trans: |
f55ab26a | 509 | mutex_unlock(&sdp->sd_inum_mutex); |
b3b94faa | 510 | gfs2_trans_end(sdp); |
420b9e5e | 511 | out: |
b3b94faa | 512 | gfs2_glock_dq_uninit(&gh); |
b3b94faa DT |
513 | return error; |
514 | } | |
515 | ||
cd915493 | 516 | static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum) |
b3b94faa DT |
517 | { |
518 | int error; | |
519 | ||
520 | error = pick_formal_ino_1(sdp, inum); | |
521 | if (error <= 0) | |
522 | return error; | |
523 | ||
524 | error = pick_formal_ino_2(sdp, inum); | |
525 | ||
526 | return error; | |
527 | } | |
528 | ||
529 | /** | |
530 | * create_ok - OK to create a new on-disk inode here? | |
531 | * @dip: Directory in which dinode is to be created | |
532 | * @name: Name of new dinode | |
533 | * @mode: | |
534 | * | |
535 | * Returns: errno | |
536 | */ | |
537 | ||
feaa7bba | 538 | static int create_ok(struct gfs2_inode *dip, const struct qstr *name, |
b3b94faa DT |
539 | unsigned int mode) |
540 | { | |
541 | int error; | |
542 | ||
faf450ef | 543 | error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL); |
b3b94faa DT |
544 | if (error) |
545 | return error; | |
546 | ||
547 | /* Don't create entries in an unlinked directory */ | |
4f56110a | 548 | if (!dip->i_inode.i_nlink) |
b3b94faa DT |
549 | return -EPERM; |
550 | ||
feaa7bba | 551 | error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL); |
b3b94faa DT |
552 | switch (error) { |
553 | case -ENOENT: | |
554 | error = 0; | |
555 | break; | |
556 | case 0: | |
557 | return -EEXIST; | |
558 | default: | |
559 | return error; | |
560 | } | |
561 | ||
cd915493 | 562 | if (dip->i_di.di_entries == (u32)-1) |
b3b94faa | 563 | return -EFBIG; |
4f56110a | 564 | if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1) |
b3b94faa DT |
565 | return -EMLINK; |
566 | ||
567 | return 0; | |
568 | } | |
569 | ||
570 | static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode, | |
571 | unsigned int *uid, unsigned int *gid) | |
572 | { | |
feaa7bba | 573 | if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir && |
2933f925 | 574 | (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) { |
b3b94faa DT |
575 | if (S_ISDIR(*mode)) |
576 | *mode |= S_ISUID; | |
2933f925 | 577 | else if (dip->i_inode.i_uid != current->fsuid) |
b3b94faa | 578 | *mode &= ~07111; |
2933f925 | 579 | *uid = dip->i_inode.i_uid; |
b3b94faa DT |
580 | } else |
581 | *uid = current->fsuid; | |
582 | ||
b60623c2 | 583 | if (dip->i_inode.i_mode & S_ISGID) { |
b3b94faa DT |
584 | if (S_ISDIR(*mode)) |
585 | *mode |= S_ISGID; | |
2933f925 | 586 | *gid = dip->i_inode.i_gid; |
b3b94faa DT |
587 | } else |
588 | *gid = current->fsgid; | |
589 | } | |
590 | ||
629a21e7 | 591 | static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum, |
4340fe62 | 592 | u64 *generation) |
b3b94faa | 593 | { |
feaa7bba | 594 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
595 | int error; |
596 | ||
597 | gfs2_alloc_get(dip); | |
598 | ||
599 | dip->i_alloc.al_requested = RES_DINODE; | |
600 | error = gfs2_inplace_reserve(dip); | |
601 | if (error) | |
602 | goto out; | |
603 | ||
feaa7bba | 604 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0); |
b3b94faa DT |
605 | if (error) |
606 | goto out_ipreserv; | |
607 | ||
4340fe62 | 608 | inum->no_addr = gfs2_alloc_di(dip, generation); |
b3b94faa DT |
609 | |
610 | gfs2_trans_end(sdp); | |
611 | ||
4340fe62 | 612 | out_ipreserv: |
b3b94faa | 613 | gfs2_inplace_release(dip); |
4340fe62 | 614 | out: |
b3b94faa | 615 | gfs2_alloc_put(dip); |
b3b94faa DT |
616 | return error; |
617 | } | |
618 | ||
619 | /** | |
620 | * init_dinode - Fill in a new dinode structure | |
621 | * @dip: the directory this inode is being created in | |
622 | * @gl: The glock covering the new inode | |
623 | * @inum: the inode number | |
624 | * @mode: the file permissions | |
625 | * @uid: | |
626 | * @gid: | |
627 | * | |
628 | */ | |
629 | ||
630 | static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, | |
629a21e7 | 631 | const struct gfs2_inum_host *inum, unsigned int mode, |
4340fe62 | 632 | unsigned int uid, unsigned int gid, |
e7f14f4d | 633 | const u64 *generation, dev_t dev) |
b3b94faa | 634 | { |
feaa7bba | 635 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b96ca4fa | 636 | struct gfs2_dinode *di; |
b3b94faa DT |
637 | struct buffer_head *dibh; |
638 | ||
639 | dibh = gfs2_meta_new(gl, inum->no_addr); | |
d4e9c4c3 | 640 | gfs2_trans_add_bh(gl, dibh, 1); |
b3b94faa DT |
641 | gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI); |
642 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); | |
b96ca4fa SW |
643 | di = (struct gfs2_dinode *)dibh->b_data; |
644 | ||
2442a098 SW |
645 | di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino); |
646 | di->di_num.no_addr = cpu_to_be64(inum->no_addr); | |
b96ca4fa SW |
647 | di->di_mode = cpu_to_be32(mode); |
648 | di->di_uid = cpu_to_be32(uid); | |
649 | di->di_gid = cpu_to_be32(gid); | |
294caaa3 SW |
650 | di->di_nlink = 0; |
651 | di->di_size = 0; | |
b96ca4fa SW |
652 | di->di_blocks = cpu_to_be64(1); |
653 | di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds()); | |
e7f14f4d SW |
654 | di->di_major = cpu_to_be32(MAJOR(dev)); |
655 | di->di_minor = cpu_to_be32(MINOR(dev)); | |
b96ca4fa | 656 | di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr); |
4340fe62 | 657 | di->di_generation = cpu_to_be64(*generation); |
294caaa3 | 658 | di->di_flags = 0; |
b3b94faa DT |
659 | |
660 | if (S_ISREG(mode)) { | |
661 | if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) || | |
662 | gfs2_tune_get(sdp, gt_new_files_jdata)) | |
b96ca4fa | 663 | di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA); |
b3b94faa DT |
664 | if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) || |
665 | gfs2_tune_get(sdp, gt_new_files_directio)) | |
b96ca4fa | 666 | di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO); |
b3b94faa | 667 | } else if (S_ISDIR(mode)) { |
568f4c96 SW |
668 | di->di_flags |= cpu_to_be32(dip->i_di.di_flags & |
669 | GFS2_DIF_INHERIT_DIRECTIO); | |
670 | di->di_flags |= cpu_to_be32(dip->i_di.di_flags & | |
671 | GFS2_DIF_INHERIT_JDATA); | |
b3b94faa DT |
672 | } |
673 | ||
b96ca4fa | 674 | di->__pad1 = 0; |
a9583c79 | 675 | di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0); |
294caaa3 | 676 | di->di_height = 0; |
b96ca4fa SW |
677 | di->__pad2 = 0; |
678 | di->__pad3 = 0; | |
294caaa3 SW |
679 | di->di_depth = 0; |
680 | di->di_entries = 0; | |
b96ca4fa | 681 | memset(&di->__pad4, 0, sizeof(di->__pad4)); |
294caaa3 | 682 | di->di_eattr = 0; |
b96ca4fa SW |
683 | memset(&di->di_reserved, 0, sizeof(di->di_reserved)); |
684 | ||
b3b94faa DT |
685 | brelse(dibh); |
686 | } | |
687 | ||
688 | static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, | |
629a21e7 | 689 | unsigned int mode, const struct gfs2_inum_host *inum, |
e7f14f4d | 690 | const u64 *generation, dev_t dev) |
b3b94faa | 691 | { |
feaa7bba | 692 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
693 | unsigned int uid, gid; |
694 | int error; | |
695 | ||
696 | munge_mode_uid_gid(dip, &mode, &uid, &gid); | |
b3b94faa DT |
697 | gfs2_alloc_get(dip); |
698 | ||
699 | error = gfs2_quota_lock(dip, uid, gid); | |
700 | if (error) | |
701 | goto out; | |
702 | ||
703 | error = gfs2_quota_check(dip, uid, gid); | |
704 | if (error) | |
705 | goto out_quota; | |
706 | ||
feaa7bba | 707 | error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0); |
b3b94faa DT |
708 | if (error) |
709 | goto out_quota; | |
710 | ||
e7f14f4d | 711 | init_dinode(dip, gl, inum, mode, uid, gid, generation, dev); |
b3b94faa | 712 | gfs2_quota_change(dip, +1, uid, gid); |
b3b94faa DT |
713 | gfs2_trans_end(sdp); |
714 | ||
feaa7bba | 715 | out_quota: |
b3b94faa | 716 | gfs2_quota_unlock(dip); |
feaa7bba | 717 | out: |
b3b94faa | 718 | gfs2_alloc_put(dip); |
b3b94faa DT |
719 | return error; |
720 | } | |
721 | ||
feaa7bba SW |
722 | static int link_dinode(struct gfs2_inode *dip, const struct qstr *name, |
723 | struct gfs2_inode *ip) | |
b3b94faa | 724 | { |
feaa7bba | 725 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
726 | struct gfs2_alloc *al; |
727 | int alloc_required; | |
728 | struct buffer_head *dibh; | |
729 | int error; | |
730 | ||
731 | al = gfs2_alloc_get(dip); | |
732 | ||
733 | error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
734 | if (error) | |
735 | goto fail; | |
736 | ||
feaa7bba | 737 | error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name); |
c752666c SW |
738 | if (alloc_required < 0) |
739 | goto fail; | |
b3b94faa | 740 | if (alloc_required) { |
2933f925 | 741 | error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid); |
b3b94faa DT |
742 | if (error) |
743 | goto fail_quota_locks; | |
744 | ||
745 | al->al_requested = sdp->sd_max_dirres; | |
746 | ||
747 | error = gfs2_inplace_reserve(dip); | |
748 | if (error) | |
749 | goto fail_quota_locks; | |
750 | ||
320dd101 | 751 | error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + |
b3b94faa | 752 | al->al_rgd->rd_ri.ri_length + |
907b9bce | 753 | 2 * RES_DINODE + |
b3b94faa DT |
754 | RES_STATFS + RES_QUOTA, 0); |
755 | if (error) | |
756 | goto fail_ipreserv; | |
757 | } else { | |
feaa7bba | 758 | error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0); |
b3b94faa DT |
759 | if (error) |
760 | goto fail_quota_locks; | |
761 | } | |
762 | ||
b60623c2 | 763 | error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode)); |
b3b94faa DT |
764 | if (error) |
765 | goto fail_end_trans; | |
766 | ||
767 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
768 | if (error) | |
769 | goto fail_end_trans; | |
4f56110a | 770 | ip->i_inode.i_nlink = 1; |
d4e9c4c3 | 771 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 772 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa | 773 | brelse(dibh); |
b3b94faa DT |
774 | return 0; |
775 | ||
320dd101 | 776 | fail_end_trans: |
b3b94faa DT |
777 | gfs2_trans_end(sdp); |
778 | ||
320dd101 | 779 | fail_ipreserv: |
b3b94faa DT |
780 | if (dip->i_alloc.al_rgd) |
781 | gfs2_inplace_release(dip); | |
782 | ||
320dd101 | 783 | fail_quota_locks: |
b3b94faa DT |
784 | gfs2_quota_unlock(dip); |
785 | ||
320dd101 | 786 | fail: |
b3b94faa | 787 | gfs2_alloc_put(dip); |
b3b94faa DT |
788 | return error; |
789 | } | |
790 | ||
fcb47e0b RH |
791 | static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip) |
792 | { | |
793 | int err; | |
794 | size_t len; | |
795 | void *value; | |
796 | char *name; | |
797 | struct gfs2_ea_request er; | |
798 | ||
799 | err = security_inode_init_security(&ip->i_inode, &dip->i_inode, | |
800 | &name, &value, &len); | |
801 | ||
802 | if (err) { | |
803 | if (err == -EOPNOTSUPP) | |
804 | return 0; | |
805 | return err; | |
806 | } | |
807 | ||
808 | memset(&er, 0, sizeof(struct gfs2_ea_request)); | |
809 | ||
810 | er.er_type = GFS2_EATYPE_SECURITY; | |
811 | er.er_name = name; | |
812 | er.er_data = value; | |
813 | er.er_name_len = strlen(name); | |
814 | er.er_data_len = len; | |
815 | ||
816 | err = gfs2_ea_set_i(ip, &er); | |
817 | ||
818 | kfree(value); | |
819 | kfree(name); | |
820 | ||
821 | return err; | |
822 | } | |
823 | ||
b3b94faa DT |
824 | /** |
825 | * gfs2_createi - Create a new inode | |
826 | * @ghs: An array of two holders | |
827 | * @name: The name of the new file | |
828 | * @mode: the permissions on the new inode | |
829 | * | |
830 | * @ghs[0] is an initialized holder for the directory | |
831 | * @ghs[1] is the holder for the inode lock | |
832 | * | |
7359a19c | 833 | * If the return value is not NULL, the glocks on both the directory and the new |
b3b94faa DT |
834 | * file are held. A transaction has been started and an inplace reservation |
835 | * is held, as well. | |
836 | * | |
7359a19c | 837 | * Returns: An inode |
b3b94faa DT |
838 | */ |
839 | ||
feaa7bba | 840 | struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name, |
e7f14f4d | 841 | unsigned int mode, dev_t dev) |
b3b94faa | 842 | { |
7359a19c | 843 | struct inode *inode; |
5c676f6d | 844 | struct gfs2_inode *dip = ghs->gh_gl->gl_object; |
feaa7bba SW |
845 | struct inode *dir = &dip->i_inode; |
846 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); | |
629a21e7 | 847 | struct gfs2_inum_host inum; |
b3b94faa | 848 | int error; |
4340fe62 | 849 | u64 generation; |
b3b94faa DT |
850 | |
851 | if (!name->len || name->len > GFS2_FNAMESIZE) | |
7359a19c | 852 | return ERR_PTR(-ENAMETOOLONG); |
b3b94faa | 853 | |
b3b94faa DT |
854 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); |
855 | error = gfs2_glock_nq(ghs); | |
856 | if (error) | |
857 | goto fail; | |
858 | ||
859 | error = create_ok(dip, name, mode); | |
860 | if (error) | |
861 | goto fail_gunlock; | |
862 | ||
feaa7bba | 863 | error = pick_formal_ino(sdp, &inum.no_formal_ino); |
b3b94faa DT |
864 | if (error) |
865 | goto fail_gunlock; | |
866 | ||
4340fe62 | 867 | error = alloc_dinode(dip, &inum, &generation); |
b3b94faa DT |
868 | if (error) |
869 | goto fail_gunlock; | |
870 | ||
28626e20 SW |
871 | error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops, |
872 | LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1); | |
873 | if (error) | |
874 | goto fail_gunlock; | |
b3b94faa | 875 | |
e7f14f4d | 876 | error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev); |
b3b94faa DT |
877 | if (error) |
878 | goto fail_gunlock2; | |
879 | ||
feaa7bba SW |
880 | inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode)); |
881 | if (IS_ERR(inode)) | |
b3b94faa DT |
882 | goto fail_gunlock2; |
883 | ||
feaa7bba | 884 | error = gfs2_inode_refresh(GFS2_I(inode)); |
b3b94faa DT |
885 | if (error) |
886 | goto fail_iput; | |
887 | ||
feaa7bba | 888 | error = gfs2_acl_create(dip, GFS2_I(inode)); |
b3b94faa DT |
889 | if (error) |
890 | goto fail_iput; | |
891 | ||
fcb47e0b RH |
892 | error = gfs2_security_init(dip, GFS2_I(inode)); |
893 | if (error) | |
894 | goto fail_iput; | |
895 | ||
feaa7bba | 896 | error = link_dinode(dip, name, GFS2_I(inode)); |
b3b94faa DT |
897 | if (error) |
898 | goto fail_iput; | |
899 | ||
7359a19c SW |
900 | if (!inode) |
901 | return ERR_PTR(-ENOMEM); | |
902 | return inode; | |
b3b94faa | 903 | |
320dd101 | 904 | fail_iput: |
feaa7bba | 905 | iput(inode); |
320dd101 | 906 | fail_gunlock2: |
b3b94faa | 907 | gfs2_glock_dq_uninit(ghs + 1); |
320dd101 | 908 | fail_gunlock: |
b3b94faa | 909 | gfs2_glock_dq(ghs); |
320dd101 | 910 | fail: |
7359a19c | 911 | return ERR_PTR(error); |
b3b94faa DT |
912 | } |
913 | ||
b3b94faa DT |
914 | /** |
915 | * gfs2_rmdiri - Remove a directory | |
916 | * @dip: The parent directory of the directory to be removed | |
917 | * @name: The name of the directory to be removed | |
918 | * @ip: The GFS2 inode of the directory to be removed | |
919 | * | |
920 | * Assumes Glocks on dip and ip are held | |
921 | * | |
922 | * Returns: errno | |
923 | */ | |
924 | ||
feaa7bba SW |
925 | int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name, |
926 | struct gfs2_inode *ip) | |
b3b94faa | 927 | { |
b3b94faa DT |
928 | struct qstr dotname; |
929 | int error; | |
930 | ||
931 | if (ip->i_di.di_entries != 2) { | |
932 | if (gfs2_consist_inode(ip)) | |
4cc14f0b | 933 | gfs2_dinode_print(ip); |
b3b94faa DT |
934 | return -EIO; |
935 | } | |
936 | ||
937 | error = gfs2_dir_del(dip, name); | |
938 | if (error) | |
939 | return error; | |
940 | ||
941 | error = gfs2_change_nlink(dip, -1); | |
942 | if (error) | |
943 | return error; | |
944 | ||
71b86f56 | 945 | gfs2_str2qstr(&dotname, "."); |
b3b94faa DT |
946 | error = gfs2_dir_del(ip, &dotname); |
947 | if (error) | |
948 | return error; | |
949 | ||
feaa7bba | 950 | gfs2_str2qstr(&dotname, ".."); |
b3b94faa DT |
951 | error = gfs2_dir_del(ip, &dotname); |
952 | if (error) | |
953 | return error; | |
954 | ||
4f56110a SW |
955 | /* It looks odd, but it really should be done twice */ |
956 | error = gfs2_change_nlink(ip, -1); | |
957 | if (error) | |
958 | return error; | |
959 | ||
960 | error = gfs2_change_nlink(ip, -1); | |
b3b94faa DT |
961 | if (error) |
962 | return error; | |
963 | ||
b3b94faa DT |
964 | return error; |
965 | } | |
966 | ||
967 | /* | |
968 | * gfs2_unlink_ok - check to see that a inode is still in a directory | |
969 | * @dip: the directory | |
970 | * @name: the name of the file | |
971 | * @ip: the inode | |
972 | * | |
973 | * Assumes that the lock on (at least) @dip is held. | |
974 | * | |
975 | * Returns: 0 if the parent/child relationship is correct, errno if it isn't | |
976 | */ | |
977 | ||
feaa7bba | 978 | int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, |
b3b94faa DT |
979 | struct gfs2_inode *ip) |
980 | { | |
629a21e7 | 981 | struct gfs2_inum_host inum; |
b3b94faa DT |
982 | unsigned int type; |
983 | int error; | |
984 | ||
feaa7bba | 985 | if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) |
b3b94faa DT |
986 | return -EPERM; |
987 | ||
b60623c2 | 988 | if ((dip->i_inode.i_mode & S_ISVTX) && |
2933f925 SW |
989 | dip->i_inode.i_uid != current->fsuid && |
990 | ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER)) | |
b3b94faa DT |
991 | return -EPERM; |
992 | ||
feaa7bba | 993 | if (IS_APPEND(&dip->i_inode)) |
b3b94faa DT |
994 | return -EPERM; |
995 | ||
faf450ef | 996 | error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL); |
b3b94faa DT |
997 | if (error) |
998 | return error; | |
999 | ||
feaa7bba | 1000 | error = gfs2_dir_search(&dip->i_inode, name, &inum, &type); |
b3b94faa DT |
1001 | if (error) |
1002 | return error; | |
1003 | ||
1004 | if (!gfs2_inum_equal(&inum, &ip->i_num)) | |
1005 | return -ENOENT; | |
1006 | ||
b60623c2 | 1007 | if (IF2DT(ip->i_inode.i_mode) != type) { |
b3b94faa DT |
1008 | gfs2_consist_inode(dip); |
1009 | return -EIO; | |
1010 | } | |
1011 | ||
1012 | return 0; | |
1013 | } | |
1014 | ||
1015 | /* | |
1016 | * gfs2_ok_to_move - check if it's ok to move a directory to another directory | |
1017 | * @this: move this | |
1018 | * @to: to here | |
1019 | * | |
1020 | * Follow @to back to the root and make sure we don't encounter @this | |
1021 | * Assumes we already hold the rename lock. | |
1022 | * | |
1023 | * Returns: errno | |
1024 | */ | |
1025 | ||
1026 | int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) | |
1027 | { | |
feaa7bba | 1028 | struct inode *dir = &to->i_inode; |
c9fd4307 | 1029 | struct super_block *sb = dir->i_sb; |
7359a19c | 1030 | struct inode *tmp; |
b3b94faa DT |
1031 | struct qstr dotdot; |
1032 | int error = 0; | |
1033 | ||
71b86f56 | 1034 | gfs2_str2qstr(&dotdot, ".."); |
b3b94faa | 1035 | |
7359a19c | 1036 | igrab(dir); |
b3b94faa DT |
1037 | |
1038 | for (;;) { | |
feaa7bba | 1039 | if (dir == &this->i_inode) { |
b3b94faa DT |
1040 | error = -EINVAL; |
1041 | break; | |
1042 | } | |
c9fd4307 | 1043 | if (dir == sb->s_root->d_inode) { |
b3b94faa DT |
1044 | error = 0; |
1045 | break; | |
1046 | } | |
1047 | ||
c752666c SW |
1048 | tmp = gfs2_lookupi(dir, &dotdot, 1, NULL); |
1049 | if (IS_ERR(tmp)) { | |
1050 | error = PTR_ERR(tmp); | |
b3b94faa | 1051 | break; |
c752666c | 1052 | } |
b3b94faa | 1053 | |
7359a19c SW |
1054 | iput(dir); |
1055 | dir = tmp; | |
b3b94faa DT |
1056 | } |
1057 | ||
7359a19c | 1058 | iput(dir); |
b3b94faa DT |
1059 | |
1060 | return error; | |
1061 | } | |
1062 | ||
1063 | /** | |
1064 | * gfs2_readlinki - return the contents of a symlink | |
1065 | * @ip: the symlink's inode | |
1066 | * @buf: a pointer to the buffer to be filled | |
1067 | * @len: a pointer to the length of @buf | |
1068 | * | |
1069 | * If @buf is too small, a piece of memory is kmalloc()ed and needs | |
1070 | * to be freed by the caller. | |
1071 | * | |
1072 | * Returns: errno | |
1073 | */ | |
1074 | ||
1075 | int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len) | |
1076 | { | |
1077 | struct gfs2_holder i_gh; | |
1078 | struct buffer_head *dibh; | |
1079 | unsigned int x; | |
1080 | int error; | |
1081 | ||
1082 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); | |
1083 | error = gfs2_glock_nq_atime(&i_gh); | |
1084 | if (error) { | |
1085 | gfs2_holder_uninit(&i_gh); | |
1086 | return error; | |
1087 | } | |
1088 | ||
1089 | if (!ip->i_di.di_size) { | |
1090 | gfs2_consist_inode(ip); | |
1091 | error = -EIO; | |
1092 | goto out; | |
1093 | } | |
1094 | ||
1095 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1096 | if (error) | |
1097 | goto out; | |
1098 | ||
1099 | x = ip->i_di.di_size + 1; | |
1100 | if (x > *len) { | |
1101 | *buf = kmalloc(x, GFP_KERNEL); | |
1102 | if (!*buf) { | |
1103 | error = -ENOMEM; | |
1104 | goto out_brelse; | |
1105 | } | |
1106 | } | |
1107 | ||
1108 | memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x); | |
1109 | *len = x; | |
1110 | ||
feaa7bba | 1111 | out_brelse: |
b3b94faa | 1112 | brelse(dibh); |
feaa7bba | 1113 | out: |
b3b94faa | 1114 | gfs2_glock_dq_uninit(&i_gh); |
b3b94faa DT |
1115 | return error; |
1116 | } | |
1117 | ||
1118 | /** | |
1119 | * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and | |
1120 | * conditionally update the inode's atime | |
1121 | * @gh: the holder to acquire | |
1122 | * | |
1123 | * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap | |
1124 | * Update if the difference between the current time and the inode's current | |
1125 | * atime is greater than an interval specified at mount. | |
1126 | * | |
1127 | * Returns: errno | |
1128 | */ | |
1129 | ||
1130 | int gfs2_glock_nq_atime(struct gfs2_holder *gh) | |
1131 | { | |
1132 | struct gfs2_glock *gl = gh->gh_gl; | |
1133 | struct gfs2_sbd *sdp = gl->gl_sbd; | |
5c676f6d | 1134 | struct gfs2_inode *ip = gl->gl_object; |
cd915493 | 1135 | s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum); |
b3b94faa DT |
1136 | unsigned int state; |
1137 | int flags; | |
1138 | int error; | |
1139 | ||
1140 | if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) || | |
1141 | gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) || | |
1142 | gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops)) | |
1143 | return -EINVAL; | |
1144 | ||
1145 | state = gh->gh_state; | |
1146 | flags = gh->gh_flags; | |
1147 | ||
1148 | error = gfs2_glock_nq(gh); | |
1149 | if (error) | |
1150 | return error; | |
1151 | ||
1152 | if (test_bit(SDF_NOATIME, &sdp->sd_flags) || | |
1153 | (sdp->sd_vfs->s_flags & MS_RDONLY)) | |
1154 | return 0; | |
1155 | ||
1156 | curtime = get_seconds(); | |
1a7b1eed | 1157 | if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) { |
b3b94faa | 1158 | gfs2_glock_dq(gh); |
fd88de56 SW |
1159 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY, |
1160 | gh); | |
b3b94faa DT |
1161 | error = gfs2_glock_nq(gh); |
1162 | if (error) | |
1163 | return error; | |
1164 | ||
1165 | /* Verify that atime hasn't been updated while we were | |
1166 | trying to get exclusive lock. */ | |
1167 | ||
1168 | curtime = get_seconds(); | |
1a7b1eed | 1169 | if (curtime - ip->i_inode.i_atime.tv_sec >= quantum) { |
b3b94faa | 1170 | struct buffer_head *dibh; |
48516ced | 1171 | struct gfs2_dinode *di; |
b3b94faa DT |
1172 | |
1173 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
1174 | if (error == -EROFS) | |
1175 | return 0; | |
1176 | if (error) | |
1177 | goto fail; | |
1178 | ||
1179 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1180 | if (error) | |
1181 | goto fail_end_trans; | |
1182 | ||
1a7b1eed | 1183 | ip->i_inode.i_atime.tv_sec = curtime; |
b3b94faa | 1184 | |
d4e9c4c3 | 1185 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
48516ced | 1186 | di = (struct gfs2_dinode *)dibh->b_data; |
1a7b1eed | 1187 | di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec); |
b3b94faa DT |
1188 | brelse(dibh); |
1189 | ||
1190 | gfs2_trans_end(sdp); | |
1191 | } | |
1192 | ||
1193 | /* If someone else has asked for the glock, | |
1194 | unlock and let them have it. Then reacquire | |
1195 | in the original state. */ | |
1196 | if (gfs2_glock_is_blocking(gl)) { | |
1197 | gfs2_glock_dq(gh); | |
1198 | gfs2_holder_reinit(state, flags, gh); | |
1199 | return gfs2_glock_nq(gh); | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | return 0; | |
1204 | ||
feaa7bba | 1205 | fail_end_trans: |
b3b94faa | 1206 | gfs2_trans_end(sdp); |
feaa7bba | 1207 | fail: |
b3b94faa | 1208 | gfs2_glock_dq(gh); |
b3b94faa DT |
1209 | return error; |
1210 | } | |
1211 | ||
b3b94faa DT |
1212 | static int |
1213 | __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) | |
1214 | { | |
1215 | struct buffer_head *dibh; | |
1216 | int error; | |
1217 | ||
1218 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
1219 | if (!error) { | |
feaa7bba SW |
1220 | error = inode_setattr(&ip->i_inode, attr); |
1221 | gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error); | |
d4e9c4c3 | 1222 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
539e5d6b | 1223 | gfs2_dinode_out(ip, dibh->b_data); |
b3b94faa DT |
1224 | brelse(dibh); |
1225 | } | |
1226 | return error; | |
1227 | } | |
1228 | ||
1229 | /** | |
1230 | * gfs2_setattr_simple - | |
1231 | * @ip: | |
1232 | * @attr: | |
1233 | * | |
1234 | * Called with a reference on the vnode. | |
1235 | * | |
1236 | * Returns: errno | |
1237 | */ | |
1238 | ||
1239 | int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) | |
1240 | { | |
1241 | int error; | |
1242 | ||
5c676f6d | 1243 | if (current->journal_info) |
b3b94faa DT |
1244 | return __gfs2_setattr_simple(ip, attr); |
1245 | ||
feaa7bba | 1246 | error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0); |
b3b94faa DT |
1247 | if (error) |
1248 | return error; | |
1249 | ||
1250 | error = __gfs2_setattr_simple(ip, attr); | |
feaa7bba | 1251 | gfs2_trans_end(GFS2_SB(&ip->i_inode)); |
b3b94faa DT |
1252 | return error; |
1253 | } | |
1254 |