]>
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 | /* | |
61e085a8 SW |
11 | * Implements Extendible Hashing as described in: |
12 | * "Extendible Hashing" by Fagin, et al in | |
13 | * __ACM Trans. on Database Systems__, Sept 1979. | |
14 | * | |
15 | * | |
16 | * Here's the layout of dirents which is essentially the same as that of ext2 | |
17 | * within a single block. The field de_name_len is the number of bytes | |
18 | * actually required for the name (no null terminator). The field de_rec_len | |
19 | * is the number of bytes allocated to the dirent. The offset of the next | |
20 | * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is | |
21 | * deleted, the preceding dirent inherits its allocated space, ie | |
22 | * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained | |
23 | * by adding de_rec_len to the current dirent, this essentially causes the | |
24 | * deleted dirent to get jumped over when iterating through all the dirents. | |
25 | * | |
26 | * When deleting the first dirent in a block, there is no previous dirent so | |
27 | * the field de_ino is set to zero to designate it as deleted. When allocating | |
28 | * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the | |
29 | * first dirent has (de_ino == 0) and de_rec_len is large enough, this first | |
30 | * dirent is allocated. Otherwise it must go through all the 'used' dirents | |
31 | * searching for one in which the amount of total space minus the amount of | |
32 | * used space will provide enough space for the new dirent. | |
33 | * | |
34 | * There are two types of blocks in which dirents reside. In a stuffed dinode, | |
35 | * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of | |
36 | * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the | |
37 | * beginning of the leaf block. The dirents reside in leaves when | |
38 | * | |
383f01fb | 39 | * dip->i_diskflags & GFS2_DIF_EXHASH is true |
61e085a8 SW |
40 | * |
41 | * Otherwise, the dirents are "linear", within a single stuffed dinode block. | |
42 | * | |
43 | * When the dirents are in leaves, the actual contents of the directory file are | |
44 | * used as an array of 64-bit block pointers pointing to the leaf blocks. The | |
45 | * dirents are NOT in the directory file itself. There can be more than one | |
46 | * block pointer in the array that points to the same leaf. In fact, when a | |
47 | * directory is first converted from linear to exhash, all of the pointers | |
48 | * point to the same leaf. | |
49 | * | |
50 | * When a leaf is completely full, the size of the hash table can be | |
51 | * doubled unless it is already at the maximum size which is hard coded into | |
52 | * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list, | |
53 | * but never before the maximum hash table size has been reached. | |
54 | */ | |
b3b94faa | 55 | |
d77d1b58 JP |
56 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
57 | ||
b3b94faa DT |
58 | #include <linux/slab.h> |
59 | #include <linux/spinlock.h> | |
b3b94faa DT |
60 | #include <linux/buffer_head.h> |
61 | #include <linux/sort.h> | |
5c676f6d | 62 | #include <linux/gfs2_ondisk.h> |
71b86f56 | 63 | #include <linux/crc32.h> |
fe1bdedc | 64 | #include <linux/vmalloc.h> |
b3b94faa DT |
65 | |
66 | #include "gfs2.h" | |
5c676f6d | 67 | #include "incore.h" |
b3b94faa DT |
68 | #include "dir.h" |
69 | #include "glock.h" | |
70 | #include "inode.h" | |
b3b94faa DT |
71 | #include "meta_io.h" |
72 | #include "quota.h" | |
73 | #include "rgrp.h" | |
74 | #include "trans.h" | |
e13940ba | 75 | #include "bmap.h" |
5c676f6d | 76 | #include "util.h" |
b3b94faa DT |
77 | |
78 | #define IS_LEAF 1 /* Hashed (leaf) directory */ | |
79 | #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */ | |
80 | ||
dfe4d34b BP |
81 | #define MAX_RA_BLOCKS 32 /* max read-ahead blocks */ |
82 | ||
cd915493 SW |
83 | #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1) |
84 | #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1)) | |
b3b94faa | 85 | |
8d123585 SW |
86 | struct qstr gfs2_qdot __read_mostly; |
87 | struct qstr gfs2_qdotdot __read_mostly; | |
88 | ||
2bdbc5d7 SW |
89 | typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent, |
90 | const struct qstr *name, void *opaque); | |
b3b94faa | 91 | |
cd915493 | 92 | int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block, |
61e085a8 | 93 | struct buffer_head **bhp) |
e13940ba SW |
94 | { |
95 | struct buffer_head *bh; | |
e13940ba | 96 | |
61e085a8 | 97 | bh = gfs2_meta_new(ip->i_gl, block); |
350a9b0a | 98 | gfs2_trans_add_meta(ip->i_gl, bh); |
61e085a8 SW |
99 | gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD); |
100 | gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); | |
e13940ba SW |
101 | *bhp = bh; |
102 | return 0; | |
103 | } | |
104 | ||
cd915493 | 105 | static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block, |
61e085a8 SW |
106 | struct buffer_head **bhp) |
107 | { | |
108 | struct buffer_head *bh; | |
109 | int error; | |
e13940ba | 110 | |
c8d57703 | 111 | error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, 0, &bh); |
61e085a8 SW |
112 | if (error) |
113 | return error; | |
feaa7bba | 114 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) { |
61e085a8 SW |
115 | brelse(bh); |
116 | return -EIO; | |
117 | } | |
118 | *bhp = bh; | |
119 | return 0; | |
120 | } | |
e13940ba SW |
121 | |
122 | static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf, | |
123 | unsigned int offset, unsigned int size) | |
e13940ba SW |
124 | { |
125 | struct buffer_head *dibh; | |
126 | int error; | |
127 | ||
128 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
129 | if (error) | |
130 | return error; | |
131 | ||
350a9b0a | 132 | gfs2_trans_add_meta(ip->i_gl, dibh); |
c752666c | 133 | memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size); |
a2e0f799 SW |
134 | if (ip->i_inode.i_size < offset + size) |
135 | i_size_write(&ip->i_inode, offset + size); | |
4bd91ba1 | 136 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
539e5d6b | 137 | gfs2_dinode_out(ip, dibh->b_data); |
e13940ba SW |
138 | |
139 | brelse(dibh); | |
140 | ||
141 | return size; | |
142 | } | |
143 | ||
144 | ||
145 | ||
146 | /** | |
147 | * gfs2_dir_write_data - Write directory information to the inode | |
148 | * @ip: The GFS2 inode | |
149 | * @buf: The buffer containing information to be written | |
150 | * @offset: The file offset to start writing at | |
151 | * @size: The amount of data to write | |
152 | * | |
153 | * Returns: The number of bytes correctly written or error code | |
154 | */ | |
155 | static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf, | |
cd915493 | 156 | u64 offset, unsigned int size) |
e13940ba | 157 | { |
feaa7bba | 158 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
e13940ba | 159 | struct buffer_head *dibh; |
cd915493 SW |
160 | u64 lblock, dblock; |
161 | u32 extlen = 0; | |
e13940ba SW |
162 | unsigned int o; |
163 | int copied = 0; | |
164 | int error = 0; | |
9b8c81d1 | 165 | int new = 0; |
e13940ba SW |
166 | |
167 | if (!size) | |
168 | return 0; | |
169 | ||
170 | if (gfs2_is_stuffed(ip) && | |
171 | offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) | |
568f4c96 SW |
172 | return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset, |
173 | size); | |
e13940ba SW |
174 | |
175 | if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) | |
176 | return -EINVAL; | |
177 | ||
178 | if (gfs2_is_stuffed(ip)) { | |
f25ef0c1 | 179 | error = gfs2_unstuff_dinode(ip, NULL); |
e13940ba | 180 | if (error) |
c752666c | 181 | return error; |
e13940ba SW |
182 | } |
183 | ||
184 | lblock = offset; | |
185 | o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); | |
186 | ||
187 | while (copied < size) { | |
188 | unsigned int amount; | |
189 | struct buffer_head *bh; | |
e13940ba SW |
190 | |
191 | amount = size - copied; | |
192 | if (amount > sdp->sd_sb.sb_bsize - o) | |
193 | amount = sdp->sd_sb.sb_bsize - o; | |
194 | ||
195 | if (!extlen) { | |
196 | new = 1; | |
feaa7bba | 197 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, |
fd88de56 | 198 | &dblock, &extlen); |
e13940ba SW |
199 | if (error) |
200 | goto fail; | |
201 | error = -EIO; | |
202 | if (gfs2_assert_withdraw(sdp, dblock)) | |
203 | goto fail; | |
204 | } | |
205 | ||
61e085a8 SW |
206 | if (amount == sdp->sd_jbsize || new) |
207 | error = gfs2_dir_get_new_buffer(ip, dblock, &bh); | |
208 | else | |
209 | error = gfs2_dir_get_existing_buffer(ip, dblock, &bh); | |
210 | ||
e13940ba SW |
211 | if (error) |
212 | goto fail; | |
213 | ||
350a9b0a | 214 | gfs2_trans_add_meta(ip->i_gl, bh); |
e13940ba SW |
215 | memcpy(bh->b_data + o, buf, amount); |
216 | brelse(bh); | |
e13940ba | 217 | |
899bb264 | 218 | buf += amount; |
e13940ba SW |
219 | copied += amount; |
220 | lblock++; | |
221 | dblock++; | |
222 | extlen--; | |
223 | ||
224 | o = sizeof(struct gfs2_meta_header); | |
225 | } | |
226 | ||
227 | out: | |
228 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
229 | if (error) | |
230 | return error; | |
231 | ||
a2e0f799 SW |
232 | if (ip->i_inode.i_size < offset + copied) |
233 | i_size_write(&ip->i_inode, offset + copied); | |
4bd91ba1 | 234 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
e13940ba | 235 | |
350a9b0a | 236 | gfs2_trans_add_meta(ip->i_gl, dibh); |
539e5d6b | 237 | gfs2_dinode_out(ip, dibh->b_data); |
e13940ba SW |
238 | brelse(dibh); |
239 | ||
240 | return copied; | |
241 | fail: | |
242 | if (copied) | |
243 | goto out; | |
244 | return error; | |
245 | } | |
246 | ||
4c28d338 SW |
247 | static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf, |
248 | unsigned int size) | |
e13940ba SW |
249 | { |
250 | struct buffer_head *dibh; | |
251 | int error; | |
252 | ||
253 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
254 | if (!error) { | |
4c28d338 | 255 | memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size); |
e13940ba SW |
256 | brelse(dibh); |
257 | } | |
258 | ||
259 | return (error) ? error : size; | |
260 | } | |
261 | ||
262 | ||
263 | /** | |
264 | * gfs2_dir_read_data - Read a data from a directory inode | |
265 | * @ip: The GFS2 Inode | |
266 | * @buf: The buffer to place result into | |
e13940ba SW |
267 | * @size: Amount of data to transfer |
268 | * | |
269 | * Returns: The amount of data actually copied or the error | |
270 | */ | |
4c28d338 SW |
271 | static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf, |
272 | unsigned int size) | |
e13940ba | 273 | { |
feaa7bba | 274 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
cd915493 SW |
275 | u64 lblock, dblock; |
276 | u32 extlen = 0; | |
e13940ba SW |
277 | unsigned int o; |
278 | int copied = 0; | |
279 | int error = 0; | |
e13940ba SW |
280 | |
281 | if (gfs2_is_stuffed(ip)) | |
4c28d338 | 282 | return gfs2_dir_read_stuffed(ip, buf, size); |
e13940ba SW |
283 | |
284 | if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) | |
285 | return -EINVAL; | |
286 | ||
4c28d338 | 287 | lblock = 0; |
e13940ba SW |
288 | o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); |
289 | ||
290 | while (copied < size) { | |
291 | unsigned int amount; | |
292 | struct buffer_head *bh; | |
293 | int new; | |
294 | ||
295 | amount = size - copied; | |
296 | if (amount > sdp->sd_sb.sb_bsize - o) | |
297 | amount = sdp->sd_sb.sb_bsize - o; | |
298 | ||
299 | if (!extlen) { | |
300 | new = 0; | |
feaa7bba | 301 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, |
fd88de56 | 302 | &dblock, &extlen); |
7276b3b0 | 303 | if (error || !dblock) |
e13940ba | 304 | goto fail; |
7276b3b0 | 305 | BUG_ON(extlen < 1); |
7276b3b0 | 306 | bh = gfs2_meta_ra(ip->i_gl, dblock, extlen); |
b7d8ac3e | 307 | } else { |
c8d57703 | 308 | error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, 0, &bh); |
e13940ba SW |
309 | if (error) |
310 | goto fail; | |
7276b3b0 SW |
311 | } |
312 | error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD); | |
313 | if (error) { | |
314 | brelse(bh); | |
315 | goto fail; | |
316 | } | |
317 | dblock++; | |
318 | extlen--; | |
e13940ba SW |
319 | memcpy(buf, bh->b_data + o, amount); |
320 | brelse(bh); | |
4c28d338 | 321 | buf += (amount/sizeof(__be64)); |
e13940ba SW |
322 | copied += amount; |
323 | lblock++; | |
e13940ba SW |
324 | o = sizeof(struct gfs2_meta_header); |
325 | } | |
326 | ||
327 | return copied; | |
328 | fail: | |
329 | return (copied) ? copied : error; | |
330 | } | |
331 | ||
17d539f0 SW |
332 | /** |
333 | * gfs2_dir_get_hash_table - Get pointer to the dir hash table | |
334 | * @ip: The inode in question | |
335 | * | |
336 | * Returns: The hash table or an error | |
337 | */ | |
338 | ||
339 | static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip) | |
340 | { | |
341 | struct inode *inode = &ip->i_inode; | |
342 | int ret; | |
343 | u32 hsize; | |
344 | __be64 *hc; | |
345 | ||
346 | BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH)); | |
347 | ||
348 | hc = ip->i_hash_cache; | |
349 | if (hc) | |
350 | return hc; | |
351 | ||
352 | hsize = 1 << ip->i_depth; | |
353 | hsize *= sizeof(__be64); | |
354 | if (hsize != i_size_read(&ip->i_inode)) { | |
355 | gfs2_consist_inode(ip); | |
356 | return ERR_PTR(-EIO); | |
357 | } | |
358 | ||
e8830d88 BP |
359 | hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN); |
360 | if (hc == NULL) | |
361 | hc = __vmalloc(hsize, GFP_NOFS, PAGE_KERNEL); | |
362 | ||
17d539f0 SW |
363 | if (hc == NULL) |
364 | return ERR_PTR(-ENOMEM); | |
365 | ||
4c28d338 | 366 | ret = gfs2_dir_read_data(ip, hc, hsize); |
17d539f0 | 367 | if (ret < 0) { |
3cdcf63e | 368 | kvfree(hc); |
17d539f0 SW |
369 | return ERR_PTR(ret); |
370 | } | |
371 | ||
372 | spin_lock(&inode->i_lock); | |
9265f1d0 | 373 | if (likely(!ip->i_hash_cache)) { |
17d539f0 | 374 | ip->i_hash_cache = hc; |
9265f1d0 AV |
375 | hc = NULL; |
376 | } | |
17d539f0 | 377 | spin_unlock(&inode->i_lock); |
9265f1d0 | 378 | kvfree(hc); |
17d539f0 SW |
379 | |
380 | return ip->i_hash_cache; | |
381 | } | |
382 | ||
383 | /** | |
384 | * gfs2_dir_hash_inval - Invalidate dir hash | |
385 | * @ip: The directory inode | |
386 | * | |
387 | * Must be called with an exclusive glock, or during glock invalidation. | |
388 | */ | |
389 | void gfs2_dir_hash_inval(struct gfs2_inode *ip) | |
390 | { | |
c36b97e9 BP |
391 | __be64 *hc; |
392 | ||
393 | spin_lock(&ip->i_inode.i_lock); | |
394 | hc = ip->i_hash_cache; | |
17d539f0 | 395 | ip->i_hash_cache = NULL; |
c36b97e9 BP |
396 | spin_unlock(&ip->i_inode.i_lock); |
397 | ||
3cdcf63e | 398 | kvfree(hc); |
17d539f0 SW |
399 | } |
400 | ||
5e7d65cd SW |
401 | static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent) |
402 | { | |
403 | return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0; | |
404 | } | |
405 | ||
c752666c SW |
406 | static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent, |
407 | const struct qstr *name, int ret) | |
408 | { | |
5e7d65cd | 409 | if (!gfs2_dirent_sentinel(dent) && |
c752666c SW |
410 | be32_to_cpu(dent->de_hash) == name->hash && |
411 | be16_to_cpu(dent->de_name_len) == name->len && | |
2bdbc5d7 | 412 | memcmp(dent+1, name->name, name->len) == 0) |
c752666c SW |
413 | return ret; |
414 | return 0; | |
415 | } | |
416 | ||
417 | static int gfs2_dirent_find(const struct gfs2_dirent *dent, | |
71b86f56 SW |
418 | const struct qstr *name, |
419 | void *opaque) | |
c752666c SW |
420 | { |
421 | return __gfs2_dirent_find(dent, name, 1); | |
422 | } | |
423 | ||
424 | static int gfs2_dirent_prev(const struct gfs2_dirent *dent, | |
71b86f56 SW |
425 | const struct qstr *name, |
426 | void *opaque) | |
c752666c SW |
427 | { |
428 | return __gfs2_dirent_find(dent, name, 2); | |
429 | } | |
430 | ||
431 | /* | |
432 | * name->name holds ptr to start of block. | |
433 | * name->len holds size of block. | |
b3b94faa | 434 | */ |
c752666c | 435 | static int gfs2_dirent_last(const struct gfs2_dirent *dent, |
71b86f56 SW |
436 | const struct qstr *name, |
437 | void *opaque) | |
c752666c SW |
438 | { |
439 | const char *start = name->name; | |
440 | const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len); | |
441 | if (name->len == (end - start)) | |
442 | return 1; | |
443 | return 0; | |
444 | } | |
b3b94faa | 445 | |
34017472 BM |
446 | /* Look for the dirent that contains the offset specified in data. Once we |
447 | * find that dirent, there must be space available there for the new dirent */ | |
448 | static int gfs2_dirent_find_offset(const struct gfs2_dirent *dent, | |
449 | const struct qstr *name, | |
450 | void *ptr) | |
451 | { | |
452 | unsigned required = GFS2_DIRENT_SIZE(name->len); | |
453 | unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); | |
454 | unsigned totlen = be16_to_cpu(dent->de_rec_len); | |
455 | ||
456 | if (ptr < (void *)dent || ptr >= (void *)dent + totlen) | |
457 | return 0; | |
458 | if (gfs2_dirent_sentinel(dent)) | |
459 | actual = 0; | |
460 | if (ptr < (void *)dent + actual) | |
461 | return -1; | |
462 | if ((void *)dent + totlen >= ptr + required) | |
463 | return 1; | |
464 | return -1; | |
465 | } | |
466 | ||
c752666c | 467 | static int gfs2_dirent_find_space(const struct gfs2_dirent *dent, |
71b86f56 SW |
468 | const struct qstr *name, |
469 | void *opaque) | |
b3b94faa | 470 | { |
c752666c SW |
471 | unsigned required = GFS2_DIRENT_SIZE(name->len); |
472 | unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); | |
473 | unsigned totlen = be16_to_cpu(dent->de_rec_len); | |
474 | ||
5e7d65cd | 475 | if (gfs2_dirent_sentinel(dent)) |
728a756b | 476 | actual = 0; |
c5392124 | 477 | if (totlen - actual >= required) |
c752666c SW |
478 | return 1; |
479 | return 0; | |
480 | } | |
481 | ||
71b86f56 SW |
482 | struct dirent_gather { |
483 | const struct gfs2_dirent **pdent; | |
484 | unsigned offset; | |
485 | }; | |
486 | ||
487 | static int gfs2_dirent_gather(const struct gfs2_dirent *dent, | |
488 | const struct qstr *name, | |
489 | void *opaque) | |
490 | { | |
491 | struct dirent_gather *g = opaque; | |
5e7d65cd | 492 | if (!gfs2_dirent_sentinel(dent)) { |
71b86f56 SW |
493 | g->pdent[g->offset++] = dent; |
494 | } | |
495 | return 0; | |
496 | } | |
497 | ||
c752666c SW |
498 | /* |
499 | * Other possible things to check: | |
500 | * - Inode located within filesystem size (and on valid block) | |
501 | * - Valid directory entry type | |
502 | * Not sure how heavy-weight we want to make this... could also check | |
503 | * hash is correct for example, but that would take a lot of extra time. | |
504 | * For now the most important thing is to check that the various sizes | |
505 | * are correct. | |
506 | */ | |
507 | static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset, | |
508 | unsigned int size, unsigned int len, int first) | |
509 | { | |
510 | const char *msg = "gfs2_dirent too small"; | |
511 | if (unlikely(size < sizeof(struct gfs2_dirent))) | |
512 | goto error; | |
513 | msg = "gfs2_dirent misaligned"; | |
514 | if (unlikely(offset & 0x7)) | |
515 | goto error; | |
516 | msg = "gfs2_dirent points beyond end of block"; | |
517 | if (unlikely(offset + size > len)) | |
518 | goto error; | |
519 | msg = "zero inode number"; | |
5e7d65cd | 520 | if (unlikely(!first && gfs2_dirent_sentinel(dent))) |
c752666c SW |
521 | goto error; |
522 | msg = "name length is greater than space in dirent"; | |
5e7d65cd | 523 | if (!gfs2_dirent_sentinel(dent) && |
c752666c SW |
524 | unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) > |
525 | size)) | |
526 | goto error; | |
527 | return 0; | |
528 | error: | |
d77d1b58 JP |
529 | pr_warn("%s: %s (%s)\n", |
530 | __func__, msg, first ? "first in block" : "not first in block"); | |
c752666c | 531 | return -EIO; |
b3b94faa DT |
532 | } |
533 | ||
71b86f56 | 534 | static int gfs2_dirent_offset(const void *buf) |
c752666c | 535 | { |
71b86f56 SW |
536 | const struct gfs2_meta_header *h = buf; |
537 | int offset; | |
c752666c SW |
538 | |
539 | BUG_ON(buf == NULL); | |
c752666c | 540 | |
e3167ded | 541 | switch(be32_to_cpu(h->mh_type)) { |
c752666c SW |
542 | case GFS2_METATYPE_LF: |
543 | offset = sizeof(struct gfs2_leaf); | |
544 | break; | |
545 | case GFS2_METATYPE_DI: | |
546 | offset = sizeof(struct gfs2_dinode); | |
547 | break; | |
548 | default: | |
549 | goto wrong_type; | |
550 | } | |
71b86f56 SW |
551 | return offset; |
552 | wrong_type: | |
d77d1b58 | 553 | pr_warn("%s: wrong block type %u\n", __func__, be32_to_cpu(h->mh_type)); |
71b86f56 SW |
554 | return -1; |
555 | } | |
556 | ||
2bdbc5d7 | 557 | static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf, |
71b86f56 SW |
558 | unsigned int len, gfs2_dscan_t scan, |
559 | const struct qstr *name, | |
560 | void *opaque) | |
561 | { | |
562 | struct gfs2_dirent *dent, *prev; | |
563 | unsigned offset; | |
564 | unsigned size; | |
565 | int ret = 0; | |
c752666c | 566 | |
71b86f56 SW |
567 | ret = gfs2_dirent_offset(buf); |
568 | if (ret < 0) | |
569 | goto consist_inode; | |
570 | ||
571 | offset = ret; | |
c752666c | 572 | prev = NULL; |
2bdbc5d7 | 573 | dent = buf + offset; |
c752666c SW |
574 | size = be16_to_cpu(dent->de_rec_len); |
575 | if (gfs2_check_dirent(dent, offset, size, len, 1)) | |
576 | goto consist_inode; | |
577 | do { | |
71b86f56 | 578 | ret = scan(dent, name, opaque); |
c752666c SW |
579 | if (ret) |
580 | break; | |
581 | offset += size; | |
582 | if (offset == len) | |
583 | break; | |
584 | prev = dent; | |
2bdbc5d7 | 585 | dent = buf + offset; |
c752666c SW |
586 | size = be16_to_cpu(dent->de_rec_len); |
587 | if (gfs2_check_dirent(dent, offset, size, len, 0)) | |
588 | goto consist_inode; | |
589 | } while(1); | |
590 | ||
591 | switch(ret) { | |
592 | case 0: | |
593 | return NULL; | |
594 | case 1: | |
595 | return dent; | |
596 | case 2: | |
597 | return prev ? prev : dent; | |
598 | default: | |
599 | BUG_ON(ret > 0); | |
600 | return ERR_PTR(ret); | |
601 | } | |
602 | ||
c752666c | 603 | consist_inode: |
feaa7bba | 604 | gfs2_consist_inode(GFS2_I(inode)); |
c752666c SW |
605 | return ERR_PTR(-EIO); |
606 | } | |
607 | ||
2bdbc5d7 SW |
608 | static int dirent_check_reclen(struct gfs2_inode *dip, |
609 | const struct gfs2_dirent *d, const void *end_p) | |
610 | { | |
611 | const void *ptr = d; | |
612 | u16 rec_len = be16_to_cpu(d->de_rec_len); | |
613 | ||
614 | if (unlikely(rec_len < sizeof(struct gfs2_dirent))) | |
615 | goto broken; | |
616 | ptr += rec_len; | |
617 | if (ptr < end_p) | |
618 | return rec_len; | |
619 | if (ptr == end_p) | |
620 | return -ENOENT; | |
621 | broken: | |
622 | gfs2_consist_inode(dip); | |
623 | return -EIO; | |
624 | } | |
625 | ||
b3b94faa DT |
626 | /** |
627 | * dirent_next - Next dirent | |
628 | * @dip: the directory | |
629 | * @bh: The buffer | |
630 | * @dent: Pointer to list of dirents | |
631 | * | |
632 | * Returns: 0 on success, error code otherwise | |
633 | */ | |
634 | ||
635 | static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh, | |
636 | struct gfs2_dirent **dent) | |
637 | { | |
2bdbc5d7 SW |
638 | struct gfs2_dirent *cur = *dent, *tmp; |
639 | char *bh_end = bh->b_data + bh->b_size; | |
640 | int ret; | |
b3b94faa | 641 | |
2bdbc5d7 SW |
642 | ret = dirent_check_reclen(dip, cur, bh_end); |
643 | if (ret < 0) | |
644 | return ret; | |
4dd651ad | 645 | |
2bdbc5d7 SW |
646 | tmp = (void *)cur + ret; |
647 | ret = dirent_check_reclen(dip, tmp, bh_end); | |
648 | if (ret == -EIO) | |
649 | return ret; | |
4dd651ad | 650 | |
b3b94faa | 651 | /* Only the first dent could ever have de_inum.no_addr == 0 */ |
5e7d65cd | 652 | if (gfs2_dirent_sentinel(tmp)) { |
b3b94faa DT |
653 | gfs2_consist_inode(dip); |
654 | return -EIO; | |
655 | } | |
656 | ||
657 | *dent = tmp; | |
b3b94faa DT |
658 | return 0; |
659 | } | |
660 | ||
661 | /** | |
662 | * dirent_del - Delete a dirent | |
663 | * @dip: The GFS2 inode | |
664 | * @bh: The buffer | |
665 | * @prev: The previous dirent | |
666 | * @cur: The current dirent | |
667 | * | |
668 | */ | |
669 | ||
670 | static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh, | |
671 | struct gfs2_dirent *prev, struct gfs2_dirent *cur) | |
672 | { | |
cd915493 | 673 | u16 cur_rec_len, prev_rec_len; |
b3b94faa | 674 | |
5e7d65cd | 675 | if (gfs2_dirent_sentinel(cur)) { |
b3b94faa DT |
676 | gfs2_consist_inode(dip); |
677 | return; | |
678 | } | |
679 | ||
350a9b0a | 680 | gfs2_trans_add_meta(dip->i_gl, bh); |
b3b94faa DT |
681 | |
682 | /* If there is no prev entry, this is the first entry in the block. | |
683 | The de_rec_len is already as big as it needs to be. Just zero | |
684 | out the inode number and return. */ | |
685 | ||
686 | if (!prev) { | |
5e7d65cd SW |
687 | cur->de_inum.no_addr = 0; |
688 | cur->de_inum.no_formal_ino = 0; | |
b3b94faa DT |
689 | return; |
690 | } | |
691 | ||
692 | /* Combine this dentry with the previous one. */ | |
693 | ||
fc69d0d3 SW |
694 | prev_rec_len = be16_to_cpu(prev->de_rec_len); |
695 | cur_rec_len = be16_to_cpu(cur->de_rec_len); | |
b3b94faa DT |
696 | |
697 | if ((char *)prev + prev_rec_len != (char *)cur) | |
698 | gfs2_consist_inode(dip); | |
699 | if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size) | |
700 | gfs2_consist_inode(dip); | |
701 | ||
702 | prev_rec_len += cur_rec_len; | |
fc69d0d3 | 703 | prev->de_rec_len = cpu_to_be16(prev_rec_len); |
b3b94faa DT |
704 | } |
705 | ||
34017472 BM |
706 | |
707 | static struct gfs2_dirent *do_init_dirent(struct inode *inode, | |
708 | struct gfs2_dirent *dent, | |
709 | const struct qstr *name, | |
710 | struct buffer_head *bh, | |
711 | unsigned offset) | |
712 | { | |
713 | struct gfs2_inode *ip = GFS2_I(inode); | |
714 | struct gfs2_dirent *ndent; | |
715 | unsigned totlen; | |
716 | ||
717 | totlen = be16_to_cpu(dent->de_rec_len); | |
718 | BUG_ON(offset + name->len > totlen); | |
719 | gfs2_trans_add_meta(ip->i_gl, bh); | |
720 | ndent = (struct gfs2_dirent *)((char *)dent + offset); | |
721 | dent->de_rec_len = cpu_to_be16(offset); | |
722 | gfs2_qstr2dirent(name, totlen - offset, ndent); | |
723 | return ndent; | |
724 | } | |
725 | ||
726 | ||
c752666c SW |
727 | /* |
728 | * Takes a dent from which to grab space as an argument. Returns the | |
729 | * newly created dent. | |
b3b94faa | 730 | */ |
08bc2dbc AB |
731 | static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode, |
732 | struct gfs2_dirent *dent, | |
733 | const struct qstr *name, | |
734 | struct buffer_head *bh) | |
b3b94faa | 735 | { |
34017472 | 736 | unsigned offset = 0; |
c752666c | 737 | |
5e7d65cd | 738 | if (!gfs2_dirent_sentinel(dent)) |
c752666c | 739 | offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); |
34017472 | 740 | return do_init_dirent(inode, dent, name, bh, offset); |
b3b94faa DT |
741 | } |
742 | ||
34017472 BM |
743 | static struct gfs2_dirent *gfs2_dirent_split_alloc(struct inode *inode, |
744 | struct buffer_head *bh, | |
745 | const struct qstr *name, | |
746 | void *ptr) | |
b3b94faa DT |
747 | { |
748 | struct gfs2_dirent *dent; | |
907b9bce | 749 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
34017472 | 750 | gfs2_dirent_find_offset, name, ptr); |
c752666c SW |
751 | if (!dent || IS_ERR(dent)) |
752 | return dent; | |
34017472 BM |
753 | return do_init_dirent(inode, dent, name, bh, |
754 | (unsigned)(ptr - (void *)dent)); | |
b3b94faa DT |
755 | } |
756 | ||
cd915493 | 757 | static int get_leaf(struct gfs2_inode *dip, u64 leaf_no, |
b3b94faa DT |
758 | struct buffer_head **bhp) |
759 | { | |
760 | int error; | |
761 | ||
c8d57703 | 762 | error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, 0, bhp); |
feaa7bba | 763 | if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) { |
d77d1b58 | 764 | /* pr_info("block num=%llu\n", leaf_no); */ |
b3b94faa | 765 | error = -EIO; |
feaa7bba | 766 | } |
b3b94faa DT |
767 | |
768 | return error; | |
769 | } | |
770 | ||
771 | /** | |
772 | * get_leaf_nr - Get a leaf number associated with the index | |
773 | * @dip: The GFS2 inode | |
774 | * @index: | |
775 | * @leaf_out: | |
776 | * | |
777 | * Returns: 0 on success, error code otherwise | |
778 | */ | |
779 | ||
cd915493 SW |
780 | static int get_leaf_nr(struct gfs2_inode *dip, u32 index, |
781 | u64 *leaf_out) | |
b3b94faa | 782 | { |
17d539f0 | 783 | __be64 *hash; |
b3b94faa | 784 | |
17d539f0 SW |
785 | hash = gfs2_dir_get_hash_table(dip); |
786 | if (IS_ERR(hash)) | |
787 | return PTR_ERR(hash); | |
788 | *leaf_out = be64_to_cpu(*(hash + index)); | |
b3b94faa DT |
789 | return 0; |
790 | } | |
791 | ||
cd915493 | 792 | static int get_first_leaf(struct gfs2_inode *dip, u32 index, |
b3b94faa DT |
793 | struct buffer_head **bh_out) |
794 | { | |
cd915493 | 795 | u64 leaf_no; |
b3b94faa DT |
796 | int error; |
797 | ||
798 | error = get_leaf_nr(dip, index, &leaf_no); | |
799 | if (!error) | |
800 | error = get_leaf(dip, leaf_no, bh_out); | |
801 | ||
802 | return error; | |
803 | } | |
804 | ||
c752666c SW |
805 | static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode, |
806 | const struct qstr *name, | |
807 | gfs2_dscan_t scan, | |
808 | struct buffer_head **pbh) | |
b3b94faa | 809 | { |
c752666c SW |
810 | struct buffer_head *bh; |
811 | struct gfs2_dirent *dent; | |
feaa7bba | 812 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa DT |
813 | int error; |
814 | ||
383f01fb | 815 | if (ip->i_diskflags & GFS2_DIF_EXHASH) { |
c752666c | 816 | struct gfs2_leaf *leaf; |
9a004508 | 817 | unsigned hsize = 1 << ip->i_depth; |
c752666c SW |
818 | unsigned index; |
819 | u64 ln; | |
a2e0f799 | 820 | if (hsize * sizeof(u64) != i_size_read(inode)) { |
c752666c SW |
821 | gfs2_consist_inode(ip); |
822 | return ERR_PTR(-EIO); | |
823 | } | |
907b9bce | 824 | |
9a004508 | 825 | index = name->hash >> (32 - ip->i_depth); |
c752666c SW |
826 | error = get_first_leaf(ip, index, &bh); |
827 | if (error) | |
828 | return ERR_PTR(error); | |
829 | do { | |
830 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, | |
71b86f56 | 831 | scan, name, NULL); |
c752666c SW |
832 | if (dent) |
833 | goto got_dent; | |
834 | leaf = (struct gfs2_leaf *)bh->b_data; | |
835 | ln = be64_to_cpu(leaf->lf_next); | |
f4154ea0 | 836 | brelse(bh); |
c752666c SW |
837 | if (!ln) |
838 | break; | |
907b9bce | 839 | |
c752666c SW |
840 | error = get_leaf(ip, ln, &bh); |
841 | } while(!error); | |
b3b94faa | 842 | |
c752666c | 843 | return error ? ERR_PTR(error) : NULL; |
b3b94faa | 844 | } |
b3b94faa | 845 | |
907b9bce | 846 | |
c752666c SW |
847 | error = gfs2_meta_inode_buffer(ip, &bh); |
848 | if (error) | |
849 | return ERR_PTR(error); | |
71b86f56 | 850 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL); |
c752666c | 851 | got_dent: |
f4154ea0 | 852 | if (unlikely(dent == NULL || IS_ERR(dent))) { |
ed386507 SW |
853 | brelse(bh); |
854 | bh = NULL; | |
855 | } | |
c752666c SW |
856 | *pbh = bh; |
857 | return dent; | |
858 | } | |
b3b94faa | 859 | |
c752666c SW |
860 | static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth) |
861 | { | |
feaa7bba | 862 | struct gfs2_inode *ip = GFS2_I(inode); |
b45e41d7 | 863 | unsigned int n = 1; |
09010978 SW |
864 | u64 bn; |
865 | int error; | |
866 | struct buffer_head *bh; | |
c752666c SW |
867 | struct gfs2_leaf *leaf; |
868 | struct gfs2_dirent *dent; | |
26fe5750 | 869 | struct qstr name = { .name = "" }; |
01bcb0de | 870 | struct timespec tv = CURRENT_TIME; |
09010978 | 871 | |
6e87ed0f | 872 | error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL); |
09010978 SW |
873 | if (error) |
874 | return NULL; | |
875 | bh = gfs2_meta_new(ip->i_gl, bn); | |
c752666c SW |
876 | if (!bh) |
877 | return NULL; | |
09010978 | 878 | |
5731be53 | 879 | gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1); |
350a9b0a | 880 | gfs2_trans_add_meta(ip->i_gl, bh); |
c752666c SW |
881 | gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF); |
882 | leaf = (struct gfs2_leaf *)bh->b_data; | |
883 | leaf->lf_depth = cpu_to_be16(depth); | |
2bdbc5d7 | 884 | leaf->lf_entries = 0; |
a2d7d021 | 885 | leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE); |
2bdbc5d7 | 886 | leaf->lf_next = 0; |
01bcb0de SW |
887 | leaf->lf_inode = cpu_to_be64(ip->i_no_addr); |
888 | leaf->lf_dist = cpu_to_be32(1); | |
889 | leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); | |
890 | leaf->lf_sec = cpu_to_be64(tv.tv_sec); | |
891 | memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2)); | |
c752666c | 892 | dent = (struct gfs2_dirent *)(leaf+1); |
71b86f56 | 893 | gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent); |
c752666c SW |
894 | *pbh = bh; |
895 | return leaf; | |
b3b94faa DT |
896 | } |
897 | ||
898 | /** | |
899 | * dir_make_exhash - Convert a stuffed directory into an ExHash directory | |
900 | * @dip: The GFS2 inode | |
901 | * | |
902 | * Returns: 0 on success, error code otherwise | |
903 | */ | |
904 | ||
c752666c | 905 | static int dir_make_exhash(struct inode *inode) |
b3b94faa | 906 | { |
feaa7bba SW |
907 | struct gfs2_inode *dip = GFS2_I(inode); |
908 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
b3b94faa | 909 | struct gfs2_dirent *dent; |
c752666c | 910 | struct qstr args; |
b3b94faa DT |
911 | struct buffer_head *bh, *dibh; |
912 | struct gfs2_leaf *leaf; | |
913 | int y; | |
cd915493 | 914 | u32 x; |
b44b84d7 AV |
915 | __be64 *lp; |
916 | u64 bn; | |
b3b94faa DT |
917 | int error; |
918 | ||
919 | error = gfs2_meta_inode_buffer(dip, &dibh); | |
920 | if (error) | |
921 | return error; | |
922 | ||
b3b94faa DT |
923 | /* Turn over a new leaf */ |
924 | ||
c752666c SW |
925 | leaf = new_leaf(inode, &bh, 0); |
926 | if (!leaf) | |
927 | return -ENOSPC; | |
928 | bn = bh->b_blocknr; | |
b3b94faa | 929 | |
ad6203f2 SW |
930 | gfs2_assert(sdp, dip->i_entries < (1 << 16)); |
931 | leaf->lf_entries = cpu_to_be16(dip->i_entries); | |
b3b94faa DT |
932 | |
933 | /* Copy dirents */ | |
934 | ||
935 | gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh, | |
936 | sizeof(struct gfs2_dinode)); | |
937 | ||
938 | /* Find last entry */ | |
939 | ||
940 | x = 0; | |
c752666c SW |
941 | args.len = bh->b_size - sizeof(struct gfs2_dinode) + |
942 | sizeof(struct gfs2_leaf); | |
943 | args.name = bh->b_data; | |
feaa7bba | 944 | dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size, |
71b86f56 | 945 | gfs2_dirent_last, &args, NULL); |
c752666c SW |
946 | if (!dent) { |
947 | brelse(bh); | |
948 | brelse(dibh); | |
949 | return -EIO; | |
950 | } | |
951 | if (IS_ERR(dent)) { | |
952 | brelse(bh); | |
953 | brelse(dibh); | |
954 | return PTR_ERR(dent); | |
b3b94faa | 955 | } |
b3b94faa DT |
956 | |
957 | /* Adjust the last dirent's record length | |
958 | (Remember that dent still points to the last entry.) */ | |
959 | ||
4dd651ad | 960 | dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) + |
b3b94faa | 961 | sizeof(struct gfs2_dinode) - |
4dd651ad | 962 | sizeof(struct gfs2_leaf)); |
b3b94faa DT |
963 | |
964 | brelse(bh); | |
965 | ||
966 | /* We're done with the new leaf block, now setup the new | |
967 | hash table. */ | |
968 | ||
350a9b0a | 969 | gfs2_trans_add_meta(dip->i_gl, dibh); |
b3b94faa DT |
970 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
971 | ||
b44b84d7 | 972 | lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode)); |
b3b94faa DT |
973 | |
974 | for (x = sdp->sd_hash_ptrs; x--; lp++) | |
975 | *lp = cpu_to_be64(bn); | |
976 | ||
a2e0f799 | 977 | i_size_write(inode, sdp->sd_sb.sb_bsize / 2); |
77658aad | 978 | gfs2_add_inode_blocks(&dip->i_inode, 1); |
383f01fb | 979 | dip->i_diskflags |= GFS2_DIF_EXHASH; |
b3b94faa DT |
980 | |
981 | for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ; | |
9a004508 | 982 | dip->i_depth = y; |
b3b94faa | 983 | |
539e5d6b | 984 | gfs2_dinode_out(dip, dibh->b_data); |
b3b94faa DT |
985 | |
986 | brelse(dibh); | |
987 | ||
988 | return 0; | |
989 | } | |
990 | ||
991 | /** | |
992 | * dir_split_leaf - Split a leaf block into two | |
993 | * @dip: The GFS2 inode | |
994 | * @index: | |
995 | * @leaf_no: | |
996 | * | |
997 | * Returns: 0 on success, error code on failure | |
998 | */ | |
999 | ||
c752666c | 1000 | static int dir_split_leaf(struct inode *inode, const struct qstr *name) |
b3b94faa | 1001 | { |
feaa7bba | 1002 | struct gfs2_inode *dip = GFS2_I(inode); |
b3b94faa DT |
1003 | struct buffer_head *nbh, *obh, *dibh; |
1004 | struct gfs2_leaf *nleaf, *oleaf; | |
4da3c646 | 1005 | struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new; |
cd915493 | 1006 | u32 start, len, half_len, divider; |
b44b84d7 AV |
1007 | u64 bn, leaf_no; |
1008 | __be64 *lp; | |
cd915493 | 1009 | u32 index; |
b3b94faa DT |
1010 | int x, moved = 0; |
1011 | int error; | |
1012 | ||
9a004508 | 1013 | index = name->hash >> (32 - dip->i_depth); |
c752666c SW |
1014 | error = get_leaf_nr(dip, index, &leaf_no); |
1015 | if (error) | |
1016 | return error; | |
b3b94faa DT |
1017 | |
1018 | /* Get the old leaf block */ | |
b3b94faa DT |
1019 | error = get_leaf(dip, leaf_no, &obh); |
1020 | if (error) | |
e90deff5 | 1021 | return error; |
b3b94faa | 1022 | |
b3b94faa | 1023 | oleaf = (struct gfs2_leaf *)obh->b_data; |
9a004508 | 1024 | if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) { |
e90deff5 SW |
1025 | brelse(obh); |
1026 | return 1; /* can't split */ | |
1027 | } | |
1028 | ||
350a9b0a | 1029 | gfs2_trans_add_meta(dip->i_gl, obh); |
b3b94faa | 1030 | |
c752666c SW |
1031 | nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1); |
1032 | if (!nleaf) { | |
1033 | brelse(obh); | |
1034 | return -ENOSPC; | |
1035 | } | |
1036 | bn = nbh->b_blocknr; | |
b3b94faa | 1037 | |
c752666c | 1038 | /* Compute the start and len of leaf pointers in the hash table. */ |
9a004508 | 1039 | len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth)); |
b3b94faa DT |
1040 | half_len = len >> 1; |
1041 | if (!half_len) { | |
d77d1b58 JP |
1042 | pr_warn("i_depth %u lf_depth %u index %u\n", |
1043 | dip->i_depth, be16_to_cpu(oleaf->lf_depth), index); | |
b3b94faa DT |
1044 | gfs2_consist_inode(dip); |
1045 | error = -EIO; | |
1046 | goto fail_brelse; | |
1047 | } | |
1048 | ||
1049 | start = (index & ~(len - 1)); | |
1050 | ||
1051 | /* Change the pointers. | |
1052 | Don't bother distinguishing stuffed from non-stuffed. | |
1053 | This code is complicated enough already. */ | |
4244b52e DR |
1054 | lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS); |
1055 | if (!lp) { | |
1056 | error = -ENOMEM; | |
1057 | goto fail_brelse; | |
1058 | } | |
1059 | ||
b3b94faa | 1060 | /* Change the pointers */ |
b3b94faa DT |
1061 | for (x = 0; x < half_len; x++) |
1062 | lp[x] = cpu_to_be64(bn); | |
1063 | ||
17d539f0 SW |
1064 | gfs2_dir_hash_inval(dip); |
1065 | ||
cd915493 SW |
1066 | error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64), |
1067 | half_len * sizeof(u64)); | |
1068 | if (error != half_len * sizeof(u64)) { | |
b3b94faa DT |
1069 | if (error >= 0) |
1070 | error = -EIO; | |
1071 | goto fail_lpfree; | |
1072 | } | |
1073 | ||
1074 | kfree(lp); | |
1075 | ||
1076 | /* Compute the divider */ | |
9a004508 | 1077 | divider = (start + half_len) << (32 - dip->i_depth); |
b3b94faa DT |
1078 | |
1079 | /* Copy the entries */ | |
1579343a | 1080 | dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf)); |
b3b94faa DT |
1081 | |
1082 | do { | |
1083 | next = dent; | |
1084 | if (dirent_next(dip, obh, &next)) | |
1085 | next = NULL; | |
1086 | ||
5e7d65cd | 1087 | if (!gfs2_dirent_sentinel(dent) && |
b3b94faa | 1088 | be32_to_cpu(dent->de_hash) < divider) { |
c752666c | 1089 | struct qstr str; |
34017472 | 1090 | void *ptr = ((char *)dent - obh->b_data) + nbh->b_data; |
c752666c SW |
1091 | str.name = (char*)(dent+1); |
1092 | str.len = be16_to_cpu(dent->de_name_len); | |
1093 | str.hash = be32_to_cpu(dent->de_hash); | |
34017472 | 1094 | new = gfs2_dirent_split_alloc(inode, nbh, &str, ptr); |
c752666c SW |
1095 | if (IS_ERR(new)) { |
1096 | error = PTR_ERR(new); | |
1097 | break; | |
1098 | } | |
b3b94faa DT |
1099 | |
1100 | new->de_inum = dent->de_inum; /* No endian worries */ | |
b3b94faa | 1101 | new->de_type = dent->de_type; /* No endian worries */ |
bb16b342 | 1102 | be16_add_cpu(&nleaf->lf_entries, 1); |
b3b94faa DT |
1103 | |
1104 | dirent_del(dip, obh, prev, dent); | |
1105 | ||
1106 | if (!oleaf->lf_entries) | |
1107 | gfs2_consist_inode(dip); | |
bb16b342 | 1108 | be16_add_cpu(&oleaf->lf_entries, -1); |
b3b94faa DT |
1109 | |
1110 | if (!prev) | |
1111 | prev = dent; | |
1112 | ||
1113 | moved = 1; | |
c752666c | 1114 | } else { |
b3b94faa | 1115 | prev = dent; |
c752666c | 1116 | } |
b3b94faa | 1117 | dent = next; |
c752666c | 1118 | } while (dent); |
b3b94faa | 1119 | |
c752666c | 1120 | oleaf->lf_depth = nleaf->lf_depth; |
b3b94faa DT |
1121 | |
1122 | error = gfs2_meta_inode_buffer(dip, &dibh); | |
feaa7bba | 1123 | if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) { |
350a9b0a | 1124 | gfs2_trans_add_meta(dip->i_gl, dibh); |
77658aad | 1125 | gfs2_add_inode_blocks(&dip->i_inode, 1); |
539e5d6b | 1126 | gfs2_dinode_out(dip, dibh->b_data); |
b3b94faa DT |
1127 | brelse(dibh); |
1128 | } | |
1129 | ||
1130 | brelse(obh); | |
1131 | brelse(nbh); | |
1132 | ||
1133 | return error; | |
1134 | ||
e90deff5 | 1135 | fail_lpfree: |
b3b94faa DT |
1136 | kfree(lp); |
1137 | ||
e90deff5 | 1138 | fail_brelse: |
b3b94faa | 1139 | brelse(obh); |
b3b94faa DT |
1140 | brelse(nbh); |
1141 | return error; | |
1142 | } | |
1143 | ||
1144 | /** | |
1145 | * dir_double_exhash - Double size of ExHash table | |
1146 | * @dip: The GFS2 dinode | |
1147 | * | |
1148 | * Returns: 0 on success, error code on failure | |
1149 | */ | |
1150 | ||
1151 | static int dir_double_exhash(struct gfs2_inode *dip) | |
1152 | { | |
b3b94faa | 1153 | struct buffer_head *dibh; |
cd915493 | 1154 | u32 hsize; |
17d539f0 SW |
1155 | u32 hsize_bytes; |
1156 | __be64 *hc; | |
1157 | __be64 *hc2, *h; | |
b3b94faa DT |
1158 | int x; |
1159 | int error = 0; | |
1160 | ||
9a004508 | 1161 | hsize = 1 << dip->i_depth; |
17d539f0 | 1162 | hsize_bytes = hsize * sizeof(__be64); |
b3b94faa | 1163 | |
17d539f0 SW |
1164 | hc = gfs2_dir_get_hash_table(dip); |
1165 | if (IS_ERR(hc)) | |
1166 | return PTR_ERR(hc); | |
b3b94faa | 1167 | |
512cbf02 | 1168 | hc2 = kmalloc(hsize_bytes * 2, GFP_NOFS | __GFP_NOWARN); |
e8830d88 BP |
1169 | if (hc2 == NULL) |
1170 | hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS, PAGE_KERNEL); | |
1171 | ||
17d539f0 | 1172 | if (!hc2) |
4244b52e | 1173 | return -ENOMEM; |
b3b94faa | 1174 | |
512cbf02 | 1175 | h = hc2; |
17d539f0 SW |
1176 | error = gfs2_meta_inode_buffer(dip, &dibh); |
1177 | if (error) | |
1178 | goto out_kfree; | |
b3b94faa | 1179 | |
17d539f0 SW |
1180 | for (x = 0; x < hsize; x++) { |
1181 | *h++ = *hc; | |
1182 | *h++ = *hc; | |
1183 | hc++; | |
b3b94faa DT |
1184 | } |
1185 | ||
17d539f0 SW |
1186 | error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2); |
1187 | if (error != (hsize_bytes * 2)) | |
1188 | goto fail; | |
b3b94faa | 1189 | |
17d539f0 SW |
1190 | gfs2_dir_hash_inval(dip); |
1191 | dip->i_hash_cache = hc2; | |
1192 | dip->i_depth++; | |
1193 | gfs2_dinode_out(dip, dibh->b_data); | |
1194 | brelse(dibh); | |
1195 | return 0; | |
b3b94faa | 1196 | |
a91ea69f | 1197 | fail: |
17d539f0 SW |
1198 | /* Replace original hash table & size */ |
1199 | gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes); | |
1200 | i_size_write(&dip->i_inode, hsize_bytes); | |
1201 | gfs2_dinode_out(dip, dibh->b_data); | |
1202 | brelse(dibh); | |
1203 | out_kfree: | |
3cdcf63e | 1204 | kvfree(hc2); |
b3b94faa DT |
1205 | return error; |
1206 | } | |
1207 | ||
1208 | /** | |
1209 | * compare_dents - compare directory entries by hash value | |
1210 | * @a: first dent | |
1211 | * @b: second dent | |
1212 | * | |
1213 | * When comparing the hash entries of @a to @b: | |
1214 | * gt: returns 1 | |
1215 | * lt: returns -1 | |
1216 | * eq: returns 0 | |
1217 | */ | |
1218 | ||
1219 | static int compare_dents(const void *a, const void *b) | |
1220 | { | |
2bdbc5d7 | 1221 | const struct gfs2_dirent *dent_a, *dent_b; |
cd915493 | 1222 | u32 hash_a, hash_b; |
b3b94faa DT |
1223 | int ret = 0; |
1224 | ||
2bdbc5d7 | 1225 | dent_a = *(const struct gfs2_dirent **)a; |
c752666c | 1226 | hash_a = be32_to_cpu(dent_a->de_hash); |
b3b94faa | 1227 | |
2bdbc5d7 | 1228 | dent_b = *(const struct gfs2_dirent **)b; |
c752666c | 1229 | hash_b = be32_to_cpu(dent_b->de_hash); |
b3b94faa DT |
1230 | |
1231 | if (hash_a > hash_b) | |
1232 | ret = 1; | |
1233 | else if (hash_a < hash_b) | |
1234 | ret = -1; | |
1235 | else { | |
4dd651ad SW |
1236 | unsigned int len_a = be16_to_cpu(dent_a->de_name_len); |
1237 | unsigned int len_b = be16_to_cpu(dent_b->de_name_len); | |
b3b94faa DT |
1238 | |
1239 | if (len_a > len_b) | |
1240 | ret = 1; | |
1241 | else if (len_a < len_b) | |
1242 | ret = -1; | |
1243 | else | |
2bdbc5d7 | 1244 | ret = memcmp(dent_a + 1, dent_b + 1, len_a); |
b3b94faa DT |
1245 | } |
1246 | ||
1247 | return ret; | |
1248 | } | |
1249 | ||
1250 | /** | |
1251 | * do_filldir_main - read out directory entries | |
1252 | * @dip: The GFS2 inode | |
d81a8ef5 | 1253 | * @ctx: what to feed the entries to |
b3b94faa DT |
1254 | * @darr: an array of struct gfs2_dirent pointers to read |
1255 | * @entries: the number of entries in darr | |
1256 | * @copied: pointer to int that's non-zero if a entry has been copied out | |
1257 | * | |
1258 | * Jump through some hoops to make sure that if there are hash collsions, | |
1259 | * they are read out at the beginning of a buffer. We want to minimize | |
1260 | * the possibility that they will fall into different readdir buffers or | |
1261 | * that someone will want to seek to that location. | |
1262 | * | |
d81a8ef5 | 1263 | * Returns: errno, >0 if the actor tells you to stop |
b3b94faa DT |
1264 | */ |
1265 | ||
d81a8ef5 | 1266 | static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx, |
cd915493 | 1267 | const struct gfs2_dirent **darr, u32 entries, |
b3b94faa DT |
1268 | int *copied) |
1269 | { | |
71b86f56 | 1270 | const struct gfs2_dirent *dent, *dent_next; |
cd915493 | 1271 | u64 off, off_next; |
b3b94faa DT |
1272 | unsigned int x, y; |
1273 | int run = 0; | |
b3b94faa DT |
1274 | |
1275 | sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL); | |
1276 | ||
1277 | dent_next = darr[0]; | |
1278 | off_next = be32_to_cpu(dent_next->de_hash); | |
1279 | off_next = gfs2_disk_hash2offset(off_next); | |
1280 | ||
1281 | for (x = 0, y = 1; x < entries; x++, y++) { | |
1282 | dent = dent_next; | |
1283 | off = off_next; | |
1284 | ||
1285 | if (y < entries) { | |
1286 | dent_next = darr[y]; | |
1287 | off_next = be32_to_cpu(dent_next->de_hash); | |
1288 | off_next = gfs2_disk_hash2offset(off_next); | |
1289 | ||
d81a8ef5 | 1290 | if (off < ctx->pos) |
b3b94faa | 1291 | continue; |
d81a8ef5 | 1292 | ctx->pos = off; |
b3b94faa DT |
1293 | |
1294 | if (off_next == off) { | |
1295 | if (*copied && !run) | |
1296 | return 1; | |
1297 | run = 1; | |
1298 | } else | |
1299 | run = 0; | |
1300 | } else { | |
d81a8ef5 | 1301 | if (off < ctx->pos) |
b3b94faa | 1302 | continue; |
d81a8ef5 | 1303 | ctx->pos = off; |
b3b94faa DT |
1304 | } |
1305 | ||
d81a8ef5 | 1306 | if (!dir_emit(ctx, (const char *)(dent + 1), |
4dd651ad | 1307 | be16_to_cpu(dent->de_name_len), |
d81a8ef5 AV |
1308 | be64_to_cpu(dent->de_inum.no_addr), |
1309 | be16_to_cpu(dent->de_type))) | |
b3b94faa DT |
1310 | return 1; |
1311 | ||
1312 | *copied = 1; | |
1313 | } | |
1314 | ||
d81a8ef5 | 1315 | /* Increment the ctx->pos by one, so the next time we come into the |
b3b94faa DT |
1316 | do_filldir fxn, we get the next entry instead of the last one in the |
1317 | current leaf */ | |
1318 | ||
d81a8ef5 | 1319 | ctx->pos++; |
b3b94faa DT |
1320 | |
1321 | return 0; | |
1322 | } | |
1323 | ||
d2a97a4e SW |
1324 | static void *gfs2_alloc_sort_buffer(unsigned size) |
1325 | { | |
1326 | void *ptr = NULL; | |
1327 | ||
1328 | if (size < KMALLOC_MAX_SIZE) | |
1329 | ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN); | |
1330 | if (!ptr) | |
1331 | ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL); | |
1332 | return ptr; | |
1333 | } | |
1334 | ||
d81a8ef5 AV |
1335 | static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx, |
1336 | int *copied, unsigned *depth, | |
3699e3a4 | 1337 | u64 leaf_no) |
b3b94faa | 1338 | { |
feaa7bba | 1339 | struct gfs2_inode *ip = GFS2_I(inode); |
bdd19a22 | 1340 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
71b86f56 SW |
1341 | struct buffer_head *bh; |
1342 | struct gfs2_leaf *lf; | |
bdd19a22 | 1343 | unsigned entries = 0, entries2 = 0; |
71b86f56 SW |
1344 | unsigned leaves = 0; |
1345 | const struct gfs2_dirent **darr, *dent; | |
1346 | struct dirent_gather g; | |
1347 | struct buffer_head **larr; | |
1348 | int leaf = 0; | |
1349 | int error, i; | |
1350 | u64 lfn = leaf_no; | |
b3b94faa | 1351 | |
b3b94faa | 1352 | do { |
71b86f56 | 1353 | error = get_leaf(ip, lfn, &bh); |
b3b94faa | 1354 | if (error) |
71b86f56 SW |
1355 | goto out; |
1356 | lf = (struct gfs2_leaf *)bh->b_data; | |
1357 | if (leaves == 0) | |
1358 | *depth = be16_to_cpu(lf->lf_depth); | |
1359 | entries += be16_to_cpu(lf->lf_entries); | |
1360 | leaves++; | |
1361 | lfn = be64_to_cpu(lf->lf_next); | |
1362 | brelse(bh); | |
1363 | } while(lfn); | |
b3b94faa DT |
1364 | |
1365 | if (!entries) | |
1366 | return 0; | |
1367 | ||
71b86f56 | 1368 | error = -ENOMEM; |
bdd19a22 SW |
1369 | /* |
1370 | * The extra 99 entries are not normally used, but are a buffer | |
1371 | * zone in case the number of entries in the leaf is corrupt. | |
1372 | * 99 is the maximum number of entries that can fit in a single | |
1373 | * leaf block. | |
1374 | */ | |
d2a97a4e | 1375 | larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *)); |
71b86f56 SW |
1376 | if (!larr) |
1377 | goto out; | |
1378 | darr = (const struct gfs2_dirent **)(larr + leaves); | |
1379 | g.pdent = darr; | |
1380 | g.offset = 0; | |
1381 | lfn = leaf_no; | |
b3b94faa | 1382 | |
71b86f56 SW |
1383 | do { |
1384 | error = get_leaf(ip, lfn, &bh); | |
b3b94faa | 1385 | if (error) |
d2a97a4e | 1386 | goto out_free; |
71b86f56 SW |
1387 | lf = (struct gfs2_leaf *)bh->b_data; |
1388 | lfn = be64_to_cpu(lf->lf_next); | |
1389 | if (lf->lf_entries) { | |
bdd19a22 | 1390 | entries2 += be16_to_cpu(lf->lf_entries); |
71b86f56 SW |
1391 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
1392 | gfs2_dirent_gather, NULL, &g); | |
1393 | error = PTR_ERR(dent); | |
bdd19a22 | 1394 | if (IS_ERR(dent)) |
d2a97a4e | 1395 | goto out_free; |
bdd19a22 | 1396 | if (entries2 != g.offset) { |
f391a4ea AM |
1397 | fs_warn(sdp, "Number of entries corrupt in dir " |
1398 | "leaf %llu, entries2 (%u) != " | |
1399 | "g.offset (%u)\n", | |
1400 | (unsigned long long)bh->b_blocknr, | |
1401 | entries2, g.offset); | |
bdd19a22 SW |
1402 | |
1403 | error = -EIO; | |
d2a97a4e | 1404 | goto out_free; |
71b86f56 SW |
1405 | } |
1406 | error = 0; | |
1407 | larr[leaf++] = bh; | |
b3b94faa | 1408 | } else { |
71b86f56 | 1409 | brelse(bh); |
b3b94faa | 1410 | } |
71b86f56 | 1411 | } while(lfn); |
b3b94faa | 1412 | |
bdd19a22 | 1413 | BUG_ON(entries2 != entries); |
d81a8ef5 | 1414 | error = do_filldir_main(ip, ctx, darr, entries, copied); |
d2a97a4e | 1415 | out_free: |
71b86f56 SW |
1416 | for(i = 0; i < leaf; i++) |
1417 | brelse(larr[i]); | |
3cdcf63e | 1418 | kvfree(larr); |
71b86f56 | 1419 | out: |
b3b94faa DT |
1420 | return error; |
1421 | } | |
1422 | ||
79c4c379 SW |
1423 | /** |
1424 | * gfs2_dir_readahead - Issue read-ahead requests for leaf blocks. | |
dfe4d34b BP |
1425 | * |
1426 | * Note: we can't calculate each index like dir_e_read can because we don't | |
1427 | * have the leaf, and therefore we don't have the depth, and therefore we | |
1428 | * don't have the length. So we have to just read enough ahead to make up | |
79c4c379 SW |
1429 | * for the loss of information. |
1430 | */ | |
dfe4d34b BP |
1431 | static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index, |
1432 | struct file_ra_state *f_ra) | |
1433 | { | |
1434 | struct gfs2_inode *ip = GFS2_I(inode); | |
1435 | struct gfs2_glock *gl = ip->i_gl; | |
1436 | struct buffer_head *bh; | |
1437 | u64 blocknr = 0, last; | |
1438 | unsigned count; | |
1439 | ||
1440 | /* First check if we've already read-ahead for the whole range. */ | |
79c4c379 | 1441 | if (index + MAX_RA_BLOCKS < f_ra->start) |
dfe4d34b BP |
1442 | return; |
1443 | ||
1444 | f_ra->start = max((pgoff_t)index, f_ra->start); | |
1445 | for (count = 0; count < MAX_RA_BLOCKS; count++) { | |
1446 | if (f_ra->start >= hsize) /* if exceeded the hash table */ | |
1447 | break; | |
1448 | ||
1449 | last = blocknr; | |
1450 | blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]); | |
1451 | f_ra->start++; | |
1452 | if (blocknr == last) | |
1453 | continue; | |
1454 | ||
1455 | bh = gfs2_getbuf(gl, blocknr, 1); | |
1456 | if (trylock_buffer(bh)) { | |
1457 | if (buffer_uptodate(bh)) { | |
1458 | unlock_buffer(bh); | |
1459 | brelse(bh); | |
1460 | continue; | |
1461 | } | |
1462 | bh->b_end_io = end_buffer_read_sync; | |
1463 | submit_bh(READA | REQ_META, bh); | |
1464 | continue; | |
1465 | } | |
1466 | brelse(bh); | |
1467 | } | |
1468 | } | |
17d539f0 | 1469 | |
b3b94faa | 1470 | /** |
c752666c SW |
1471 | * dir_e_read - Reads the entries from a directory into a filldir buffer |
1472 | * @dip: dinode pointer | |
d81a8ef5 | 1473 | * @ctx: actor to feed the entries to |
b3b94faa | 1474 | * |
c752666c | 1475 | * Returns: errno |
b3b94faa DT |
1476 | */ |
1477 | ||
d81a8ef5 AV |
1478 | static int dir_e_read(struct inode *inode, struct dir_context *ctx, |
1479 | struct file_ra_state *f_ra) | |
b3b94faa | 1480 | { |
feaa7bba | 1481 | struct gfs2_inode *dip = GFS2_I(inode); |
cd915493 | 1482 | u32 hsize, len = 0; |
cd915493 | 1483 | u32 hash, index; |
b44b84d7 | 1484 | __be64 *lp; |
c752666c SW |
1485 | int copied = 0; |
1486 | int error = 0; | |
4da3c646 | 1487 | unsigned depth = 0; |
b3b94faa | 1488 | |
9a004508 | 1489 | hsize = 1 << dip->i_depth; |
d81a8ef5 | 1490 | hash = gfs2_dir_offset2hash(ctx->pos); |
9a004508 | 1491 | index = hash >> (32 - dip->i_depth); |
b3b94faa | 1492 | |
79c4c379 | 1493 | if (dip->i_hash_cache == NULL) |
dfe4d34b | 1494 | f_ra->start = 0; |
17d539f0 SW |
1495 | lp = gfs2_dir_get_hash_table(dip); |
1496 | if (IS_ERR(lp)) | |
1497 | return PTR_ERR(lp); | |
b3b94faa | 1498 | |
dfe4d34b BP |
1499 | gfs2_dir_readahead(inode, hsize, index, f_ra); |
1500 | ||
b3b94faa | 1501 | while (index < hsize) { |
d81a8ef5 | 1502 | error = gfs2_dir_read_leaf(inode, ctx, |
71b86f56 | 1503 | &copied, &depth, |
17d539f0 | 1504 | be64_to_cpu(lp[index])); |
b3b94faa | 1505 | if (error) |
71b86f56 | 1506 | break; |
b3b94faa | 1507 | |
9a004508 | 1508 | len = 1 << (dip->i_depth - depth); |
b3b94faa DT |
1509 | index = (index & ~(len - 1)) + len; |
1510 | } | |
1511 | ||
71b86f56 SW |
1512 | if (error > 0) |
1513 | error = 0; | |
b3b94faa DT |
1514 | return error; |
1515 | } | |
1516 | ||
d81a8ef5 AV |
1517 | int gfs2_dir_read(struct inode *inode, struct dir_context *ctx, |
1518 | struct file_ra_state *f_ra) | |
b3b94faa | 1519 | { |
feaa7bba | 1520 | struct gfs2_inode *dip = GFS2_I(inode); |
bdd19a22 | 1521 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
71b86f56 SW |
1522 | struct dirent_gather g; |
1523 | const struct gfs2_dirent **darr, *dent; | |
b3b94faa DT |
1524 | struct buffer_head *dibh; |
1525 | int copied = 0; | |
1526 | int error; | |
1527 | ||
ad6203f2 | 1528 | if (!dip->i_entries) |
71b86f56 SW |
1529 | return 0; |
1530 | ||
383f01fb | 1531 | if (dip->i_diskflags & GFS2_DIF_EXHASH) |
d81a8ef5 | 1532 | return dir_e_read(inode, ctx, f_ra); |
71b86f56 | 1533 | |
b3b94faa DT |
1534 | if (!gfs2_is_stuffed(dip)) { |
1535 | gfs2_consist_inode(dip); | |
1536 | return -EIO; | |
1537 | } | |
1538 | ||
b3b94faa DT |
1539 | error = gfs2_meta_inode_buffer(dip, &dibh); |
1540 | if (error) | |
1541 | return error; | |
1542 | ||
71b86f56 | 1543 | error = -ENOMEM; |
bdd19a22 | 1544 | /* 96 is max number of dirents which can be stuffed into an inode */ |
16c5f06f | 1545 | darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS); |
71b86f56 SW |
1546 | if (darr) { |
1547 | g.pdent = darr; | |
1548 | g.offset = 0; | |
1549 | dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size, | |
1550 | gfs2_dirent_gather, NULL, &g); | |
1551 | if (IS_ERR(dent)) { | |
1552 | error = PTR_ERR(dent); | |
1553 | goto out; | |
1554 | } | |
ad6203f2 | 1555 | if (dip->i_entries != g.offset) { |
bdd19a22 | 1556 | fs_warn(sdp, "Number of entries corrupt in dir %llu, " |
ad6203f2 | 1557 | "ip->i_entries (%u) != g.offset (%u)\n", |
dbb7cae2 | 1558 | (unsigned long long)dip->i_no_addr, |
ad6203f2 | 1559 | dip->i_entries, |
bdd19a22 SW |
1560 | g.offset); |
1561 | error = -EIO; | |
1562 | goto out; | |
1563 | } | |
d81a8ef5 | 1564 | error = do_filldir_main(dip, ctx, darr, |
ad6203f2 | 1565 | dip->i_entries, &copied); |
71b86f56 SW |
1566 | out: |
1567 | kfree(darr); | |
1568 | } | |
1569 | ||
b3b94faa DT |
1570 | if (error > 0) |
1571 | error = 0; | |
1572 | ||
1573 | brelse(dibh); | |
1574 | ||
1575 | return error; | |
1576 | } | |
1577 | ||
b3b94faa DT |
1578 | /** |
1579 | * gfs2_dir_search - Search a directory | |
5a00f3cc SW |
1580 | * @dip: The GFS2 dir inode |
1581 | * @name: The name we are looking up | |
1582 | * @fail_on_exist: Fail if the name exists rather than looking it up | |
b3b94faa DT |
1583 | * |
1584 | * This routine searches a directory for a file or another directory. | |
1585 | * Assumes a glock is held on dip. | |
1586 | * | |
1587 | * Returns: errno | |
1588 | */ | |
1589 | ||
5a00f3cc SW |
1590 | struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name, |
1591 | bool fail_on_exist) | |
b3b94faa | 1592 | { |
c752666c SW |
1593 | struct buffer_head *bh; |
1594 | struct gfs2_dirent *dent; | |
5a00f3cc SW |
1595 | u64 addr, formal_ino; |
1596 | u16 dtype; | |
dbb7cae2 SW |
1597 | |
1598 | dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh); | |
1599 | if (dent) { | |
c8d57703 AG |
1600 | struct inode *inode; |
1601 | u16 rahead; | |
1602 | ||
dbb7cae2 | 1603 | if (IS_ERR(dent)) |
e231c2ee | 1604 | return ERR_CAST(dent); |
5a00f3cc | 1605 | dtype = be16_to_cpu(dent->de_type); |
c8d57703 | 1606 | rahead = be16_to_cpu(dent->de_rahead); |
5a00f3cc SW |
1607 | addr = be64_to_cpu(dent->de_inum.no_addr); |
1608 | formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino); | |
dbb7cae2 | 1609 | brelse(bh); |
5a00f3cc SW |
1610 | if (fail_on_exist) |
1611 | return ERR_PTR(-EEXIST); | |
c8d57703 AG |
1612 | inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, 0); |
1613 | if (!IS_ERR(inode)) | |
1614 | GFS2_I(inode)->i_rahead = rahead; | |
1615 | return inode; | |
dbb7cae2 SW |
1616 | } |
1617 | return ERR_PTR(-ENOENT); | |
1618 | } | |
1619 | ||
1620 | int gfs2_dir_check(struct inode *dir, const struct qstr *name, | |
1621 | const struct gfs2_inode *ip) | |
1622 | { | |
1623 | struct buffer_head *bh; | |
1624 | struct gfs2_dirent *dent; | |
1625 | int ret = -ENOENT; | |
c752666c SW |
1626 | |
1627 | dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh); | |
1628 | if (dent) { | |
1629 | if (IS_ERR(dent)) | |
1630 | return PTR_ERR(dent); | |
dbb7cae2 SW |
1631 | if (ip) { |
1632 | if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr) | |
1633 | goto out; | |
1634 | if (be64_to_cpu(dent->de_inum.no_formal_ino) != | |
1635 | ip->i_no_formal_ino) | |
1636 | goto out; | |
1637 | if (unlikely(IF2DT(ip->i_inode.i_mode) != | |
1638 | be16_to_cpu(dent->de_type))) { | |
1639 | gfs2_consist_inode(GFS2_I(dir)); | |
1640 | ret = -EIO; | |
1641 | goto out; | |
1642 | } | |
1643 | } | |
1644 | ret = 0; | |
1645 | out: | |
c752666c | 1646 | brelse(bh); |
c752666c | 1647 | } |
dbb7cae2 | 1648 | return ret; |
c752666c SW |
1649 | } |
1650 | ||
01bcb0de SW |
1651 | /** |
1652 | * dir_new_leaf - Add a new leaf onto hash chain | |
1653 | * @inode: The directory | |
1654 | * @name: The name we are adding | |
1655 | * | |
1656 | * This adds a new dir leaf onto an existing leaf when there is not | |
1657 | * enough space to add a new dir entry. This is a last resort after | |
1658 | * we've expanded the hash table to max size and also split existing | |
1659 | * leaf blocks, so it will only occur for very large directories. | |
1660 | * | |
1661 | * The dist parameter is set to 1 for leaf blocks directly attached | |
1662 | * to the hash table, 2 for one layer of indirection, 3 for two layers | |
1663 | * etc. We are thus able to tell the difference between an old leaf | |
1664 | * with dist set to zero (i.e. "don't know") and a new one where we | |
1665 | * set this information for debug/fsck purposes. | |
1666 | * | |
1667 | * Returns: 0 on success, or -ve on error | |
1668 | */ | |
1669 | ||
c752666c SW |
1670 | static int dir_new_leaf(struct inode *inode, const struct qstr *name) |
1671 | { | |
1672 | struct buffer_head *bh, *obh; | |
feaa7bba | 1673 | struct gfs2_inode *ip = GFS2_I(inode); |
c752666c | 1674 | struct gfs2_leaf *leaf, *oleaf; |
01bcb0de | 1675 | u32 dist = 1; |
b3b94faa | 1676 | int error; |
c752666c SW |
1677 | u32 index; |
1678 | u64 bn; | |
b3b94faa | 1679 | |
9a004508 | 1680 | index = name->hash >> (32 - ip->i_depth); |
c752666c SW |
1681 | error = get_first_leaf(ip, index, &obh); |
1682 | if (error) | |
1683 | return error; | |
1684 | do { | |
01bcb0de | 1685 | dist++; |
c752666c SW |
1686 | oleaf = (struct gfs2_leaf *)obh->b_data; |
1687 | bn = be64_to_cpu(oleaf->lf_next); | |
1688 | if (!bn) | |
1689 | break; | |
1690 | brelse(obh); | |
1691 | error = get_leaf(ip, bn, &obh); | |
1692 | if (error) | |
1693 | return error; | |
1694 | } while(1); | |
b3b94faa | 1695 | |
350a9b0a | 1696 | gfs2_trans_add_meta(ip->i_gl, obh); |
c752666c SW |
1697 | |
1698 | leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth)); | |
1699 | if (!leaf) { | |
1700 | brelse(obh); | |
1701 | return -ENOSPC; | |
1702 | } | |
01bcb0de | 1703 | leaf->lf_dist = cpu_to_be32(dist); |
4d8012b6 | 1704 | oleaf->lf_next = cpu_to_be64(bh->b_blocknr); |
c752666c SW |
1705 | brelse(bh); |
1706 | brelse(obh); | |
1707 | ||
1708 | error = gfs2_meta_inode_buffer(ip, &bh); | |
1709 | if (error) | |
1710 | return error; | |
350a9b0a | 1711 | gfs2_trans_add_meta(ip->i_gl, bh); |
77658aad | 1712 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
539e5d6b | 1713 | gfs2_dinode_out(ip, bh->b_data); |
c752666c SW |
1714 | brelse(bh); |
1715 | return 0; | |
b3b94faa DT |
1716 | } |
1717 | ||
44aaada9 SW |
1718 | static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip) |
1719 | { | |
1720 | u64 where = ip->i_no_addr + 1; | |
1721 | if (ip->i_eattr == where) | |
1722 | return 1; | |
1723 | return 0; | |
1724 | } | |
1725 | ||
b3b94faa DT |
1726 | /** |
1727 | * gfs2_dir_add - Add new filename into directory | |
2b47dad8 SW |
1728 | * @inode: The directory inode |
1729 | * @name: The new name | |
1730 | * @nip: The GFS2 inode to be linked in to the directory | |
1731 | * @da: The directory addition info | |
1732 | * | |
1733 | * If the call to gfs2_diradd_alloc_required resulted in there being | |
1734 | * no need to allocate any new directory blocks, then it will contain | |
1735 | * a pointer to the directory entry and the bh in which it resides. We | |
1736 | * can use that without having to repeat the search. If there was no | |
1737 | * free space, then we must now create more space. | |
b3b94faa DT |
1738 | * |
1739 | * Returns: 0 on success, error code on failure | |
1740 | */ | |
1741 | ||
c752666c | 1742 | int gfs2_dir_add(struct inode *inode, const struct qstr *name, |
2b47dad8 | 1743 | const struct gfs2_inode *nip, struct gfs2_diradd *da) |
b3b94faa | 1744 | { |
feaa7bba | 1745 | struct gfs2_inode *ip = GFS2_I(inode); |
2b47dad8 SW |
1746 | struct buffer_head *bh = da->bh; |
1747 | struct gfs2_dirent *dent = da->dent; | |
01bcb0de | 1748 | struct timespec tv; |
c752666c | 1749 | struct gfs2_leaf *leaf; |
b3b94faa DT |
1750 | int error; |
1751 | ||
c752666c | 1752 | while(1) { |
2b47dad8 SW |
1753 | if (da->bh == NULL) { |
1754 | dent = gfs2_dirent_search(inode, name, | |
1755 | gfs2_dirent_find_space, &bh); | |
1756 | } | |
c752666c SW |
1757 | if (dent) { |
1758 | if (IS_ERR(dent)) | |
1759 | return PTR_ERR(dent); | |
1760 | dent = gfs2_init_dirent(inode, dent, name, bh); | |
dbb7cae2 | 1761 | gfs2_inum_out(nip, dent); |
3d6ecb7d | 1762 | dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode)); |
44aaada9 | 1763 | dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip)); |
01bcb0de | 1764 | tv = CURRENT_TIME; |
383f01fb | 1765 | if (ip->i_diskflags & GFS2_DIF_EXHASH) { |
c752666c | 1766 | leaf = (struct gfs2_leaf *)bh->b_data; |
bb16b342 | 1767 | be16_add_cpu(&leaf->lf_entries, 1); |
01bcb0de SW |
1768 | leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); |
1769 | leaf->lf_sec = cpu_to_be64(tv.tv_sec); | |
c752666c | 1770 | } |
2b47dad8 SW |
1771 | da->dent = NULL; |
1772 | da->bh = NULL; | |
c752666c | 1773 | brelse(bh); |
ad6203f2 | 1774 | ip->i_entries++; |
01bcb0de | 1775 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = tv; |
3d6ecb7d SW |
1776 | if (S_ISDIR(nip->i_inode.i_mode)) |
1777 | inc_nlink(&ip->i_inode); | |
343cd8f0 | 1778 | mark_inode_dirty(inode); |
c752666c SW |
1779 | error = 0; |
1780 | break; | |
1781 | } | |
383f01fb | 1782 | if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) { |
c752666c SW |
1783 | error = dir_make_exhash(inode); |
1784 | if (error) | |
1785 | break; | |
1786 | continue; | |
1787 | } | |
1788 | error = dir_split_leaf(inode, name); | |
1789 | if (error == 0) | |
1790 | continue; | |
e90deff5 | 1791 | if (error < 0) |
c752666c | 1792 | break; |
9a004508 | 1793 | if (ip->i_depth < GFS2_DIR_MAX_DEPTH) { |
c752666c SW |
1794 | error = dir_double_exhash(ip); |
1795 | if (error) | |
1796 | break; | |
1797 | error = dir_split_leaf(inode, name); | |
e90deff5 | 1798 | if (error < 0) |
c752666c | 1799 | break; |
e90deff5 SW |
1800 | if (error == 0) |
1801 | continue; | |
c752666c SW |
1802 | } |
1803 | error = dir_new_leaf(inode, name); | |
1804 | if (!error) | |
1805 | continue; | |
1806 | error = -ENOSPC; | |
1807 | break; | |
1808 | } | |
b3b94faa DT |
1809 | return error; |
1810 | } | |
1811 | ||
c752666c | 1812 | |
b3b94faa DT |
1813 | /** |
1814 | * gfs2_dir_del - Delete a directory entry | |
1815 | * @dip: The GFS2 inode | |
1816 | * @filename: The filename | |
1817 | * | |
1818 | * Returns: 0 on success, error code on failure | |
1819 | */ | |
1820 | ||
855d23ce | 1821 | int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry) |
b3b94faa | 1822 | { |
855d23ce | 1823 | const struct qstr *name = &dentry->d_name; |
c752666c SW |
1824 | struct gfs2_dirent *dent, *prev = NULL; |
1825 | struct buffer_head *bh; | |
01bcb0de | 1826 | struct timespec tv = CURRENT_TIME; |
b3b94faa | 1827 | |
c752666c SW |
1828 | /* Returns _either_ the entry (if its first in block) or the |
1829 | previous entry otherwise */ | |
feaa7bba | 1830 | dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh); |
c752666c SW |
1831 | if (!dent) { |
1832 | gfs2_consist_inode(dip); | |
1833 | return -EIO; | |
1834 | } | |
1835 | if (IS_ERR(dent)) { | |
1836 | gfs2_consist_inode(dip); | |
1837 | return PTR_ERR(dent); | |
1838 | } | |
1839 | /* If not first in block, adjust pointers accordingly */ | |
71b86f56 | 1840 | if (gfs2_dirent_find(dent, name, NULL) == 0) { |
c752666c SW |
1841 | prev = dent; |
1842 | dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len)); | |
1843 | } | |
1844 | ||
1845 | dirent_del(dip, bh, prev, dent); | |
383f01fb | 1846 | if (dip->i_diskflags & GFS2_DIF_EXHASH) { |
c752666c SW |
1847 | struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data; |
1848 | u16 entries = be16_to_cpu(leaf->lf_entries); | |
1849 | if (!entries) | |
1850 | gfs2_consist_inode(dip); | |
1851 | leaf->lf_entries = cpu_to_be16(--entries); | |
01bcb0de SW |
1852 | leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); |
1853 | leaf->lf_sec = cpu_to_be64(tv.tv_sec); | |
c752666c | 1854 | } |
ed386507 | 1855 | brelse(bh); |
c752666c | 1856 | |
ad6203f2 | 1857 | if (!dip->i_entries) |
c752666c | 1858 | gfs2_consist_inode(dip); |
ad6203f2 | 1859 | dip->i_entries--; |
01bcb0de | 1860 | dip->i_inode.i_mtime = dip->i_inode.i_ctime = tv; |
e36cb0b8 | 1861 | if (d_is_dir(dentry)) |
855d23ce | 1862 | drop_nlink(&dip->i_inode); |
feaa7bba | 1863 | mark_inode_dirty(&dip->i_inode); |
b3b94faa | 1864 | |
ab9bbda0 | 1865 | return 0; |
b3b94faa DT |
1866 | } |
1867 | ||
b3b94faa DT |
1868 | /** |
1869 | * gfs2_dir_mvino - Change inode number of directory entry | |
1870 | * @dip: The GFS2 inode | |
1871 | * @filename: | |
1872 | * @new_inode: | |
1873 | * | |
1874 | * This routine changes the inode number of a directory entry. It's used | |
1875 | * by rename to change ".." when a directory is moved. | |
1876 | * Assumes a glock is held on dvp. | |
1877 | * | |
1878 | * Returns: errno | |
1879 | */ | |
1880 | ||
c752666c | 1881 | int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename, |
dbb7cae2 | 1882 | const struct gfs2_inode *nip, unsigned int new_type) |
b3b94faa | 1883 | { |
c752666c SW |
1884 | struct buffer_head *bh; |
1885 | struct gfs2_dirent *dent; | |
b3b94faa DT |
1886 | int error; |
1887 | ||
feaa7bba | 1888 | dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh); |
c752666c SW |
1889 | if (!dent) { |
1890 | gfs2_consist_inode(dip); | |
1891 | return -EIO; | |
1892 | } | |
1893 | if (IS_ERR(dent)) | |
1894 | return PTR_ERR(dent); | |
b3b94faa | 1895 | |
350a9b0a | 1896 | gfs2_trans_add_meta(dip->i_gl, bh); |
dbb7cae2 | 1897 | gfs2_inum_out(nip, dent); |
c752666c SW |
1898 | dent->de_type = cpu_to_be16(new_type); |
1899 | ||
383f01fb | 1900 | if (dip->i_diskflags & GFS2_DIF_EXHASH) { |
c752666c SW |
1901 | brelse(bh); |
1902 | error = gfs2_meta_inode_buffer(dip, &bh); | |
1903 | if (error) | |
1904 | return error; | |
350a9b0a | 1905 | gfs2_trans_add_meta(dip->i_gl, bh); |
c752666c SW |
1906 | } |
1907 | ||
4bd91ba1 | 1908 | dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME; |
539e5d6b | 1909 | gfs2_dinode_out(dip, bh->b_data); |
c752666c SW |
1910 | brelse(bh); |
1911 | return 0; | |
b3b94faa DT |
1912 | } |
1913 | ||
b3b94faa DT |
1914 | /** |
1915 | * leaf_dealloc - Deallocate a directory leaf | |
1916 | * @dip: the directory | |
1917 | * @index: the hash table offset in the directory | |
1918 | * @len: the number of pointers to this leaf | |
1919 | * @leaf_no: the leaf number | |
ec038c82 | 1920 | * @leaf_bh: buffer_head for the starting leaf |
d24a7a43 | 1921 | * last_dealloc: 1 if this is the final dealloc for the leaf, else 0 |
b3b94faa DT |
1922 | * |
1923 | * Returns: errno | |
1924 | */ | |
1925 | ||
cd915493 | 1926 | static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len, |
ec038c82 BP |
1927 | u64 leaf_no, struct buffer_head *leaf_bh, |
1928 | int last_dealloc) | |
b3b94faa | 1929 | { |
feaa7bba | 1930 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
c752666c | 1931 | struct gfs2_leaf *tmp_leaf; |
b3b94faa DT |
1932 | struct gfs2_rgrp_list rlist; |
1933 | struct buffer_head *bh, *dibh; | |
cd915493 | 1934 | u64 blk, nblk; |
b3b94faa DT |
1935 | unsigned int rg_blocks = 0, l_blocks = 0; |
1936 | char *ht; | |
cd915493 | 1937 | unsigned int x, size = len * sizeof(u64); |
b3b94faa DT |
1938 | int error; |
1939 | ||
5e2f7d61 BP |
1940 | error = gfs2_rindex_update(sdp); |
1941 | if (error) | |
1942 | return error; | |
1943 | ||
b3b94faa DT |
1944 | memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); |
1945 | ||
8be04b93 | 1946 | ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN); |
e8830d88 | 1947 | if (ht == NULL) |
7456a37d OD |
1948 | ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO, |
1949 | PAGE_KERNEL); | |
b3b94faa DT |
1950 | if (!ht) |
1951 | return -ENOMEM; | |
1952 | ||
f4108a60 | 1953 | error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); |
b3b94faa | 1954 | if (error) |
5407e242 | 1955 | goto out; |
b3b94faa | 1956 | |
b3b94faa | 1957 | /* Count the number of leaves */ |
ec038c82 | 1958 | bh = leaf_bh; |
b3b94faa | 1959 | |
c752666c | 1960 | for (blk = leaf_no; blk; blk = nblk) { |
ec038c82 BP |
1961 | if (blk != leaf_no) { |
1962 | error = get_leaf(dip, blk, &bh); | |
1963 | if (error) | |
1964 | goto out_rlist; | |
1965 | } | |
c752666c SW |
1966 | tmp_leaf = (struct gfs2_leaf *)bh->b_data; |
1967 | nblk = be64_to_cpu(tmp_leaf->lf_next); | |
ec038c82 BP |
1968 | if (blk != leaf_no) |
1969 | brelse(bh); | |
b3b94faa | 1970 | |
70b0c365 | 1971 | gfs2_rlist_add(dip, &rlist, blk); |
b3b94faa DT |
1972 | l_blocks++; |
1973 | } | |
1974 | ||
fe6c991c | 1975 | gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE); |
b3b94faa DT |
1976 | |
1977 | for (x = 0; x < rlist.rl_rgrps; x++) { | |
1978 | struct gfs2_rgrpd *rgd; | |
5c676f6d | 1979 | rgd = rlist.rl_ghs[x].gh_gl->gl_object; |
bb8d8a6f | 1980 | rg_blocks += rgd->rd_length; |
b3b94faa DT |
1981 | } |
1982 | ||
1983 | error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs); | |
1984 | if (error) | |
1985 | goto out_rlist; | |
1986 | ||
1987 | error = gfs2_trans_begin(sdp, | |
5c676f6d | 1988 | rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) + |
b3b94faa DT |
1989 | RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks); |
1990 | if (error) | |
1991 | goto out_rg_gunlock; | |
1992 | ||
ec038c82 BP |
1993 | bh = leaf_bh; |
1994 | ||
c752666c | 1995 | for (blk = leaf_no; blk; blk = nblk) { |
ec038c82 BP |
1996 | if (blk != leaf_no) { |
1997 | error = get_leaf(dip, blk, &bh); | |
1998 | if (error) | |
1999 | goto out_end_trans; | |
2000 | } | |
c752666c SW |
2001 | tmp_leaf = (struct gfs2_leaf *)bh->b_data; |
2002 | nblk = be64_to_cpu(tmp_leaf->lf_next); | |
ec038c82 BP |
2003 | if (blk != leaf_no) |
2004 | brelse(bh); | |
b3b94faa DT |
2005 | |
2006 | gfs2_free_meta(dip, blk, 1); | |
77658aad | 2007 | gfs2_add_inode_blocks(&dip->i_inode, -1); |
b3b94faa DT |
2008 | } |
2009 | ||
cd915493 | 2010 | error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size); |
b3b94faa DT |
2011 | if (error != size) { |
2012 | if (error >= 0) | |
2013 | error = -EIO; | |
2014 | goto out_end_trans; | |
2015 | } | |
2016 | ||
2017 | error = gfs2_meta_inode_buffer(dip, &dibh); | |
2018 | if (error) | |
2019 | goto out_end_trans; | |
2020 | ||
350a9b0a | 2021 | gfs2_trans_add_meta(dip->i_gl, dibh); |
d24a7a43 BP |
2022 | /* On the last dealloc, make this a regular file in case we crash. |
2023 | (We don't want to free these blocks a second time.) */ | |
2024 | if (last_dealloc) | |
2025 | dip->i_inode.i_mode = S_IFREG; | |
539e5d6b | 2026 | gfs2_dinode_out(dip, dibh->b_data); |
b3b94faa DT |
2027 | brelse(dibh); |
2028 | ||
a91ea69f | 2029 | out_end_trans: |
b3b94faa | 2030 | gfs2_trans_end(sdp); |
a91ea69f | 2031 | out_rg_gunlock: |
b3b94faa | 2032 | gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); |
a91ea69f | 2033 | out_rlist: |
b3b94faa | 2034 | gfs2_rlist_free(&rlist); |
b3b94faa | 2035 | gfs2_quota_unhold(dip); |
182fe5ab | 2036 | out: |
3cdcf63e | 2037 | kvfree(ht); |
b3b94faa DT |
2038 | return error; |
2039 | } | |
2040 | ||
2041 | /** | |
2042 | * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory | |
2043 | * @dip: the directory | |
2044 | * | |
2045 | * Dealloc all on-disk directory leaves to FREEMETA state | |
2046 | * Change on-disk inode type to "regular file" | |
2047 | * | |
2048 | * Returns: errno | |
2049 | */ | |
2050 | ||
2051 | int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip) | |
2052 | { | |
556bb179 BP |
2053 | struct buffer_head *bh; |
2054 | struct gfs2_leaf *leaf; | |
2055 | u32 hsize, len; | |
556bb179 BP |
2056 | u32 index = 0, next_index; |
2057 | __be64 *lp; | |
2058 | u64 leaf_no; | |
2059 | int error = 0, last; | |
2060 | ||
2061 | hsize = 1 << dip->i_depth; | |
556bb179 | 2062 | |
17d539f0 SW |
2063 | lp = gfs2_dir_get_hash_table(dip); |
2064 | if (IS_ERR(lp)) | |
2065 | return PTR_ERR(lp); | |
556bb179 BP |
2066 | |
2067 | while (index < hsize) { | |
17d539f0 | 2068 | leaf_no = be64_to_cpu(lp[index]); |
556bb179 BP |
2069 | if (leaf_no) { |
2070 | error = get_leaf(dip, leaf_no, &bh); | |
2071 | if (error) | |
2072 | goto out; | |
2073 | leaf = (struct gfs2_leaf *)bh->b_data; | |
2074 | len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth)); | |
2075 | ||
2076 | next_index = (index & ~(len - 1)) + len; | |
2077 | last = ((next_index >= hsize) ? 1 : 0); | |
2078 | error = leaf_dealloc(dip, index, len, leaf_no, bh, | |
2079 | last); | |
2080 | brelse(bh); | |
2081 | if (error) | |
2082 | goto out; | |
2083 | index = next_index; | |
2084 | } else | |
2085 | index++; | |
2086 | } | |
2087 | ||
2088 | if (index != hsize) { | |
2089 | gfs2_consist_inode(dip); | |
2090 | error = -EIO; | |
2091 | } | |
2092 | ||
2093 | out: | |
556bb179 BP |
2094 | |
2095 | return error; | |
b3b94faa DT |
2096 | } |
2097 | ||
2098 | /** | |
2099 | * gfs2_diradd_alloc_required - find if adding entry will require an allocation | |
2100 | * @ip: the file being written to | |
2101 | * @filname: the filename that's going to be added | |
3c1c0ae1 | 2102 | * @da: The structure to return dir alloc info |
b3b94faa | 2103 | * |
3c1c0ae1 | 2104 | * Returns: 0 if ok, -ve on error |
b3b94faa DT |
2105 | */ |
2106 | ||
3c1c0ae1 SW |
2107 | int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name, |
2108 | struct gfs2_diradd *da) | |
b3b94faa | 2109 | { |
22b5a6c0 | 2110 | struct gfs2_inode *ip = GFS2_I(inode); |
3c1c0ae1 | 2111 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
22b5a6c0 | 2112 | const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf); |
c752666c SW |
2113 | struct gfs2_dirent *dent; |
2114 | struct buffer_head *bh; | |
b3b94faa | 2115 | |
3c1c0ae1 | 2116 | da->nr_blocks = 0; |
2b47dad8 SW |
2117 | da->bh = NULL; |
2118 | da->dent = NULL; | |
3c1c0ae1 | 2119 | |
c752666c | 2120 | dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh); |
ed386507 | 2121 | if (!dent) { |
3c1c0ae1 | 2122 | da->nr_blocks = sdp->sd_max_dirres; |
22b5a6c0 SW |
2123 | if (!(ip->i_diskflags & GFS2_DIF_EXHASH) && |
2124 | (GFS2_DIRENT_SIZE(name->len) < extra)) | |
2125 | da->nr_blocks = 1; | |
3c1c0ae1 | 2126 | return 0; |
ed386507 | 2127 | } |
c752666c SW |
2128 | if (IS_ERR(dent)) |
2129 | return PTR_ERR(dent); | |
19aeb5a6 BP |
2130 | |
2131 | if (da->save_loc) { | |
2132 | da->bh = bh; | |
2133 | da->dent = dent; | |
2134 | } else { | |
2135 | brelse(bh); | |
2136 | } | |
c752666c | 2137 | return 0; |
b3b94faa DT |
2138 | } |
2139 |