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