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 | * | |
39 | * dip->i_di.di_flags & GFS2_DIF_EXHASH is true | |
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 DT |
55 | |
56 | #include <linux/sched.h> | |
57 | #include <linux/slab.h> | |
58 | #include <linux/spinlock.h> | |
b3b94faa DT |
59 | #include <linux/buffer_head.h> |
60 | #include <linux/sort.h> | |
5c676f6d | 61 | #include <linux/gfs2_ondisk.h> |
71b86f56 | 62 | #include <linux/crc32.h> |
fe1bdedc | 63 | #include <linux/vmalloc.h> |
b3b94faa DT |
64 | |
65 | #include "gfs2.h" | |
5c676f6d SW |
66 | #include "lm_interface.h" |
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 | ||
b3b94faa DT |
81 | #define gfs2_disk_hash2offset(h) (((uint64_t)(h)) >> 1) |
82 | #define gfs2_dir_offset2hash(p) ((uint32_t)(((uint64_t)(p)) << 1)) | |
b3b94faa DT |
83 | |
84 | typedef int (*leaf_call_t) (struct gfs2_inode *dip, | |
85 | uint32_t index, uint32_t len, uint64_t leaf_no, | |
86 | void *data); | |
87 | ||
61e085a8 SW |
88 | |
89 | int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, uint64_t block, | |
90 | struct buffer_head **bhp) | |
e13940ba SW |
91 | { |
92 | struct buffer_head *bh; | |
e13940ba | 93 | |
61e085a8 SW |
94 | bh = gfs2_meta_new(ip->i_gl, block); |
95 | gfs2_trans_add_bh(ip->i_gl, bh, 1); | |
96 | gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD); | |
97 | gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); | |
e13940ba SW |
98 | *bhp = bh; |
99 | return 0; | |
100 | } | |
101 | ||
61e085a8 SW |
102 | static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, uint64_t block, |
103 | struct buffer_head **bhp) | |
104 | { | |
105 | struct buffer_head *bh; | |
106 | int error; | |
e13940ba | 107 | |
61e085a8 SW |
108 | error = gfs2_meta_read(ip->i_gl, block, DIO_START | DIO_WAIT, &bh); |
109 | if (error) | |
110 | return error; | |
feaa7bba | 111 | if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) { |
61e085a8 SW |
112 | brelse(bh); |
113 | return -EIO; | |
114 | } | |
115 | *bhp = bh; | |
116 | return 0; | |
117 | } | |
e13940ba SW |
118 | |
119 | static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf, | |
120 | unsigned int offset, unsigned int size) | |
121 | ||
122 | { | |
123 | struct buffer_head *dibh; | |
124 | int error; | |
125 | ||
126 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
127 | if (error) | |
128 | return error; | |
129 | ||
130 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); | |
c752666c | 131 | memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size); |
e13940ba SW |
132 | if (ip->i_di.di_size < offset + size) |
133 | ip->i_di.di_size = offset + size; | |
134 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); | |
135 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
136 | ||
137 | brelse(dibh); | |
138 | ||
139 | return size; | |
140 | } | |
141 | ||
142 | ||
143 | ||
144 | /** | |
145 | * gfs2_dir_write_data - Write directory information to the inode | |
146 | * @ip: The GFS2 inode | |
147 | * @buf: The buffer containing information to be written | |
148 | * @offset: The file offset to start writing at | |
149 | * @size: The amount of data to write | |
150 | * | |
151 | * Returns: The number of bytes correctly written or error code | |
152 | */ | |
153 | static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf, | |
154 | uint64_t offset, unsigned int size) | |
155 | { | |
feaa7bba | 156 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
e13940ba SW |
157 | struct buffer_head *dibh; |
158 | uint64_t lblock, dblock; | |
159 | uint32_t extlen = 0; | |
160 | unsigned int o; | |
161 | int copied = 0; | |
162 | int error = 0; | |
163 | ||
164 | if (!size) | |
165 | return 0; | |
166 | ||
167 | if (gfs2_is_stuffed(ip) && | |
168 | offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) | |
568f4c96 SW |
169 | return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset, |
170 | size); | |
e13940ba SW |
171 | |
172 | if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) | |
173 | return -EINVAL; | |
174 | ||
175 | if (gfs2_is_stuffed(ip)) { | |
f25ef0c1 | 176 | error = gfs2_unstuff_dinode(ip, NULL); |
e13940ba | 177 | if (error) |
c752666c | 178 | return error; |
e13940ba SW |
179 | } |
180 | ||
181 | lblock = offset; | |
182 | o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); | |
183 | ||
184 | while (copied < size) { | |
185 | unsigned int amount; | |
186 | struct buffer_head *bh; | |
187 | int new; | |
188 | ||
189 | amount = size - copied; | |
190 | if (amount > sdp->sd_sb.sb_bsize - o) | |
191 | amount = sdp->sd_sb.sb_bsize - o; | |
192 | ||
193 | if (!extlen) { | |
194 | new = 1; | |
feaa7bba | 195 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, |
fd88de56 | 196 | &dblock, &extlen); |
e13940ba SW |
197 | if (error) |
198 | goto fail; | |
199 | error = -EIO; | |
200 | if (gfs2_assert_withdraw(sdp, dblock)) | |
201 | goto fail; | |
202 | } | |
203 | ||
61e085a8 SW |
204 | if (amount == sdp->sd_jbsize || new) |
205 | error = gfs2_dir_get_new_buffer(ip, dblock, &bh); | |
206 | else | |
207 | error = gfs2_dir_get_existing_buffer(ip, dblock, &bh); | |
208 | ||
e13940ba SW |
209 | if (error) |
210 | goto fail; | |
211 | ||
212 | gfs2_trans_add_bh(ip->i_gl, bh, 1); | |
213 | memcpy(bh->b_data + o, buf, amount); | |
214 | brelse(bh); | |
215 | if (error) | |
216 | goto fail; | |
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 | ||
232 | if (ip->i_di.di_size < offset + copied) | |
233 | ip->i_di.di_size = offset + copied; | |
234 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); | |
235 | ||
236 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); | |
237 | gfs2_dinode_out(&ip->i_di, dibh->b_data); | |
238 | brelse(dibh); | |
239 | ||
240 | return copied; | |
241 | fail: | |
242 | if (copied) | |
243 | goto out; | |
244 | return error; | |
245 | } | |
246 | ||
247 | static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf, | |
c752666c | 248 | unsigned int offset, 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) { | |
255 | offset += sizeof(struct gfs2_dinode); | |
256 | memcpy(buf, dibh->b_data + offset, size); | |
257 | brelse(dibh); | |
258 | } | |
259 | ||
260 | return (error) ? error : size; | |
261 | } | |
262 | ||
263 | ||
264 | /** | |
265 | * gfs2_dir_read_data - Read a data from a directory inode | |
266 | * @ip: The GFS2 Inode | |
267 | * @buf: The buffer to place result into | |
268 | * @offset: File offset to begin jdata_readng from | |
269 | * @size: Amount of data to transfer | |
270 | * | |
271 | * Returns: The amount of data actually copied or the error | |
272 | */ | |
273 | static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, | |
274 | uint64_t offset, unsigned int size) | |
275 | { | |
feaa7bba | 276 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
e13940ba SW |
277 | uint64_t lblock, dblock; |
278 | uint32_t extlen = 0; | |
279 | unsigned int o; | |
280 | int copied = 0; | |
281 | int error = 0; | |
282 | ||
283 | if (offset >= ip->i_di.di_size) | |
284 | return 0; | |
285 | ||
286 | if ((offset + size) > ip->i_di.di_size) | |
287 | size = ip->i_di.di_size - offset; | |
288 | ||
289 | if (!size) | |
290 | return 0; | |
291 | ||
292 | if (gfs2_is_stuffed(ip)) | |
568f4c96 SW |
293 | return gfs2_dir_read_stuffed(ip, buf, (unsigned int)offset, |
294 | size); | |
e13940ba SW |
295 | |
296 | if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) | |
297 | return -EINVAL; | |
298 | ||
299 | lblock = offset; | |
300 | o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); | |
301 | ||
302 | while (copied < size) { | |
303 | unsigned int amount; | |
304 | struct buffer_head *bh; | |
305 | int new; | |
306 | ||
307 | amount = size - copied; | |
308 | if (amount > sdp->sd_sb.sb_bsize - o) | |
309 | amount = sdp->sd_sb.sb_bsize - o; | |
310 | ||
311 | if (!extlen) { | |
312 | new = 0; | |
feaa7bba | 313 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, |
fd88de56 | 314 | &dblock, &extlen); |
e13940ba SW |
315 | if (error) |
316 | goto fail; | |
317 | } | |
318 | ||
319 | if (extlen > 1) | |
320 | gfs2_meta_ra(ip->i_gl, dblock, extlen); | |
321 | ||
322 | if (dblock) { | |
61e085a8 SW |
323 | if (new) |
324 | error = gfs2_dir_get_new_buffer(ip, dblock, &bh); | |
325 | else | |
326 | error = gfs2_dir_get_existing_buffer(ip, dblock, &bh); | |
e13940ba SW |
327 | if (error) |
328 | goto fail; | |
329 | dblock++; | |
330 | extlen--; | |
331 | } else | |
332 | bh = NULL; | |
333 | ||
334 | memcpy(buf, bh->b_data + o, amount); | |
335 | brelse(bh); | |
336 | if (error) | |
337 | goto fail; | |
338 | ||
899bb264 | 339 | buf += amount; |
e13940ba SW |
340 | copied += amount; |
341 | lblock++; | |
342 | ||
343 | o = sizeof(struct gfs2_meta_header); | |
344 | } | |
345 | ||
346 | return copied; | |
347 | fail: | |
348 | return (copied) ? copied : error; | |
349 | } | |
350 | ||
c752666c | 351 | typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent, |
71b86f56 SW |
352 | const struct qstr *name, |
353 | void *opaque); | |
c752666c SW |
354 | |
355 | static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent, | |
356 | const struct qstr *name, int ret) | |
357 | { | |
358 | if (dent->de_inum.no_addr != 0 && | |
359 | be32_to_cpu(dent->de_hash) == name->hash && | |
360 | be16_to_cpu(dent->de_name_len) == name->len && | |
361 | memcmp((char *)(dent+1), name->name, name->len) == 0) | |
362 | return ret; | |
363 | return 0; | |
364 | } | |
365 | ||
366 | static int gfs2_dirent_find(const struct gfs2_dirent *dent, | |
71b86f56 SW |
367 | const struct qstr *name, |
368 | void *opaque) | |
c752666c SW |
369 | { |
370 | return __gfs2_dirent_find(dent, name, 1); | |
371 | } | |
372 | ||
373 | static int gfs2_dirent_prev(const struct gfs2_dirent *dent, | |
71b86f56 SW |
374 | const struct qstr *name, |
375 | void *opaque) | |
c752666c SW |
376 | { |
377 | return __gfs2_dirent_find(dent, name, 2); | |
378 | } | |
379 | ||
380 | /* | |
381 | * name->name holds ptr to start of block. | |
382 | * name->len holds size of block. | |
b3b94faa | 383 | */ |
c752666c | 384 | static int gfs2_dirent_last(const struct gfs2_dirent *dent, |
71b86f56 SW |
385 | const struct qstr *name, |
386 | void *opaque) | |
c752666c SW |
387 | { |
388 | const char *start = name->name; | |
389 | const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len); | |
390 | if (name->len == (end - start)) | |
391 | return 1; | |
392 | return 0; | |
393 | } | |
b3b94faa | 394 | |
c752666c | 395 | static int gfs2_dirent_find_space(const struct gfs2_dirent *dent, |
71b86f56 SW |
396 | const struct qstr *name, |
397 | void *opaque) | |
b3b94faa | 398 | { |
c752666c SW |
399 | unsigned required = GFS2_DIRENT_SIZE(name->len); |
400 | unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); | |
401 | unsigned totlen = be16_to_cpu(dent->de_rec_len); | |
402 | ||
71b86f56 SW |
403 | if (!dent->de_inum.no_addr) |
404 | actual = GFS2_DIRENT_SIZE(0); | |
c752666c SW |
405 | if ((totlen - actual) >= required) |
406 | return 1; | |
407 | return 0; | |
408 | } | |
409 | ||
71b86f56 SW |
410 | struct dirent_gather { |
411 | const struct gfs2_dirent **pdent; | |
412 | unsigned offset; | |
413 | }; | |
414 | ||
415 | static int gfs2_dirent_gather(const struct gfs2_dirent *dent, | |
416 | const struct qstr *name, | |
417 | void *opaque) | |
418 | { | |
419 | struct dirent_gather *g = opaque; | |
420 | if (dent->de_inum.no_addr) { | |
421 | g->pdent[g->offset++] = dent; | |
422 | } | |
423 | return 0; | |
424 | } | |
425 | ||
c752666c SW |
426 | /* |
427 | * Other possible things to check: | |
428 | * - Inode located within filesystem size (and on valid block) | |
429 | * - Valid directory entry type | |
430 | * Not sure how heavy-weight we want to make this... could also check | |
431 | * hash is correct for example, but that would take a lot of extra time. | |
432 | * For now the most important thing is to check that the various sizes | |
433 | * are correct. | |
434 | */ | |
435 | static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset, | |
436 | unsigned int size, unsigned int len, int first) | |
437 | { | |
438 | const char *msg = "gfs2_dirent too small"; | |
439 | if (unlikely(size < sizeof(struct gfs2_dirent))) | |
440 | goto error; | |
441 | msg = "gfs2_dirent misaligned"; | |
442 | if (unlikely(offset & 0x7)) | |
443 | goto error; | |
444 | msg = "gfs2_dirent points beyond end of block"; | |
445 | if (unlikely(offset + size > len)) | |
446 | goto error; | |
447 | msg = "zero inode number"; | |
448 | if (unlikely(!first && !dent->de_inum.no_addr)) | |
449 | goto error; | |
450 | msg = "name length is greater than space in dirent"; | |
451 | if (dent->de_inum.no_addr && | |
452 | unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) > | |
453 | size)) | |
454 | goto error; | |
455 | return 0; | |
456 | error: | |
457 | printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg, | |
458 | first ? "first in block" : "not first in block"); | |
459 | return -EIO; | |
b3b94faa DT |
460 | } |
461 | ||
71b86f56 | 462 | static int gfs2_dirent_offset(const void *buf) |
c752666c | 463 | { |
71b86f56 SW |
464 | const struct gfs2_meta_header *h = buf; |
465 | int offset; | |
c752666c SW |
466 | |
467 | BUG_ON(buf == NULL); | |
c752666c | 468 | |
e3167ded | 469 | switch(be32_to_cpu(h->mh_type)) { |
c752666c SW |
470 | case GFS2_METATYPE_LF: |
471 | offset = sizeof(struct gfs2_leaf); | |
472 | break; | |
473 | case GFS2_METATYPE_DI: | |
474 | offset = sizeof(struct gfs2_dinode); | |
475 | break; | |
476 | default: | |
477 | goto wrong_type; | |
478 | } | |
71b86f56 SW |
479 | return offset; |
480 | wrong_type: | |
481 | printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n", | |
e3167ded | 482 | be32_to_cpu(h->mh_type)); |
71b86f56 SW |
483 | return -1; |
484 | } | |
485 | ||
486 | static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, | |
487 | void *buf, | |
488 | unsigned int len, gfs2_dscan_t scan, | |
489 | const struct qstr *name, | |
490 | void *opaque) | |
491 | { | |
492 | struct gfs2_dirent *dent, *prev; | |
493 | unsigned offset; | |
494 | unsigned size; | |
495 | int ret = 0; | |
c752666c | 496 | |
71b86f56 SW |
497 | ret = gfs2_dirent_offset(buf); |
498 | if (ret < 0) | |
499 | goto consist_inode; | |
500 | ||
501 | offset = ret; | |
c752666c SW |
502 | prev = NULL; |
503 | dent = (struct gfs2_dirent *)(buf + offset); | |
504 | size = be16_to_cpu(dent->de_rec_len); | |
505 | if (gfs2_check_dirent(dent, offset, size, len, 1)) | |
506 | goto consist_inode; | |
507 | do { | |
71b86f56 | 508 | ret = scan(dent, name, opaque); |
c752666c SW |
509 | if (ret) |
510 | break; | |
511 | offset += size; | |
512 | if (offset == len) | |
513 | break; | |
514 | prev = dent; | |
515 | dent = (struct gfs2_dirent *)(buf + offset); | |
516 | size = be16_to_cpu(dent->de_rec_len); | |
517 | if (gfs2_check_dirent(dent, offset, size, len, 0)) | |
518 | goto consist_inode; | |
519 | } while(1); | |
520 | ||
521 | switch(ret) { | |
522 | case 0: | |
523 | return NULL; | |
524 | case 1: | |
525 | return dent; | |
526 | case 2: | |
527 | return prev ? prev : dent; | |
528 | default: | |
529 | BUG_ON(ret > 0); | |
530 | return ERR_PTR(ret); | |
531 | } | |
532 | ||
c752666c | 533 | consist_inode: |
feaa7bba | 534 | gfs2_consist_inode(GFS2_I(inode)); |
c752666c SW |
535 | return ERR_PTR(-EIO); |
536 | } | |
537 | ||
538 | ||
b3b94faa DT |
539 | /** |
540 | * dirent_first - Return the first dirent | |
541 | * @dip: the directory | |
542 | * @bh: The buffer | |
543 | * @dent: Pointer to list of dirents | |
544 | * | |
545 | * return first dirent whether bh points to leaf or stuffed dinode | |
546 | * | |
547 | * Returns: IS_LEAF, IS_DINODE, or -errno | |
548 | */ | |
549 | ||
550 | static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh, | |
551 | struct gfs2_dirent **dent) | |
552 | { | |
553 | struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data; | |
554 | ||
e3167ded | 555 | if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) { |
feaa7bba | 556 | if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh)) |
b3b94faa DT |
557 | return -EIO; |
558 | *dent = (struct gfs2_dirent *)(bh->b_data + | |
559 | sizeof(struct gfs2_leaf)); | |
560 | return IS_LEAF; | |
561 | } else { | |
feaa7bba | 562 | if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI)) |
b3b94faa DT |
563 | return -EIO; |
564 | *dent = (struct gfs2_dirent *)(bh->b_data + | |
565 | sizeof(struct gfs2_dinode)); | |
566 | return IS_DINODE; | |
567 | } | |
568 | } | |
569 | ||
570 | /** | |
571 | * dirent_next - Next dirent | |
572 | * @dip: the directory | |
573 | * @bh: The buffer | |
574 | * @dent: Pointer to list of dirents | |
575 | * | |
576 | * Returns: 0 on success, error code otherwise | |
577 | */ | |
578 | ||
579 | static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh, | |
580 | struct gfs2_dirent **dent) | |
581 | { | |
582 | struct gfs2_dirent *tmp, *cur; | |
583 | char *bh_end; | |
fc69d0d3 | 584 | uint16_t cur_rec_len; |
b3b94faa DT |
585 | |
586 | cur = *dent; | |
587 | bh_end = bh->b_data + bh->b_size; | |
fc69d0d3 | 588 | cur_rec_len = be16_to_cpu(cur->de_rec_len); |
b3b94faa DT |
589 | |
590 | if ((char *)cur + cur_rec_len >= bh_end) { | |
591 | if ((char *)cur + cur_rec_len > bh_end) { | |
592 | gfs2_consist_inode(dip); | |
593 | return -EIO; | |
594 | } | |
595 | return -ENOENT; | |
596 | } | |
597 | ||
598 | tmp = (struct gfs2_dirent *)((char *)cur + cur_rec_len); | |
599 | ||
fc69d0d3 | 600 | if ((char *)tmp + be16_to_cpu(tmp->de_rec_len) > bh_end) { |
b3b94faa DT |
601 | gfs2_consist_inode(dip); |
602 | return -EIO; | |
603 | } | |
4dd651ad SW |
604 | |
605 | if (cur_rec_len == 0) { | |
606 | gfs2_consist_inode(dip); | |
607 | return -EIO; | |
608 | } | |
609 | ||
b3b94faa DT |
610 | /* Only the first dent could ever have de_inum.no_addr == 0 */ |
611 | if (!tmp->de_inum.no_addr) { | |
612 | gfs2_consist_inode(dip); | |
613 | return -EIO; | |
614 | } | |
615 | ||
616 | *dent = tmp; | |
617 | ||
618 | return 0; | |
619 | } | |
620 | ||
621 | /** | |
622 | * dirent_del - Delete a dirent | |
623 | * @dip: The GFS2 inode | |
624 | * @bh: The buffer | |
625 | * @prev: The previous dirent | |
626 | * @cur: The current dirent | |
627 | * | |
628 | */ | |
629 | ||
630 | static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh, | |
631 | struct gfs2_dirent *prev, struct gfs2_dirent *cur) | |
632 | { | |
fc69d0d3 | 633 | uint16_t cur_rec_len, prev_rec_len; |
b3b94faa DT |
634 | |
635 | if (!cur->de_inum.no_addr) { | |
636 | gfs2_consist_inode(dip); | |
637 | return; | |
638 | } | |
639 | ||
d4e9c4c3 | 640 | gfs2_trans_add_bh(dip->i_gl, bh, 1); |
b3b94faa DT |
641 | |
642 | /* If there is no prev entry, this is the first entry in the block. | |
643 | The de_rec_len is already as big as it needs to be. Just zero | |
644 | out the inode number and return. */ | |
645 | ||
646 | if (!prev) { | |
647 | cur->de_inum.no_addr = 0; /* No endianess worries */ | |
648 | return; | |
649 | } | |
650 | ||
651 | /* Combine this dentry with the previous one. */ | |
652 | ||
fc69d0d3 SW |
653 | prev_rec_len = be16_to_cpu(prev->de_rec_len); |
654 | cur_rec_len = be16_to_cpu(cur->de_rec_len); | |
b3b94faa DT |
655 | |
656 | if ((char *)prev + prev_rec_len != (char *)cur) | |
657 | gfs2_consist_inode(dip); | |
658 | if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size) | |
659 | gfs2_consist_inode(dip); | |
660 | ||
661 | prev_rec_len += cur_rec_len; | |
fc69d0d3 | 662 | prev->de_rec_len = cpu_to_be16(prev_rec_len); |
b3b94faa DT |
663 | } |
664 | ||
c752666c SW |
665 | /* |
666 | * Takes a dent from which to grab space as an argument. Returns the | |
667 | * newly created dent. | |
b3b94faa | 668 | */ |
08bc2dbc AB |
669 | static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode, |
670 | struct gfs2_dirent *dent, | |
671 | const struct qstr *name, | |
672 | struct buffer_head *bh) | |
b3b94faa | 673 | { |
feaa7bba | 674 | struct gfs2_inode *ip = GFS2_I(inode); |
c752666c SW |
675 | struct gfs2_dirent *ndent; |
676 | unsigned offset = 0, totlen; | |
677 | ||
678 | if (dent->de_inum.no_addr) | |
679 | offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); | |
680 | totlen = be16_to_cpu(dent->de_rec_len); | |
681 | BUG_ON(offset + name->len > totlen); | |
682 | gfs2_trans_add_bh(ip->i_gl, bh, 1); | |
683 | ndent = (struct gfs2_dirent *)((char *)dent + offset); | |
684 | dent->de_rec_len = cpu_to_be16(offset); | |
685 | gfs2_qstr2dirent(name, totlen - offset, ndent); | |
686 | return ndent; | |
b3b94faa DT |
687 | } |
688 | ||
c752666c SW |
689 | static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode, |
690 | struct buffer_head *bh, | |
691 | const struct qstr *name) | |
b3b94faa DT |
692 | { |
693 | struct gfs2_dirent *dent; | |
71b86f56 SW |
694 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
695 | gfs2_dirent_find_space, name, NULL); | |
c752666c SW |
696 | if (!dent || IS_ERR(dent)) |
697 | return dent; | |
698 | return gfs2_init_dirent(inode, dent, name, bh); | |
b3b94faa DT |
699 | } |
700 | ||
701 | static int get_leaf(struct gfs2_inode *dip, uint64_t leaf_no, | |
702 | struct buffer_head **bhp) | |
703 | { | |
704 | int error; | |
705 | ||
706 | error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_START | DIO_WAIT, bhp); | |
feaa7bba SW |
707 | if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) { |
708 | /* printk(KERN_INFO "block num=%llu\n", leaf_no); */ | |
b3b94faa | 709 | error = -EIO; |
feaa7bba | 710 | } |
b3b94faa DT |
711 | |
712 | return error; | |
713 | } | |
714 | ||
715 | /** | |
716 | * get_leaf_nr - Get a leaf number associated with the index | |
717 | * @dip: The GFS2 inode | |
718 | * @index: | |
719 | * @leaf_out: | |
720 | * | |
721 | * Returns: 0 on success, error code otherwise | |
722 | */ | |
723 | ||
724 | static int get_leaf_nr(struct gfs2_inode *dip, uint32_t index, | |
725 | uint64_t *leaf_out) | |
726 | { | |
727 | uint64_t leaf_no; | |
728 | int error; | |
729 | ||
e13940ba | 730 | error = gfs2_dir_read_data(dip, (char *)&leaf_no, |
b3b94faa DT |
731 | index * sizeof(uint64_t), |
732 | sizeof(uint64_t)); | |
733 | if (error != sizeof(uint64_t)) | |
734 | return (error < 0) ? error : -EIO; | |
735 | ||
736 | *leaf_out = be64_to_cpu(leaf_no); | |
737 | ||
738 | return 0; | |
739 | } | |
740 | ||
741 | static int get_first_leaf(struct gfs2_inode *dip, uint32_t index, | |
742 | struct buffer_head **bh_out) | |
743 | { | |
744 | uint64_t leaf_no; | |
745 | int error; | |
746 | ||
747 | error = get_leaf_nr(dip, index, &leaf_no); | |
748 | if (!error) | |
749 | error = get_leaf(dip, leaf_no, bh_out); | |
750 | ||
751 | return error; | |
752 | } | |
753 | ||
c752666c SW |
754 | static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode, |
755 | const struct qstr *name, | |
756 | gfs2_dscan_t scan, | |
757 | struct buffer_head **pbh) | |
b3b94faa | 758 | { |
c752666c SW |
759 | struct buffer_head *bh; |
760 | struct gfs2_dirent *dent; | |
feaa7bba | 761 | struct gfs2_inode *ip = GFS2_I(inode); |
b3b94faa DT |
762 | int error; |
763 | ||
c752666c SW |
764 | if (ip->i_di.di_flags & GFS2_DIF_EXHASH) { |
765 | struct gfs2_leaf *leaf; | |
766 | unsigned hsize = 1 << ip->i_di.di_depth; | |
767 | unsigned index; | |
768 | u64 ln; | |
769 | if (hsize * sizeof(u64) != ip->i_di.di_size) { | |
770 | gfs2_consist_inode(ip); | |
771 | return ERR_PTR(-EIO); | |
772 | } | |
feaa7bba | 773 | |
c752666c SW |
774 | index = name->hash >> (32 - ip->i_di.di_depth); |
775 | error = get_first_leaf(ip, index, &bh); | |
776 | if (error) | |
777 | return ERR_PTR(error); | |
778 | do { | |
779 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, | |
71b86f56 | 780 | scan, name, NULL); |
c752666c SW |
781 | if (dent) |
782 | goto got_dent; | |
783 | leaf = (struct gfs2_leaf *)bh->b_data; | |
784 | ln = be64_to_cpu(leaf->lf_next); | |
f4154ea0 | 785 | brelse(bh); |
c752666c SW |
786 | if (!ln) |
787 | break; | |
feaa7bba | 788 | |
c752666c SW |
789 | error = get_leaf(ip, ln, &bh); |
790 | } while(!error); | |
b3b94faa | 791 | |
c752666c | 792 | return error ? ERR_PTR(error) : NULL; |
b3b94faa | 793 | } |
b3b94faa | 794 | |
feaa7bba | 795 | |
c752666c SW |
796 | error = gfs2_meta_inode_buffer(ip, &bh); |
797 | if (error) | |
798 | return ERR_PTR(error); | |
71b86f56 | 799 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL); |
c752666c | 800 | got_dent: |
f4154ea0 | 801 | if (unlikely(dent == NULL || IS_ERR(dent))) { |
ed386507 SW |
802 | brelse(bh); |
803 | bh = NULL; | |
804 | } | |
c752666c SW |
805 | *pbh = bh; |
806 | return dent; | |
807 | } | |
b3b94faa | 808 | |
c752666c SW |
809 | static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth) |
810 | { | |
feaa7bba | 811 | struct gfs2_inode *ip = GFS2_I(inode); |
c752666c SW |
812 | u64 bn = gfs2_alloc_meta(ip); |
813 | struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn); | |
814 | struct gfs2_leaf *leaf; | |
815 | struct gfs2_dirent *dent; | |
71b86f56 | 816 | struct qstr name = { .name = "", .len = 0, .hash = 0 }; |
c752666c SW |
817 | if (!bh) |
818 | return NULL; | |
feaa7bba | 819 | |
c752666c SW |
820 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
821 | gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF); | |
822 | leaf = (struct gfs2_leaf *)bh->b_data; | |
823 | leaf->lf_depth = cpu_to_be16(depth); | |
824 | leaf->lf_entries = cpu_to_be16(0); | |
825 | leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE); | |
826 | leaf->lf_next = cpu_to_be64(0); | |
827 | memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved)); | |
828 | dent = (struct gfs2_dirent *)(leaf+1); | |
71b86f56 | 829 | gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent); |
c752666c SW |
830 | *pbh = bh; |
831 | return leaf; | |
b3b94faa DT |
832 | } |
833 | ||
834 | /** | |
835 | * dir_make_exhash - Convert a stuffed directory into an ExHash directory | |
836 | * @dip: The GFS2 inode | |
837 | * | |
838 | * Returns: 0 on success, error code otherwise | |
839 | */ | |
840 | ||
c752666c | 841 | static int dir_make_exhash(struct inode *inode) |
b3b94faa | 842 | { |
feaa7bba SW |
843 | struct gfs2_inode *dip = GFS2_I(inode); |
844 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
b3b94faa | 845 | struct gfs2_dirent *dent; |
c752666c | 846 | struct qstr args; |
b3b94faa DT |
847 | struct buffer_head *bh, *dibh; |
848 | struct gfs2_leaf *leaf; | |
849 | int y; | |
850 | uint32_t x; | |
851 | uint64_t *lp, bn; | |
852 | int error; | |
853 | ||
854 | error = gfs2_meta_inode_buffer(dip, &dibh); | |
855 | if (error) | |
856 | return error; | |
857 | ||
b3b94faa DT |
858 | /* Turn over a new leaf */ |
859 | ||
c752666c SW |
860 | leaf = new_leaf(inode, &bh, 0); |
861 | if (!leaf) | |
862 | return -ENOSPC; | |
863 | bn = bh->b_blocknr; | |
b3b94faa DT |
864 | |
865 | gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16)); | |
b3b94faa DT |
866 | leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries); |
867 | ||
868 | /* Copy dirents */ | |
869 | ||
870 | gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh, | |
871 | sizeof(struct gfs2_dinode)); | |
872 | ||
873 | /* Find last entry */ | |
874 | ||
875 | x = 0; | |
c752666c SW |
876 | args.len = bh->b_size - sizeof(struct gfs2_dinode) + |
877 | sizeof(struct gfs2_leaf); | |
878 | args.name = bh->b_data; | |
feaa7bba | 879 | dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size, |
71b86f56 | 880 | gfs2_dirent_last, &args, NULL); |
c752666c SW |
881 | if (!dent) { |
882 | brelse(bh); | |
883 | brelse(dibh); | |
884 | return -EIO; | |
885 | } | |
886 | if (IS_ERR(dent)) { | |
887 | brelse(bh); | |
888 | brelse(dibh); | |
889 | return PTR_ERR(dent); | |
b3b94faa | 890 | } |
b3b94faa DT |
891 | |
892 | /* Adjust the last dirent's record length | |
893 | (Remember that dent still points to the last entry.) */ | |
894 | ||
4dd651ad | 895 | dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) + |
b3b94faa | 896 | sizeof(struct gfs2_dinode) - |
4dd651ad | 897 | sizeof(struct gfs2_leaf)); |
b3b94faa DT |
898 | |
899 | brelse(bh); | |
900 | ||
901 | /* We're done with the new leaf block, now setup the new | |
902 | hash table. */ | |
903 | ||
d4e9c4c3 | 904 | gfs2_trans_add_bh(dip->i_gl, dibh, 1); |
b3b94faa DT |
905 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
906 | ||
907 | lp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)); | |
908 | ||
909 | for (x = sdp->sd_hash_ptrs; x--; lp++) | |
910 | *lp = cpu_to_be64(bn); | |
911 | ||
912 | dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2; | |
913 | dip->i_di.di_blocks++; | |
914 | dip->i_di.di_flags |= GFS2_DIF_EXHASH; | |
915 | dip->i_di.di_payload_format = 0; | |
916 | ||
917 | for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ; | |
918 | dip->i_di.di_depth = y; | |
919 | ||
920 | gfs2_dinode_out(&dip->i_di, dibh->b_data); | |
921 | ||
922 | brelse(dibh); | |
923 | ||
924 | return 0; | |
925 | } | |
926 | ||
927 | /** | |
928 | * dir_split_leaf - Split a leaf block into two | |
929 | * @dip: The GFS2 inode | |
930 | * @index: | |
931 | * @leaf_no: | |
932 | * | |
933 | * Returns: 0 on success, error code on failure | |
934 | */ | |
935 | ||
c752666c | 936 | static int dir_split_leaf(struct inode *inode, const struct qstr *name) |
b3b94faa | 937 | { |
feaa7bba | 938 | struct gfs2_inode *dip = GFS2_I(inode); |
b3b94faa DT |
939 | struct buffer_head *nbh, *obh, *dibh; |
940 | struct gfs2_leaf *nleaf, *oleaf; | |
4da3c646 | 941 | struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new; |
b3b94faa | 942 | uint32_t start, len, half_len, divider; |
c752666c SW |
943 | uint64_t bn, *lp, leaf_no; |
944 | uint32_t index; | |
b3b94faa DT |
945 | int x, moved = 0; |
946 | int error; | |
947 | ||
c752666c SW |
948 | index = name->hash >> (32 - dip->i_di.di_depth); |
949 | error = get_leaf_nr(dip, index, &leaf_no); | |
950 | if (error) | |
951 | return error; | |
b3b94faa DT |
952 | |
953 | /* Get the old leaf block */ | |
b3b94faa DT |
954 | error = get_leaf(dip, leaf_no, &obh); |
955 | if (error) | |
e90deff5 | 956 | return error; |
b3b94faa | 957 | |
b3b94faa | 958 | oleaf = (struct gfs2_leaf *)obh->b_data; |
e90deff5 SW |
959 | if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) { |
960 | brelse(obh); | |
961 | return 1; /* can't split */ | |
962 | } | |
963 | ||
964 | gfs2_trans_add_bh(dip->i_gl, obh, 1); | |
b3b94faa | 965 | |
c752666c SW |
966 | nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1); |
967 | if (!nleaf) { | |
968 | brelse(obh); | |
969 | return -ENOSPC; | |
970 | } | |
971 | bn = nbh->b_blocknr; | |
b3b94faa | 972 | |
c752666c | 973 | /* Compute the start and len of leaf pointers in the hash table. */ |
b3b94faa DT |
974 | len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth)); |
975 | half_len = len >> 1; | |
976 | if (!half_len) { | |
e90deff5 | 977 | printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index); |
b3b94faa DT |
978 | gfs2_consist_inode(dip); |
979 | error = -EIO; | |
980 | goto fail_brelse; | |
981 | } | |
982 | ||
983 | start = (index & ~(len - 1)); | |
984 | ||
985 | /* Change the pointers. | |
986 | Don't bother distinguishing stuffed from non-stuffed. | |
987 | This code is complicated enough already. */ | |
c752666c | 988 | lp = kmalloc(half_len * sizeof(uint64_t), GFP_NOFS | __GFP_NOFAIL); |
b3b94faa | 989 | /* Change the pointers */ |
b3b94faa DT |
990 | for (x = 0; x < half_len; x++) |
991 | lp[x] = cpu_to_be64(bn); | |
992 | ||
e13940ba | 993 | error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(uint64_t), |
71b86f56 | 994 | half_len * sizeof(uint64_t)); |
b3b94faa DT |
995 | if (error != half_len * sizeof(uint64_t)) { |
996 | if (error >= 0) | |
997 | error = -EIO; | |
998 | goto fail_lpfree; | |
999 | } | |
1000 | ||
1001 | kfree(lp); | |
1002 | ||
1003 | /* Compute the divider */ | |
b3b94faa DT |
1004 | divider = (start + half_len) << (32 - dip->i_di.di_depth); |
1005 | ||
1006 | /* Copy the entries */ | |
b3b94faa DT |
1007 | dirent_first(dip, obh, &dent); |
1008 | ||
1009 | do { | |
1010 | next = dent; | |
1011 | if (dirent_next(dip, obh, &next)) | |
1012 | next = NULL; | |
1013 | ||
1014 | if (dent->de_inum.no_addr && | |
1015 | be32_to_cpu(dent->de_hash) < divider) { | |
c752666c SW |
1016 | struct qstr str; |
1017 | str.name = (char*)(dent+1); | |
1018 | str.len = be16_to_cpu(dent->de_name_len); | |
1019 | str.hash = be32_to_cpu(dent->de_hash); | |
71b86f56 | 1020 | new = gfs2_dirent_alloc(inode, nbh, &str); |
c752666c SW |
1021 | if (IS_ERR(new)) { |
1022 | error = PTR_ERR(new); | |
1023 | break; | |
1024 | } | |
b3b94faa DT |
1025 | |
1026 | new->de_inum = dent->de_inum; /* No endian worries */ | |
b3b94faa | 1027 | new->de_type = dent->de_type; /* No endian worries */ |
c752666c | 1028 | nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1); |
b3b94faa DT |
1029 | |
1030 | dirent_del(dip, obh, prev, dent); | |
1031 | ||
1032 | if (!oleaf->lf_entries) | |
1033 | gfs2_consist_inode(dip); | |
c752666c | 1034 | oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1); |
b3b94faa DT |
1035 | |
1036 | if (!prev) | |
1037 | prev = dent; | |
1038 | ||
1039 | moved = 1; | |
c752666c | 1040 | } else { |
b3b94faa | 1041 | prev = dent; |
c752666c | 1042 | } |
b3b94faa | 1043 | dent = next; |
c752666c | 1044 | } while (dent); |
b3b94faa | 1045 | |
c752666c | 1046 | oleaf->lf_depth = nleaf->lf_depth; |
b3b94faa DT |
1047 | |
1048 | error = gfs2_meta_inode_buffer(dip, &dibh); | |
feaa7bba | 1049 | if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) { |
b3b94faa DT |
1050 | dip->i_di.di_blocks++; |
1051 | gfs2_dinode_out(&dip->i_di, dibh->b_data); | |
1052 | brelse(dibh); | |
1053 | } | |
1054 | ||
1055 | brelse(obh); | |
1056 | brelse(nbh); | |
1057 | ||
1058 | return error; | |
1059 | ||
e90deff5 | 1060 | fail_lpfree: |
b3b94faa DT |
1061 | kfree(lp); |
1062 | ||
e90deff5 | 1063 | fail_brelse: |
b3b94faa | 1064 | brelse(obh); |
b3b94faa DT |
1065 | brelse(nbh); |
1066 | return error; | |
1067 | } | |
1068 | ||
1069 | /** | |
1070 | * dir_double_exhash - Double size of ExHash table | |
1071 | * @dip: The GFS2 dinode | |
1072 | * | |
1073 | * Returns: 0 on success, error code on failure | |
1074 | */ | |
1075 | ||
1076 | static int dir_double_exhash(struct gfs2_inode *dip) | |
1077 | { | |
feaa7bba | 1078 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
1079 | struct buffer_head *dibh; |
1080 | uint32_t hsize; | |
1081 | uint64_t *buf; | |
1082 | uint64_t *from, *to; | |
1083 | uint64_t block; | |
1084 | int x; | |
1085 | int error = 0; | |
1086 | ||
1087 | hsize = 1 << dip->i_di.di_depth; | |
1088 | if (hsize * sizeof(uint64_t) != dip->i_di.di_size) { | |
1089 | gfs2_consist_inode(dip); | |
1090 | return -EIO; | |
1091 | } | |
1092 | ||
1093 | /* Allocate both the "from" and "to" buffers in one big chunk */ | |
1094 | ||
1095 | buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL); | |
1096 | ||
1097 | for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) { | |
e13940ba | 1098 | error = gfs2_dir_read_data(dip, (char *)buf, |
b3b94faa DT |
1099 | block * sdp->sd_hash_bsize, |
1100 | sdp->sd_hash_bsize); | |
1101 | if (error != sdp->sd_hash_bsize) { | |
1102 | if (error >= 0) | |
1103 | error = -EIO; | |
1104 | goto fail; | |
1105 | } | |
1106 | ||
1107 | from = buf; | |
1108 | to = (uint64_t *)((char *)buf + sdp->sd_hash_bsize); | |
1109 | ||
1110 | for (x = sdp->sd_hash_ptrs; x--; from++) { | |
1111 | *to++ = *from; /* No endianess worries */ | |
1112 | *to++ = *from; | |
1113 | } | |
1114 | ||
e13940ba | 1115 | error = gfs2_dir_write_data(dip, |
b3b94faa DT |
1116 | (char *)buf + sdp->sd_hash_bsize, |
1117 | block * sdp->sd_sb.sb_bsize, | |
1118 | sdp->sd_sb.sb_bsize); | |
1119 | if (error != sdp->sd_sb.sb_bsize) { | |
1120 | if (error >= 0) | |
1121 | error = -EIO; | |
1122 | goto fail; | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | kfree(buf); | |
1127 | ||
1128 | error = gfs2_meta_inode_buffer(dip, &dibh); | |
1129 | if (!gfs2_assert_withdraw(sdp, !error)) { | |
1130 | dip->i_di.di_depth++; | |
1131 | gfs2_dinode_out(&dip->i_di, dibh->b_data); | |
1132 | brelse(dibh); | |
1133 | } | |
1134 | ||
1135 | return error; | |
1136 | ||
1137 | fail: | |
1138 | kfree(buf); | |
1139 | ||
1140 | return error; | |
1141 | } | |
1142 | ||
1143 | /** | |
1144 | * compare_dents - compare directory entries by hash value | |
1145 | * @a: first dent | |
1146 | * @b: second dent | |
1147 | * | |
1148 | * When comparing the hash entries of @a to @b: | |
1149 | * gt: returns 1 | |
1150 | * lt: returns -1 | |
1151 | * eq: returns 0 | |
1152 | */ | |
1153 | ||
1154 | static int compare_dents(const void *a, const void *b) | |
1155 | { | |
1156 | struct gfs2_dirent *dent_a, *dent_b; | |
1157 | uint32_t hash_a, hash_b; | |
1158 | int ret = 0; | |
1159 | ||
1160 | dent_a = *(struct gfs2_dirent **)a; | |
c752666c | 1161 | hash_a = be32_to_cpu(dent_a->de_hash); |
b3b94faa DT |
1162 | |
1163 | dent_b = *(struct gfs2_dirent **)b; | |
c752666c | 1164 | hash_b = be32_to_cpu(dent_b->de_hash); |
b3b94faa DT |
1165 | |
1166 | if (hash_a > hash_b) | |
1167 | ret = 1; | |
1168 | else if (hash_a < hash_b) | |
1169 | ret = -1; | |
1170 | else { | |
4dd651ad SW |
1171 | unsigned int len_a = be16_to_cpu(dent_a->de_name_len); |
1172 | unsigned int len_b = be16_to_cpu(dent_b->de_name_len); | |
b3b94faa DT |
1173 | |
1174 | if (len_a > len_b) | |
1175 | ret = 1; | |
1176 | else if (len_a < len_b) | |
1177 | ret = -1; | |
1178 | else | |
1179 | ret = memcmp((char *)(dent_a + 1), | |
1180 | (char *)(dent_b + 1), | |
1181 | len_a); | |
1182 | } | |
1183 | ||
1184 | return ret; | |
1185 | } | |
1186 | ||
1187 | /** | |
1188 | * do_filldir_main - read out directory entries | |
1189 | * @dip: The GFS2 inode | |
1190 | * @offset: The offset in the file to read from | |
1191 | * @opaque: opaque data to pass to filldir | |
1192 | * @filldir: The function to pass entries to | |
1193 | * @darr: an array of struct gfs2_dirent pointers to read | |
1194 | * @entries: the number of entries in darr | |
1195 | * @copied: pointer to int that's non-zero if a entry has been copied out | |
1196 | * | |
1197 | * Jump through some hoops to make sure that if there are hash collsions, | |
1198 | * they are read out at the beginning of a buffer. We want to minimize | |
1199 | * the possibility that they will fall into different readdir buffers or | |
1200 | * that someone will want to seek to that location. | |
1201 | * | |
1202 | * Returns: errno, >0 on exception from filldir | |
1203 | */ | |
1204 | ||
1205 | static int do_filldir_main(struct gfs2_inode *dip, uint64_t *offset, | |
1206 | void *opaque, gfs2_filldir_t filldir, | |
71b86f56 | 1207 | const struct gfs2_dirent **darr, uint32_t entries, |
b3b94faa DT |
1208 | int *copied) |
1209 | { | |
71b86f56 | 1210 | const struct gfs2_dirent *dent, *dent_next; |
b3b94faa DT |
1211 | struct gfs2_inum inum; |
1212 | uint64_t off, off_next; | |
1213 | unsigned int x, y; | |
1214 | int run = 0; | |
1215 | int error = 0; | |
1216 | ||
1217 | sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL); | |
1218 | ||
1219 | dent_next = darr[0]; | |
1220 | off_next = be32_to_cpu(dent_next->de_hash); | |
1221 | off_next = gfs2_disk_hash2offset(off_next); | |
1222 | ||
1223 | for (x = 0, y = 1; x < entries; x++, y++) { | |
1224 | dent = dent_next; | |
1225 | off = off_next; | |
1226 | ||
1227 | if (y < entries) { | |
1228 | dent_next = darr[y]; | |
1229 | off_next = be32_to_cpu(dent_next->de_hash); | |
1230 | off_next = gfs2_disk_hash2offset(off_next); | |
1231 | ||
1232 | if (off < *offset) | |
1233 | continue; | |
1234 | *offset = off; | |
1235 | ||
1236 | if (off_next == off) { | |
1237 | if (*copied && !run) | |
1238 | return 1; | |
1239 | run = 1; | |
1240 | } else | |
1241 | run = 0; | |
1242 | } else { | |
1243 | if (off < *offset) | |
1244 | continue; | |
1245 | *offset = off; | |
1246 | } | |
1247 | ||
1248 | gfs2_inum_in(&inum, (char *)&dent->de_inum); | |
1249 | ||
1250 | error = filldir(opaque, (char *)(dent + 1), | |
4dd651ad | 1251 | be16_to_cpu(dent->de_name_len), |
b3b94faa | 1252 | off, &inum, |
4dd651ad | 1253 | be16_to_cpu(dent->de_type)); |
b3b94faa DT |
1254 | if (error) |
1255 | return 1; | |
1256 | ||
1257 | *copied = 1; | |
1258 | } | |
1259 | ||
1260 | /* Increment the *offset by one, so the next time we come into the | |
1261 | do_filldir fxn, we get the next entry instead of the last one in the | |
1262 | current leaf */ | |
1263 | ||
1264 | (*offset)++; | |
1265 | ||
1266 | return 0; | |
1267 | } | |
1268 | ||
71b86f56 SW |
1269 | static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque, |
1270 | gfs2_filldir_t filldir, int *copied, | |
1271 | unsigned *depth, u64 leaf_no) | |
b3b94faa | 1272 | { |
feaa7bba | 1273 | struct gfs2_inode *ip = GFS2_I(inode); |
71b86f56 SW |
1274 | struct buffer_head *bh; |
1275 | struct gfs2_leaf *lf; | |
1276 | unsigned entries = 0; | |
1277 | unsigned leaves = 0; | |
1278 | const struct gfs2_dirent **darr, *dent; | |
1279 | struct dirent_gather g; | |
1280 | struct buffer_head **larr; | |
1281 | int leaf = 0; | |
1282 | int error, i; | |
1283 | u64 lfn = leaf_no; | |
b3b94faa | 1284 | |
b3b94faa | 1285 | do { |
71b86f56 | 1286 | error = get_leaf(ip, lfn, &bh); |
b3b94faa | 1287 | if (error) |
71b86f56 SW |
1288 | goto out; |
1289 | lf = (struct gfs2_leaf *)bh->b_data; | |
1290 | if (leaves == 0) | |
1291 | *depth = be16_to_cpu(lf->lf_depth); | |
1292 | entries += be16_to_cpu(lf->lf_entries); | |
1293 | leaves++; | |
1294 | lfn = be64_to_cpu(lf->lf_next); | |
1295 | brelse(bh); | |
1296 | } while(lfn); | |
b3b94faa DT |
1297 | |
1298 | if (!entries) | |
1299 | return 0; | |
1300 | ||
71b86f56 | 1301 | error = -ENOMEM; |
fe1bdedc | 1302 | larr = vmalloc((leaves + entries) * sizeof(void*)); |
71b86f56 SW |
1303 | if (!larr) |
1304 | goto out; | |
1305 | darr = (const struct gfs2_dirent **)(larr + leaves); | |
1306 | g.pdent = darr; | |
1307 | g.offset = 0; | |
1308 | lfn = leaf_no; | |
b3b94faa | 1309 | |
71b86f56 SW |
1310 | do { |
1311 | error = get_leaf(ip, lfn, &bh); | |
b3b94faa | 1312 | if (error) |
71b86f56 SW |
1313 | goto out_kfree; |
1314 | lf = (struct gfs2_leaf *)bh->b_data; | |
1315 | lfn = be64_to_cpu(lf->lf_next); | |
1316 | if (lf->lf_entries) { | |
1317 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, | |
1318 | gfs2_dirent_gather, NULL, &g); | |
1319 | error = PTR_ERR(dent); | |
1320 | if (IS_ERR(dent)) { | |
1321 | goto out_kfree; | |
1322 | } | |
1323 | error = 0; | |
1324 | larr[leaf++] = bh; | |
b3b94faa | 1325 | } else { |
71b86f56 | 1326 | brelse(bh); |
b3b94faa | 1327 | } |
71b86f56 | 1328 | } while(lfn); |
b3b94faa | 1329 | |
71b86f56 | 1330 | error = do_filldir_main(ip, offset, opaque, filldir, darr, |
b3b94faa | 1331 | entries, copied); |
71b86f56 SW |
1332 | out_kfree: |
1333 | for(i = 0; i < leaf; i++) | |
1334 | brelse(larr[i]); | |
fe1bdedc | 1335 | vfree(larr); |
71b86f56 | 1336 | out: |
b3b94faa DT |
1337 | return error; |
1338 | } | |
1339 | ||
1340 | /** | |
c752666c SW |
1341 | * dir_e_read - Reads the entries from a directory into a filldir buffer |
1342 | * @dip: dinode pointer | |
1343 | * @offset: the hash of the last entry read shifted to the right once | |
1344 | * @opaque: buffer for the filldir function to fill | |
1345 | * @filldir: points to the filldir function to use | |
b3b94faa | 1346 | * |
c752666c | 1347 | * Returns: errno |
b3b94faa DT |
1348 | */ |
1349 | ||
71b86f56 | 1350 | static int dir_e_read(struct inode *inode, uint64_t *offset, void *opaque, |
c752666c | 1351 | gfs2_filldir_t filldir) |
b3b94faa | 1352 | { |
feaa7bba SW |
1353 | struct gfs2_inode *dip = GFS2_I(inode); |
1354 | struct gfs2_sbd *sdp = GFS2_SB(inode); | |
71b86f56 | 1355 | uint32_t hsize, len = 0; |
c752666c SW |
1356 | uint32_t ht_offset, lp_offset, ht_offset_cur = -1; |
1357 | uint32_t hash, index; | |
1358 | uint64_t *lp; | |
1359 | int copied = 0; | |
1360 | int error = 0; | |
4da3c646 | 1361 | unsigned depth = 0; |
b3b94faa DT |
1362 | |
1363 | hsize = 1 << dip->i_di.di_depth; | |
1364 | if (hsize * sizeof(uint64_t) != dip->i_di.di_size) { | |
1365 | gfs2_consist_inode(dip); | |
1366 | return -EIO; | |
1367 | } | |
1368 | ||
1369 | hash = gfs2_dir_offset2hash(*offset); | |
1370 | index = hash >> (32 - dip->i_di.di_depth); | |
1371 | ||
1372 | lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL); | |
1373 | if (!lp) | |
1374 | return -ENOMEM; | |
1375 | ||
1376 | while (index < hsize) { | |
1377 | lp_offset = index & (sdp->sd_hash_ptrs - 1); | |
1378 | ht_offset = index - lp_offset; | |
1379 | ||
1380 | if (ht_offset_cur != ht_offset) { | |
e13940ba | 1381 | error = gfs2_dir_read_data(dip, (char *)lp, |
b3b94faa DT |
1382 | ht_offset * sizeof(uint64_t), |
1383 | sdp->sd_hash_bsize); | |
1384 | if (error != sdp->sd_hash_bsize) { | |
1385 | if (error >= 0) | |
1386 | error = -EIO; | |
1387 | goto out; | |
1388 | } | |
1389 | ht_offset_cur = ht_offset; | |
1390 | } | |
1391 | ||
71b86f56 SW |
1392 | error = gfs2_dir_read_leaf(inode, offset, opaque, filldir, |
1393 | &copied, &depth, | |
1394 | be64_to_cpu(lp[lp_offset])); | |
b3b94faa | 1395 | if (error) |
71b86f56 | 1396 | break; |
b3b94faa | 1397 | |
71b86f56 | 1398 | len = 1 << (dip->i_di.di_depth - depth); |
b3b94faa DT |
1399 | index = (index & ~(len - 1)) + len; |
1400 | } | |
1401 | ||
71b86f56 | 1402 | out: |
b3b94faa | 1403 | kfree(lp); |
71b86f56 SW |
1404 | if (error > 0) |
1405 | error = 0; | |
b3b94faa DT |
1406 | return error; |
1407 | } | |
1408 | ||
71b86f56 SW |
1409 | int gfs2_dir_read(struct inode *inode, uint64_t *offset, void *opaque, |
1410 | gfs2_filldir_t filldir) | |
b3b94faa | 1411 | { |
feaa7bba | 1412 | struct gfs2_inode *dip = GFS2_I(inode); |
71b86f56 SW |
1413 | struct dirent_gather g; |
1414 | const struct gfs2_dirent **darr, *dent; | |
b3b94faa DT |
1415 | struct buffer_head *dibh; |
1416 | int copied = 0; | |
1417 | int error; | |
1418 | ||
71b86f56 SW |
1419 | if (!dip->i_di.di_entries) |
1420 | return 0; | |
1421 | ||
1422 | if (dip->i_di.di_flags & GFS2_DIF_EXHASH) | |
1423 | return dir_e_read(inode, offset, opaque, filldir); | |
1424 | ||
b3b94faa DT |
1425 | if (!gfs2_is_stuffed(dip)) { |
1426 | gfs2_consist_inode(dip); | |
1427 | return -EIO; | |
1428 | } | |
1429 | ||
b3b94faa DT |
1430 | error = gfs2_meta_inode_buffer(dip, &dibh); |
1431 | if (error) | |
1432 | return error; | |
1433 | ||
71b86f56 SW |
1434 | error = -ENOMEM; |
1435 | darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *), | |
1436 | GFP_KERNEL); | |
1437 | if (darr) { | |
1438 | g.pdent = darr; | |
1439 | g.offset = 0; | |
1440 | dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size, | |
1441 | gfs2_dirent_gather, NULL, &g); | |
1442 | if (IS_ERR(dent)) { | |
1443 | error = PTR_ERR(dent); | |
1444 | goto out; | |
1445 | } | |
1446 | error = do_filldir_main(dip, offset, opaque, filldir, darr, | |
1447 | dip->i_di.di_entries, &copied); | |
1448 | out: | |
1449 | kfree(darr); | |
1450 | } | |
1451 | ||
b3b94faa DT |
1452 | if (error > 0) |
1453 | error = 0; | |
1454 | ||
1455 | brelse(dibh); | |
1456 | ||
1457 | return error; | |
1458 | } | |
1459 | ||
b3b94faa DT |
1460 | /** |
1461 | * gfs2_dir_search - Search a directory | |
1462 | * @dip: The GFS2 inode | |
1463 | * @filename: | |
1464 | * @inode: | |
1465 | * | |
1466 | * This routine searches a directory for a file or another directory. | |
1467 | * Assumes a glock is held on dip. | |
1468 | * | |
1469 | * Returns: errno | |
1470 | */ | |
1471 | ||
c752666c | 1472 | int gfs2_dir_search(struct inode *dir, const struct qstr *name, |
b3b94faa DT |
1473 | struct gfs2_inum *inum, unsigned int *type) |
1474 | { | |
c752666c SW |
1475 | struct buffer_head *bh; |
1476 | struct gfs2_dirent *dent; | |
1477 | ||
1478 | dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh); | |
1479 | if (dent) { | |
1480 | if (IS_ERR(dent)) | |
1481 | return PTR_ERR(dent); | |
1482 | if (inum) | |
1483 | gfs2_inum_in(inum, (char *)&dent->de_inum); | |
1484 | if (type) | |
1485 | *type = be16_to_cpu(dent->de_type); | |
1486 | brelse(bh); | |
1487 | return 0; | |
1488 | } | |
1489 | return -ENOENT; | |
1490 | } | |
1491 | ||
1492 | static int dir_new_leaf(struct inode *inode, const struct qstr *name) | |
1493 | { | |
1494 | struct buffer_head *bh, *obh; | |
feaa7bba | 1495 | struct gfs2_inode *ip = GFS2_I(inode); |
c752666c | 1496 | struct gfs2_leaf *leaf, *oleaf; |
b3b94faa | 1497 | int error; |
c752666c SW |
1498 | u32 index; |
1499 | u64 bn; | |
b3b94faa | 1500 | |
c752666c SW |
1501 | index = name->hash >> (32 - ip->i_di.di_depth); |
1502 | error = get_first_leaf(ip, index, &obh); | |
1503 | if (error) | |
1504 | return error; | |
1505 | do { | |
1506 | oleaf = (struct gfs2_leaf *)obh->b_data; | |
1507 | bn = be64_to_cpu(oleaf->lf_next); | |
1508 | if (!bn) | |
1509 | break; | |
1510 | brelse(obh); | |
1511 | error = get_leaf(ip, bn, &obh); | |
1512 | if (error) | |
1513 | return error; | |
1514 | } while(1); | |
b3b94faa | 1515 | |
c752666c SW |
1516 | gfs2_trans_add_bh(ip->i_gl, obh, 1); |
1517 | ||
1518 | leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth)); | |
1519 | if (!leaf) { | |
1520 | brelse(obh); | |
1521 | return -ENOSPC; | |
1522 | } | |
4d8012b6 | 1523 | oleaf->lf_next = cpu_to_be64(bh->b_blocknr); |
c752666c SW |
1524 | brelse(bh); |
1525 | brelse(obh); | |
1526 | ||
1527 | error = gfs2_meta_inode_buffer(ip, &bh); | |
1528 | if (error) | |
1529 | return error; | |
1530 | gfs2_trans_add_bh(ip->i_gl, bh, 1); | |
1531 | ip->i_di.di_blocks++; | |
1532 | gfs2_dinode_out(&ip->i_di, bh->b_data); | |
1533 | brelse(bh); | |
1534 | return 0; | |
b3b94faa DT |
1535 | } |
1536 | ||
1537 | /** | |
1538 | * gfs2_dir_add - Add new filename into directory | |
1539 | * @dip: The GFS2 inode | |
1540 | * @filename: The new name | |
1541 | * @inode: The inode number of the entry | |
1542 | * @type: The type of the entry | |
1543 | * | |
1544 | * Returns: 0 on success, error code on failure | |
1545 | */ | |
1546 | ||
c752666c SW |
1547 | int gfs2_dir_add(struct inode *inode, const struct qstr *name, |
1548 | const struct gfs2_inum *inum, unsigned type) | |
b3b94faa | 1549 | { |
feaa7bba | 1550 | struct gfs2_inode *ip = GFS2_I(inode); |
c752666c SW |
1551 | struct buffer_head *bh; |
1552 | struct gfs2_dirent *dent; | |
1553 | struct gfs2_leaf *leaf; | |
b3b94faa DT |
1554 | int error; |
1555 | ||
c752666c SW |
1556 | while(1) { |
1557 | dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, | |
1558 | &bh); | |
1559 | if (dent) { | |
1560 | if (IS_ERR(dent)) | |
1561 | return PTR_ERR(dent); | |
1562 | dent = gfs2_init_dirent(inode, dent, name, bh); | |
1563 | gfs2_inum_out(inum, (char *)&dent->de_inum); | |
1564 | dent->de_type = cpu_to_be16(type); | |
1565 | if (ip->i_di.di_flags & GFS2_DIF_EXHASH) { | |
1566 | leaf = (struct gfs2_leaf *)bh->b_data; | |
1567 | leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1); | |
1568 | } | |
1569 | brelse(bh); | |
1570 | error = gfs2_meta_inode_buffer(ip, &bh); | |
1571 | if (error) | |
1572 | break; | |
1573 | gfs2_trans_add_bh(ip->i_gl, bh, 1); | |
1574 | ip->i_di.di_entries++; | |
1575 | ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds(); | |
1576 | gfs2_dinode_out(&ip->i_di, bh->b_data); | |
1577 | brelse(bh); | |
1578 | error = 0; | |
1579 | break; | |
1580 | } | |
1581 | if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) { | |
1582 | error = dir_make_exhash(inode); | |
1583 | if (error) | |
1584 | break; | |
1585 | continue; | |
1586 | } | |
1587 | error = dir_split_leaf(inode, name); | |
1588 | if (error == 0) | |
1589 | continue; | |
e90deff5 | 1590 | if (error < 0) |
c752666c SW |
1591 | break; |
1592 | if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) { | |
1593 | error = dir_double_exhash(ip); | |
1594 | if (error) | |
1595 | break; | |
1596 | error = dir_split_leaf(inode, name); | |
e90deff5 | 1597 | if (error < 0) |
c752666c | 1598 | break; |
e90deff5 SW |
1599 | if (error == 0) |
1600 | continue; | |
c752666c SW |
1601 | } |
1602 | error = dir_new_leaf(inode, name); | |
1603 | if (!error) | |
1604 | continue; | |
1605 | error = -ENOSPC; | |
1606 | break; | |
1607 | } | |
b3b94faa DT |
1608 | return error; |
1609 | } | |
1610 | ||
c752666c | 1611 | |
b3b94faa DT |
1612 | /** |
1613 | * gfs2_dir_del - Delete a directory entry | |
1614 | * @dip: The GFS2 inode | |
1615 | * @filename: The filename | |
1616 | * | |
1617 | * Returns: 0 on success, error code on failure | |
1618 | */ | |
1619 | ||
c752666c | 1620 | int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name) |
b3b94faa | 1621 | { |
c752666c SW |
1622 | struct gfs2_dirent *dent, *prev = NULL; |
1623 | struct buffer_head *bh; | |
b3b94faa DT |
1624 | int error; |
1625 | ||
c752666c SW |
1626 | /* Returns _either_ the entry (if its first in block) or the |
1627 | previous entry otherwise */ | |
feaa7bba | 1628 | dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh); |
c752666c SW |
1629 | if (!dent) { |
1630 | gfs2_consist_inode(dip); | |
1631 | return -EIO; | |
1632 | } | |
1633 | if (IS_ERR(dent)) { | |
1634 | gfs2_consist_inode(dip); | |
1635 | return PTR_ERR(dent); | |
1636 | } | |
1637 | /* If not first in block, adjust pointers accordingly */ | |
71b86f56 | 1638 | if (gfs2_dirent_find(dent, name, NULL) == 0) { |
c752666c SW |
1639 | prev = dent; |
1640 | dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len)); | |
1641 | } | |
1642 | ||
1643 | dirent_del(dip, bh, prev, dent); | |
1644 | if (dip->i_di.di_flags & GFS2_DIF_EXHASH) { | |
1645 | struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data; | |
1646 | u16 entries = be16_to_cpu(leaf->lf_entries); | |
1647 | if (!entries) | |
1648 | gfs2_consist_inode(dip); | |
1649 | leaf->lf_entries = cpu_to_be16(--entries); | |
c752666c | 1650 | } |
ed386507 | 1651 | brelse(bh); |
c752666c SW |
1652 | |
1653 | error = gfs2_meta_inode_buffer(dip, &bh); | |
1654 | if (error) | |
1655 | return error; | |
1656 | ||
1657 | if (!dip->i_di.di_entries) | |
1658 | gfs2_consist_inode(dip); | |
1659 | gfs2_trans_add_bh(dip->i_gl, bh, 1); | |
1660 | dip->i_di.di_entries--; | |
1661 | dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds(); | |
1662 | gfs2_dinode_out(&dip->i_di, bh->b_data); | |
1663 | brelse(bh); | |
feaa7bba | 1664 | mark_inode_dirty(&dip->i_inode); |
b3b94faa DT |
1665 | |
1666 | return error; | |
1667 | } | |
1668 | ||
b3b94faa DT |
1669 | /** |
1670 | * gfs2_dir_mvino - Change inode number of directory entry | |
1671 | * @dip: The GFS2 inode | |
1672 | * @filename: | |
1673 | * @new_inode: | |
1674 | * | |
1675 | * This routine changes the inode number of a directory entry. It's used | |
1676 | * by rename to change ".." when a directory is moved. | |
1677 | * Assumes a glock is held on dvp. | |
1678 | * | |
1679 | * Returns: errno | |
1680 | */ | |
1681 | ||
c752666c | 1682 | int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename, |
b3b94faa DT |
1683 | struct gfs2_inum *inum, unsigned int new_type) |
1684 | { | |
c752666c SW |
1685 | struct buffer_head *bh; |
1686 | struct gfs2_dirent *dent; | |
b3b94faa DT |
1687 | int error; |
1688 | ||
feaa7bba | 1689 | dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh); |
c752666c SW |
1690 | if (!dent) { |
1691 | gfs2_consist_inode(dip); | |
1692 | return -EIO; | |
1693 | } | |
1694 | if (IS_ERR(dent)) | |
1695 | return PTR_ERR(dent); | |
b3b94faa | 1696 | |
c752666c SW |
1697 | gfs2_trans_add_bh(dip->i_gl, bh, 1); |
1698 | gfs2_inum_out(inum, (char *)&dent->de_inum); | |
1699 | dent->de_type = cpu_to_be16(new_type); | |
1700 | ||
1701 | if (dip->i_di.di_flags & GFS2_DIF_EXHASH) { | |
1702 | brelse(bh); | |
1703 | error = gfs2_meta_inode_buffer(dip, &bh); | |
1704 | if (error) | |
1705 | return error; | |
1706 | gfs2_trans_add_bh(dip->i_gl, bh, 1); | |
1707 | } | |
1708 | ||
1709 | dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds(); | |
1710 | gfs2_dinode_out(&dip->i_di, bh->b_data); | |
1711 | brelse(bh); | |
1712 | return 0; | |
b3b94faa DT |
1713 | } |
1714 | ||
1715 | /** | |
1716 | * foreach_leaf - call a function for each leaf in a directory | |
1717 | * @dip: the directory | |
1718 | * @lc: the function to call for each each | |
1719 | * @data: private data to pass to it | |
1720 | * | |
1721 | * Returns: errno | |
1722 | */ | |
1723 | ||
1724 | static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data) | |
1725 | { | |
feaa7bba | 1726 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa | 1727 | struct buffer_head *bh; |
c752666c | 1728 | struct gfs2_leaf *leaf; |
b3b94faa DT |
1729 | uint32_t hsize, len; |
1730 | uint32_t ht_offset, lp_offset, ht_offset_cur = -1; | |
1731 | uint32_t index = 0; | |
1732 | uint64_t *lp; | |
1733 | uint64_t leaf_no; | |
1734 | int error = 0; | |
1735 | ||
1736 | hsize = 1 << dip->i_di.di_depth; | |
1737 | if (hsize * sizeof(uint64_t) != dip->i_di.di_size) { | |
1738 | gfs2_consist_inode(dip); | |
1739 | return -EIO; | |
1740 | } | |
1741 | ||
1742 | lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL); | |
1743 | if (!lp) | |
1744 | return -ENOMEM; | |
1745 | ||
1746 | while (index < hsize) { | |
1747 | lp_offset = index & (sdp->sd_hash_ptrs - 1); | |
1748 | ht_offset = index - lp_offset; | |
1749 | ||
1750 | if (ht_offset_cur != ht_offset) { | |
e13940ba | 1751 | error = gfs2_dir_read_data(dip, (char *)lp, |
b3b94faa DT |
1752 | ht_offset * sizeof(uint64_t), |
1753 | sdp->sd_hash_bsize); | |
1754 | if (error != sdp->sd_hash_bsize) { | |
1755 | if (error >= 0) | |
1756 | error = -EIO; | |
1757 | goto out; | |
1758 | } | |
1759 | ht_offset_cur = ht_offset; | |
1760 | } | |
1761 | ||
1762 | leaf_no = be64_to_cpu(lp[lp_offset]); | |
1763 | if (leaf_no) { | |
1764 | error = get_leaf(dip, leaf_no, &bh); | |
1765 | if (error) | |
1766 | goto out; | |
c752666c | 1767 | leaf = (struct gfs2_leaf *)bh->b_data; |
c752666c | 1768 | len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth)); |
634ee0b9 | 1769 | brelse(bh); |
b3b94faa DT |
1770 | |
1771 | error = lc(dip, index, len, leaf_no, data); | |
1772 | if (error) | |
1773 | goto out; | |
1774 | ||
1775 | index = (index & ~(len - 1)) + len; | |
1776 | } else | |
1777 | index++; | |
1778 | } | |
1779 | ||
1780 | if (index != hsize) { | |
1781 | gfs2_consist_inode(dip); | |
1782 | error = -EIO; | |
1783 | } | |
1784 | ||
634ee0b9 | 1785 | out: |
b3b94faa DT |
1786 | kfree(lp); |
1787 | ||
1788 | return error; | |
1789 | } | |
1790 | ||
1791 | /** | |
1792 | * leaf_dealloc - Deallocate a directory leaf | |
1793 | * @dip: the directory | |
1794 | * @index: the hash table offset in the directory | |
1795 | * @len: the number of pointers to this leaf | |
1796 | * @leaf_no: the leaf number | |
1797 | * @data: not used | |
1798 | * | |
1799 | * Returns: errno | |
1800 | */ | |
1801 | ||
1802 | static int leaf_dealloc(struct gfs2_inode *dip, uint32_t index, uint32_t len, | |
1803 | uint64_t leaf_no, void *data) | |
1804 | { | |
feaa7bba | 1805 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
c752666c | 1806 | struct gfs2_leaf *tmp_leaf; |
b3b94faa DT |
1807 | struct gfs2_rgrp_list rlist; |
1808 | struct buffer_head *bh, *dibh; | |
c752666c | 1809 | uint64_t blk, nblk; |
b3b94faa DT |
1810 | unsigned int rg_blocks = 0, l_blocks = 0; |
1811 | char *ht; | |
1812 | unsigned int x, size = len * sizeof(uint64_t); | |
1813 | int error; | |
1814 | ||
1815 | memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); | |
1816 | ||
1817 | ht = kzalloc(size, GFP_KERNEL); | |
1818 | if (!ht) | |
1819 | return -ENOMEM; | |
1820 | ||
1821 | gfs2_alloc_get(dip); | |
1822 | ||
1823 | error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); | |
1824 | if (error) | |
1825 | goto out; | |
1826 | ||
1827 | error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh); | |
1828 | if (error) | |
1829 | goto out_qs; | |
1830 | ||
1831 | /* Count the number of leaves */ | |
1832 | ||
c752666c | 1833 | for (blk = leaf_no; blk; blk = nblk) { |
b3b94faa DT |
1834 | error = get_leaf(dip, blk, &bh); |
1835 | if (error) | |
1836 | goto out_rlist; | |
c752666c SW |
1837 | tmp_leaf = (struct gfs2_leaf *)bh->b_data; |
1838 | nblk = be64_to_cpu(tmp_leaf->lf_next); | |
b3b94faa DT |
1839 | brelse(bh); |
1840 | ||
1841 | gfs2_rlist_add(sdp, &rlist, blk); | |
1842 | l_blocks++; | |
1843 | } | |
1844 | ||
1845 | gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0); | |
1846 | ||
1847 | for (x = 0; x < rlist.rl_rgrps; x++) { | |
1848 | struct gfs2_rgrpd *rgd; | |
5c676f6d | 1849 | rgd = rlist.rl_ghs[x].gh_gl->gl_object; |
b3b94faa DT |
1850 | rg_blocks += rgd->rd_ri.ri_length; |
1851 | } | |
1852 | ||
1853 | error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs); | |
1854 | if (error) | |
1855 | goto out_rlist; | |
1856 | ||
1857 | error = gfs2_trans_begin(sdp, | |
5c676f6d | 1858 | rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) + |
b3b94faa DT |
1859 | RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks); |
1860 | if (error) | |
1861 | goto out_rg_gunlock; | |
1862 | ||
c752666c | 1863 | for (blk = leaf_no; blk; blk = nblk) { |
b3b94faa DT |
1864 | error = get_leaf(dip, blk, &bh); |
1865 | if (error) | |
1866 | goto out_end_trans; | |
c752666c SW |
1867 | tmp_leaf = (struct gfs2_leaf *)bh->b_data; |
1868 | nblk = be64_to_cpu(tmp_leaf->lf_next); | |
b3b94faa DT |
1869 | brelse(bh); |
1870 | ||
1871 | gfs2_free_meta(dip, blk, 1); | |
1872 | ||
1873 | if (!dip->i_di.di_blocks) | |
1874 | gfs2_consist_inode(dip); | |
1875 | dip->i_di.di_blocks--; | |
1876 | } | |
1877 | ||
e13940ba | 1878 | error = gfs2_dir_write_data(dip, ht, index * sizeof(uint64_t), size); |
b3b94faa DT |
1879 | if (error != size) { |
1880 | if (error >= 0) | |
1881 | error = -EIO; | |
1882 | goto out_end_trans; | |
1883 | } | |
1884 | ||
1885 | error = gfs2_meta_inode_buffer(dip, &dibh); | |
1886 | if (error) | |
1887 | goto out_end_trans; | |
1888 | ||
d4e9c4c3 | 1889 | gfs2_trans_add_bh(dip->i_gl, dibh, 1); |
b3b94faa DT |
1890 | gfs2_dinode_out(&dip->i_di, dibh->b_data); |
1891 | brelse(dibh); | |
1892 | ||
1893 | out_end_trans: | |
1894 | gfs2_trans_end(sdp); | |
1895 | ||
1896 | out_rg_gunlock: | |
1897 | gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); | |
1898 | ||
1899 | out_rlist: | |
1900 | gfs2_rlist_free(&rlist); | |
1901 | gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh); | |
1902 | ||
1903 | out_qs: | |
1904 | gfs2_quota_unhold(dip); | |
1905 | ||
1906 | out: | |
1907 | gfs2_alloc_put(dip); | |
1908 | kfree(ht); | |
1909 | ||
1910 | return error; | |
1911 | } | |
1912 | ||
1913 | /** | |
1914 | * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory | |
1915 | * @dip: the directory | |
1916 | * | |
1917 | * Dealloc all on-disk directory leaves to FREEMETA state | |
1918 | * Change on-disk inode type to "regular file" | |
1919 | * | |
1920 | * Returns: errno | |
1921 | */ | |
1922 | ||
1923 | int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip) | |
1924 | { | |
feaa7bba | 1925 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
b3b94faa DT |
1926 | struct buffer_head *bh; |
1927 | int error; | |
1928 | ||
1929 | /* Dealloc on-disk leaves to FREEMETA state */ | |
1930 | error = foreach_leaf(dip, leaf_dealloc, NULL); | |
1931 | if (error) | |
1932 | return error; | |
1933 | ||
1934 | /* Make this a regular file in case we crash. | |
1935 | (We don't want to free these blocks a second time.) */ | |
1936 | ||
1937 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); | |
1938 | if (error) | |
1939 | return error; | |
1940 | ||
1941 | error = gfs2_meta_inode_buffer(dip, &bh); | |
1942 | if (!error) { | |
d4e9c4c3 | 1943 | gfs2_trans_add_bh(dip->i_gl, bh, 1); |
568f4c96 SW |
1944 | ((struct gfs2_dinode *)bh->b_data)->di_mode = |
1945 | cpu_to_be32(S_IFREG); | |
b3b94faa DT |
1946 | brelse(bh); |
1947 | } | |
1948 | ||
1949 | gfs2_trans_end(sdp); | |
1950 | ||
1951 | return error; | |
1952 | } | |
1953 | ||
1954 | /** | |
1955 | * gfs2_diradd_alloc_required - find if adding entry will require an allocation | |
1956 | * @ip: the file being written to | |
1957 | * @filname: the filename that's going to be added | |
b3b94faa | 1958 | * |
c752666c | 1959 | * Returns: 1 if alloc required, 0 if not, -ve on error |
b3b94faa DT |
1960 | */ |
1961 | ||
4d8012b6 | 1962 | int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name) |
b3b94faa | 1963 | { |
c752666c SW |
1964 | struct gfs2_dirent *dent; |
1965 | struct buffer_head *bh; | |
b3b94faa | 1966 | |
c752666c | 1967 | dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh); |
ed386507 | 1968 | if (!dent) { |
c752666c | 1969 | return 1; |
ed386507 | 1970 | } |
c752666c SW |
1971 | if (IS_ERR(dent)) |
1972 | return PTR_ERR(dent); | |
1973 | brelse(bh); | |
1974 | return 0; | |
b3b94faa DT |
1975 | } |
1976 |