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