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