Commit | Line | Data |
---|---|---|
ccd979bd MF |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * dir.c | |
5 | * | |
6 | * Creates, reads, walks and deletes directory-nodes | |
7 | * | |
8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. | |
9 | * | |
10 | * Portions of this code from linux/fs/ext3/dir.c | |
11 | * | |
12 | * Copyright (C) 1992, 1993, 1994, 1995 | |
13 | * Remy Card (card@masi.ibp.fr) | |
14 | * Laboratoire MASI - Institut Blaise pascal | |
15 | * Universite Pierre et Marie Curie (Paris VI) | |
16 | * | |
17 | * from | |
18 | * | |
19 | * linux/fs/minix/dir.c | |
20 | * | |
762515a8 | 21 | * Copyright (C) 1991, 1992 Linus Torvalds |
ccd979bd MF |
22 | * |
23 | * This program is free software; you can redistribute it and/or | |
24 | * modify it under the terms of the GNU General Public | |
25 | * License as published by the Free Software Foundation; either | |
26 | * version 2 of the License, or (at your option) any later version. | |
27 | * | |
28 | * This program is distributed in the hope that it will be useful, | |
29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
31 | * General Public License for more details. | |
32 | * | |
33 | * You should have received a copy of the GNU General Public | |
34 | * License along with this program; if not, write to the | |
35 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
36 | * Boston, MA 021110-1307, USA. | |
37 | */ | |
38 | ||
39 | #include <linux/fs.h> | |
40 | #include <linux/types.h> | |
41 | #include <linux/slab.h> | |
42 | #include <linux/highmem.h> | |
a90714c1 | 43 | #include <linux/quotaops.h> |
9b7895ef | 44 | #include <linux/sort.h> |
ccd979bd | 45 | |
ccd979bd MF |
46 | #include <cluster/masklog.h> |
47 | ||
48 | #include "ocfs2.h" | |
49 | ||
50 | #include "alloc.h" | |
c175a518 | 51 | #include "blockcheck.h" |
ccd979bd MF |
52 | #include "dir.h" |
53 | #include "dlmglue.h" | |
54 | #include "extent_map.h" | |
55 | #include "file.h" | |
56 | #include "inode.h" | |
57 | #include "journal.h" | |
58 | #include "namei.h" | |
59 | #include "suballoc.h" | |
316f4b9f | 60 | #include "super.h" |
9b7895ef | 61 | #include "sysfile.h" |
ccd979bd | 62 | #include "uptodate.h" |
f1088d47 | 63 | #include "ocfs2_trace.h" |
ccd979bd MF |
64 | |
65 | #include "buffer_head_io.h" | |
66 | ||
316f4b9f MF |
67 | #define NAMEI_RA_CHUNKS 2 |
68 | #define NAMEI_RA_BLOCKS 4 | |
69 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) | |
316f4b9f | 70 | |
ccd979bd MF |
71 | static unsigned char ocfs2_filetype_table[] = { |
72 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | |
73 | }; | |
74 | ||
316f4b9f MF |
75 | static int ocfs2_do_extend_dir(struct super_block *sb, |
76 | handle_t *handle, | |
77 | struct inode *dir, | |
78 | struct buffer_head *parent_fe_bh, | |
79 | struct ocfs2_alloc_context *data_ac, | |
80 | struct ocfs2_alloc_context *meta_ac, | |
81 | struct buffer_head **new_bh); | |
e7c17e43 | 82 | static int ocfs2_dir_indexed(struct inode *inode); |
316f4b9f | 83 | |
87d35a74 MF |
84 | /* |
85 | * These are distinct checks because future versions of the file system will | |
86 | * want to have a trailing dirent structure independent of indexing. | |
87 | */ | |
e7c17e43 | 88 | static int ocfs2_supports_dir_trailer(struct inode *dir) |
87d35a74 | 89 | { |
e7c17e43 MF |
90 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
91 | ||
87d35a74 MF |
92 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
93 | return 0; | |
94 | ||
e7c17e43 | 95 | return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir); |
87d35a74 MF |
96 | } |
97 | ||
e7c17e43 MF |
98 | /* |
99 | * "new' here refers to the point at which we're creating a new | |
100 | * directory via "mkdir()", but also when we're expanding an inline | |
101 | * directory. In either case, we don't yet have the indexing bit set | |
102 | * on the directory, so the standard checks will fail in when metaecc | |
103 | * is turned off. Only directory-initialization type functions should | |
104 | * use this then. Everything else wants ocfs2_supports_dir_trailer() | |
105 | */ | |
106 | static int ocfs2_new_dir_wants_trailer(struct inode *dir) | |
87d35a74 | 107 | { |
e7c17e43 MF |
108 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
109 | ||
110 | return ocfs2_meta_ecc(osb) || | |
111 | ocfs2_supports_indexed_dirs(osb); | |
87d35a74 MF |
112 | } |
113 | ||
114 | static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb) | |
115 | { | |
116 | return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer); | |
117 | } | |
118 | ||
119 | #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb)))) | |
120 | ||
c175a518 JB |
121 | /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make |
122 | * them more consistent? */ | |
123 | struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize, | |
124 | void *data) | |
125 | { | |
126 | char *p = data; | |
127 | ||
128 | p += blocksize - sizeof(struct ocfs2_dir_block_trailer); | |
129 | return (struct ocfs2_dir_block_trailer *)p; | |
130 | } | |
131 | ||
87d35a74 MF |
132 | /* |
133 | * XXX: This is executed once on every dirent. We should consider optimizing | |
134 | * it. | |
135 | */ | |
136 | static int ocfs2_skip_dir_trailer(struct inode *dir, | |
137 | struct ocfs2_dir_entry *de, | |
138 | unsigned long offset, | |
139 | unsigned long blklen) | |
140 | { | |
141 | unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer); | |
142 | ||
e7c17e43 | 143 | if (!ocfs2_supports_dir_trailer(dir)) |
87d35a74 MF |
144 | return 0; |
145 | ||
146 | if (offset != toff) | |
147 | return 0; | |
148 | ||
149 | return 1; | |
150 | } | |
151 | ||
152 | static void ocfs2_init_dir_trailer(struct inode *inode, | |
e7c17e43 | 153 | struct buffer_head *bh, u16 rec_len) |
87d35a74 MF |
154 | { |
155 | struct ocfs2_dir_block_trailer *trailer; | |
156 | ||
157 | trailer = ocfs2_trailer_from_bh(bh, inode->i_sb); | |
158 | strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE); | |
159 | trailer->db_compat_rec_len = | |
160 | cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer)); | |
161 | trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); | |
162 | trailer->db_blkno = cpu_to_le64(bh->b_blocknr); | |
e7c17e43 MF |
163 | trailer->db_free_rec_len = cpu_to_le16(rec_len); |
164 | } | |
165 | /* | |
166 | * Link an unindexed block with a dir trailer structure into the index free | |
167 | * list. This function will modify dirdata_bh, but assumes you've already | |
168 | * passed it to the journal. | |
169 | */ | |
170 | static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle, | |
171 | struct buffer_head *dx_root_bh, | |
172 | struct buffer_head *dirdata_bh) | |
173 | { | |
174 | int ret; | |
175 | struct ocfs2_dx_root_block *dx_root; | |
176 | struct ocfs2_dir_block_trailer *trailer; | |
177 | ||
0cf2f763 | 178 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
e7c17e43 MF |
179 | OCFS2_JOURNAL_ACCESS_WRITE); |
180 | if (ret) { | |
181 | mlog_errno(ret); | |
182 | goto out; | |
183 | } | |
184 | trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb); | |
185 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; | |
186 | ||
187 | trailer->db_free_next = dx_root->dr_free_blk; | |
188 | dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr); | |
189 | ||
190 | ocfs2_journal_dirty(handle, dx_root_bh); | |
191 | ||
192 | out: | |
193 | return ret; | |
194 | } | |
195 | ||
196 | static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res) | |
197 | { | |
198 | return res->dl_prev_leaf_bh == NULL; | |
87d35a74 MF |
199 | } |
200 | ||
4a12ca3a MF |
201 | void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res) |
202 | { | |
4ed8a6bb | 203 | brelse(res->dl_dx_root_bh); |
4a12ca3a | 204 | brelse(res->dl_leaf_bh); |
9b7895ef | 205 | brelse(res->dl_dx_leaf_bh); |
e7c17e43 | 206 | brelse(res->dl_prev_leaf_bh); |
9b7895ef MF |
207 | } |
208 | ||
209 | static int ocfs2_dir_indexed(struct inode *inode) | |
210 | { | |
211 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL) | |
212 | return 1; | |
213 | return 0; | |
214 | } | |
215 | ||
4ed8a6bb MF |
216 | static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root) |
217 | { | |
218 | return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE; | |
219 | } | |
220 | ||
9b7895ef MF |
221 | /* |
222 | * Hashing code adapted from ext3 | |
223 | */ | |
224 | #define DELTA 0x9E3779B9 | |
225 | ||
226 | static void TEA_transform(__u32 buf[4], __u32 const in[]) | |
227 | { | |
228 | __u32 sum = 0; | |
229 | __u32 b0 = buf[0], b1 = buf[1]; | |
230 | __u32 a = in[0], b = in[1], c = in[2], d = in[3]; | |
231 | int n = 16; | |
232 | ||
233 | do { | |
234 | sum += DELTA; | |
235 | b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); | |
236 | b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); | |
237 | } while (--n); | |
238 | ||
239 | buf[0] += b0; | |
240 | buf[1] += b1; | |
241 | } | |
242 | ||
243 | static void str2hashbuf(const char *msg, int len, __u32 *buf, int num) | |
244 | { | |
245 | __u32 pad, val; | |
246 | int i; | |
247 | ||
248 | pad = (__u32)len | ((__u32)len << 8); | |
249 | pad |= pad << 16; | |
250 | ||
251 | val = pad; | |
252 | if (len > num*4) | |
253 | len = num * 4; | |
254 | for (i = 0; i < len; i++) { | |
255 | if ((i % 4) == 0) | |
256 | val = pad; | |
257 | val = msg[i] + (val << 8); | |
258 | if ((i % 4) == 3) { | |
259 | *buf++ = val; | |
260 | val = pad; | |
261 | num--; | |
262 | } | |
263 | } | |
264 | if (--num >= 0) | |
265 | *buf++ = val; | |
266 | while (--num >= 0) | |
267 | *buf++ = pad; | |
268 | } | |
269 | ||
270 | static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len, | |
271 | struct ocfs2_dx_hinfo *hinfo) | |
272 | { | |
273 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); | |
274 | const char *p; | |
275 | __u32 in[8], buf[4]; | |
276 | ||
277 | /* | |
278 | * XXX: Is this really necessary, if the index is never looked | |
279 | * at by readdir? Is a hash value of '0' a bad idea? | |
280 | */ | |
281 | if ((len == 1 && !strncmp(".", name, 1)) || | |
282 | (len == 2 && !strncmp("..", name, 2))) { | |
283 | buf[0] = buf[1] = 0; | |
284 | goto out; | |
285 | } | |
286 | ||
287 | #ifdef OCFS2_DEBUG_DX_DIRS | |
288 | /* | |
289 | * This makes it very easy to debug indexing problems. We | |
290 | * should never allow this to be selected without hand editing | |
291 | * this file though. | |
292 | */ | |
293 | buf[0] = buf[1] = len; | |
294 | goto out; | |
295 | #endif | |
296 | ||
297 | memcpy(buf, osb->osb_dx_seed, sizeof(buf)); | |
298 | ||
299 | p = name; | |
300 | while (len > 0) { | |
301 | str2hashbuf(p, len, in, 4); | |
302 | TEA_transform(buf, in); | |
303 | len -= 16; | |
304 | p += 16; | |
305 | } | |
306 | ||
307 | out: | |
308 | hinfo->major_hash = buf[0]; | |
309 | hinfo->minor_hash = buf[1]; | |
4a12ca3a MF |
310 | } |
311 | ||
23193e51 MF |
312 | /* |
313 | * bh passed here can be an inode block or a dir data block, depending | |
314 | * on the inode inline data flag. | |
315 | */ | |
5eae5b96 MF |
316 | static int ocfs2_check_dir_entry(struct inode * dir, |
317 | struct ocfs2_dir_entry * de, | |
318 | struct buffer_head * bh, | |
319 | unsigned long offset) | |
316f4b9f MF |
320 | { |
321 | const char *error_msg = NULL; | |
322 | const int rlen = le16_to_cpu(de->rec_len); | |
323 | ||
1dd9ffc8 | 324 | if (unlikely(rlen < OCFS2_DIR_REC_LEN(1))) |
316f4b9f | 325 | error_msg = "rec_len is smaller than minimal"; |
1dd9ffc8 | 326 | else if (unlikely(rlen % 4 != 0)) |
316f4b9f | 327 | error_msg = "rec_len % 4 != 0"; |
1dd9ffc8 | 328 | else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len))) |
316f4b9f | 329 | error_msg = "rec_len is too small for name_len"; |
1dd9ffc8 TM |
330 | else if (unlikely( |
331 | ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)) | |
316f4b9f MF |
332 | error_msg = "directory entry across blocks"; |
333 | ||
1dd9ffc8 | 334 | if (unlikely(error_msg != NULL)) |
316f4b9f MF |
335 | mlog(ML_ERROR, "bad entry in directory #%llu: %s - " |
336 | "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n", | |
337 | (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg, | |
338 | offset, (unsigned long long)le64_to_cpu(de->inode), rlen, | |
339 | de->name_len); | |
1dd9ffc8 | 340 | |
316f4b9f MF |
341 | return error_msg == NULL ? 1 : 0; |
342 | } | |
343 | ||
344 | static inline int ocfs2_match(int len, | |
345 | const char * const name, | |
346 | struct ocfs2_dir_entry *de) | |
347 | { | |
348 | if (len != de->name_len) | |
349 | return 0; | |
350 | if (!de->inode) | |
351 | return 0; | |
352 | return !memcmp(name, de->name, len); | |
353 | } | |
354 | ||
355 | /* | |
356 | * Returns 0 if not found, -1 on failure, and 1 on success | |
357 | */ | |
42b16b3f | 358 | static inline int ocfs2_search_dirblock(struct buffer_head *bh, |
316f4b9f MF |
359 | struct inode *dir, |
360 | const char *name, int namelen, | |
361 | unsigned long offset, | |
23193e51 MF |
362 | char *first_de, |
363 | unsigned int bytes, | |
316f4b9f MF |
364 | struct ocfs2_dir_entry **res_dir) |
365 | { | |
366 | struct ocfs2_dir_entry *de; | |
367 | char *dlimit, *de_buf; | |
368 | int de_len; | |
369 | int ret = 0; | |
370 | ||
23193e51 MF |
371 | de_buf = first_de; |
372 | dlimit = de_buf + bytes; | |
316f4b9f MF |
373 | |
374 | while (de_buf < dlimit) { | |
375 | /* this code is executed quadratically often */ | |
376 | /* do minimal checking `by hand' */ | |
377 | ||
378 | de = (struct ocfs2_dir_entry *) de_buf; | |
379 | ||
380 | if (de_buf + namelen <= dlimit && | |
381 | ocfs2_match(namelen, name, de)) { | |
382 | /* found a match - just to be sure, do a full check */ | |
383 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { | |
384 | ret = -1; | |
385 | goto bail; | |
386 | } | |
387 | *res_dir = de; | |
388 | ret = 1; | |
389 | goto bail; | |
390 | } | |
391 | ||
392 | /* prevent looping on a bad block */ | |
393 | de_len = le16_to_cpu(de->rec_len); | |
394 | if (de_len <= 0) { | |
395 | ret = -1; | |
396 | goto bail; | |
397 | } | |
398 | ||
399 | de_buf += de_len; | |
400 | offset += de_len; | |
401 | } | |
402 | ||
403 | bail: | |
f1088d47 | 404 | trace_ocfs2_search_dirblock(ret); |
316f4b9f MF |
405 | return ret; |
406 | } | |
407 | ||
23193e51 MF |
408 | static struct buffer_head *ocfs2_find_entry_id(const char *name, |
409 | int namelen, | |
410 | struct inode *dir, | |
411 | struct ocfs2_dir_entry **res_dir) | |
412 | { | |
413 | int ret, found; | |
414 | struct buffer_head *di_bh = NULL; | |
415 | struct ocfs2_dinode *di; | |
416 | struct ocfs2_inline_data *data; | |
417 | ||
b657c95c | 418 | ret = ocfs2_read_inode_block(dir, &di_bh); |
23193e51 MF |
419 | if (ret) { |
420 | mlog_errno(ret); | |
421 | goto out; | |
422 | } | |
423 | ||
424 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
425 | data = &di->id2.i_data; | |
426 | ||
427 | found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0, | |
428 | data->id_data, i_size_read(dir), res_dir); | |
429 | if (found == 1) | |
430 | return di_bh; | |
431 | ||
432 | brelse(di_bh); | |
433 | out: | |
434 | return NULL; | |
435 | } | |
436 | ||
a22305cc JB |
437 | static int ocfs2_validate_dir_block(struct super_block *sb, |
438 | struct buffer_head *bh) | |
439 | { | |
c175a518 JB |
440 | int rc; |
441 | struct ocfs2_dir_block_trailer *trailer = | |
442 | ocfs2_trailer_from_bh(bh, sb); | |
443 | ||
444 | ||
a22305cc | 445 | /* |
c175a518 | 446 | * We don't validate dirents here, that's handled |
a22305cc JB |
447 | * in-place when the code walks them. |
448 | */ | |
f1088d47 | 449 | trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr); |
a22305cc | 450 | |
c175a518 JB |
451 | BUG_ON(!buffer_uptodate(bh)); |
452 | ||
453 | /* | |
454 | * If the ecc fails, we return the error but otherwise | |
455 | * leave the filesystem running. We know any error is | |
456 | * local to this block. | |
457 | * | |
458 | * Note that we are safe to call this even if the directory | |
459 | * doesn't have a trailer. Filesystems without metaecc will do | |
460 | * nothing, and filesystems with it will have one. | |
461 | */ | |
462 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check); | |
463 | if (rc) | |
464 | mlog(ML_ERROR, "Checksum failed for dinode %llu\n", | |
465 | (unsigned long long)bh->b_blocknr); | |
466 | ||
467 | return rc; | |
a22305cc JB |
468 | } |
469 | ||
9b7895ef MF |
470 | /* |
471 | * Validate a directory trailer. | |
472 | * | |
473 | * We check the trailer here rather than in ocfs2_validate_dir_block() | |
474 | * because that function doesn't have the inode to test. | |
475 | */ | |
476 | static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh) | |
477 | { | |
478 | int rc = 0; | |
479 | struct ocfs2_dir_block_trailer *trailer; | |
480 | ||
481 | trailer = ocfs2_trailer_from_bh(bh, dir->i_sb); | |
482 | if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) { | |
17a5b9ab | 483 | rc = ocfs2_error(dir->i_sb, |
9b7895ef MF |
484 | "Invalid dirblock #%llu: " |
485 | "signature = %.*s\n", | |
486 | (unsigned long long)bh->b_blocknr, 7, | |
487 | trailer->db_signature); | |
488 | goto out; | |
489 | } | |
490 | if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) { | |
17a5b9ab | 491 | rc = ocfs2_error(dir->i_sb, |
9b7895ef MF |
492 | "Directory block #%llu has an invalid " |
493 | "db_blkno of %llu", | |
494 | (unsigned long long)bh->b_blocknr, | |
495 | (unsigned long long)le64_to_cpu(trailer->db_blkno)); | |
496 | goto out; | |
497 | } | |
498 | if (le64_to_cpu(trailer->db_parent_dinode) != | |
499 | OCFS2_I(dir)->ip_blkno) { | |
17a5b9ab | 500 | rc = ocfs2_error(dir->i_sb, |
9b7895ef MF |
501 | "Directory block #%llu on dinode " |
502 | "#%llu has an invalid parent_dinode " | |
503 | "of %llu", | |
504 | (unsigned long long)bh->b_blocknr, | |
505 | (unsigned long long)OCFS2_I(dir)->ip_blkno, | |
506 | (unsigned long long)le64_to_cpu(trailer->db_blkno)); | |
507 | goto out; | |
508 | } | |
509 | out: | |
510 | return rc; | |
511 | } | |
512 | ||
a22305cc JB |
513 | /* |
514 | * This function forces all errors to -EIO for consistency with its | |
515 | * predecessor, ocfs2_bread(). We haven't audited what returning the | |
516 | * real error codes would do to callers. We log the real codes with | |
517 | * mlog_errno() before we squash them. | |
518 | */ | |
519 | static int ocfs2_read_dir_block(struct inode *inode, u64 v_block, | |
520 | struct buffer_head **bh, int flags) | |
521 | { | |
522 | int rc = 0; | |
523 | struct buffer_head *tmp = *bh; | |
a22305cc | 524 | |
511308d9 JB |
525 | rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags, |
526 | ocfs2_validate_dir_block); | |
87d35a74 | 527 | if (rc) { |
a22305cc | 528 | mlog_errno(rc); |
87d35a74 MF |
529 | goto out; |
530 | } | |
531 | ||
87d35a74 | 532 | if (!(flags & OCFS2_BH_READAHEAD) && |
e7c17e43 | 533 | ocfs2_supports_dir_trailer(inode)) { |
9b7895ef MF |
534 | rc = ocfs2_check_dir_trailer(inode, tmp); |
535 | if (rc) { | |
536 | if (!*bh) | |
537 | brelse(tmp); | |
538 | mlog_errno(rc); | |
87d35a74 MF |
539 | goto out; |
540 | } | |
541 | } | |
a22305cc | 542 | |
511308d9 | 543 | /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */ |
87d35a74 | 544 | if (!*bh) |
a22305cc JB |
545 | *bh = tmp; |
546 | ||
87d35a74 | 547 | out: |
a22305cc JB |
548 | return rc ? -EIO : 0; |
549 | } | |
550 | ||
9b7895ef MF |
551 | /* |
552 | * Read the block at 'phys' which belongs to this directory | |
553 | * inode. This function does no virtual->physical block translation - | |
554 | * what's passed in is assumed to be a valid directory block. | |
555 | */ | |
556 | static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys, | |
557 | struct buffer_head **bh) | |
558 | { | |
559 | int ret; | |
560 | struct buffer_head *tmp = *bh; | |
561 | ||
8cb471e8 JB |
562 | ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp, |
563 | ocfs2_validate_dir_block); | |
9b7895ef MF |
564 | if (ret) { |
565 | mlog_errno(ret); | |
566 | goto out; | |
567 | } | |
568 | ||
569 | if (ocfs2_supports_dir_trailer(dir)) { | |
570 | ret = ocfs2_check_dir_trailer(dir, tmp); | |
571 | if (ret) { | |
572 | if (!*bh) | |
573 | brelse(tmp); | |
574 | mlog_errno(ret); | |
575 | goto out; | |
576 | } | |
577 | } | |
578 | ||
579 | if (!ret && !*bh) | |
580 | *bh = tmp; | |
581 | out: | |
582 | return ret; | |
583 | } | |
584 | ||
585 | static int ocfs2_validate_dx_root(struct super_block *sb, | |
586 | struct buffer_head *bh) | |
587 | { | |
588 | int ret; | |
589 | struct ocfs2_dx_root_block *dx_root; | |
590 | ||
591 | BUG_ON(!buffer_uptodate(bh)); | |
592 | ||
593 | dx_root = (struct ocfs2_dx_root_block *) bh->b_data; | |
594 | ||
595 | ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check); | |
596 | if (ret) { | |
597 | mlog(ML_ERROR, | |
598 | "Checksum failed for dir index root block %llu\n", | |
599 | (unsigned long long)bh->b_blocknr); | |
600 | return ret; | |
601 | } | |
602 | ||
603 | if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) { | |
17a5b9ab | 604 | ret = ocfs2_error(sb, |
9b7895ef MF |
605 | "Dir Index Root # %llu has bad signature %.*s", |
606 | (unsigned long long)le64_to_cpu(dx_root->dr_blkno), | |
607 | 7, dx_root->dr_signature); | |
9b7895ef MF |
608 | } |
609 | ||
17a5b9ab | 610 | return ret; |
9b7895ef MF |
611 | } |
612 | ||
613 | static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di, | |
614 | struct buffer_head **dx_root_bh) | |
615 | { | |
616 | int ret; | |
617 | u64 blkno = le64_to_cpu(di->i_dx_root); | |
618 | struct buffer_head *tmp = *dx_root_bh; | |
619 | ||
8cb471e8 JB |
620 | ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp, |
621 | ocfs2_validate_dx_root); | |
9b7895ef MF |
622 | |
623 | /* If ocfs2_read_block() got us a new bh, pass it up. */ | |
624 | if (!ret && !*dx_root_bh) | |
625 | *dx_root_bh = tmp; | |
626 | ||
627 | return ret; | |
628 | } | |
629 | ||
630 | static int ocfs2_validate_dx_leaf(struct super_block *sb, | |
631 | struct buffer_head *bh) | |
632 | { | |
633 | int ret; | |
634 | struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data; | |
635 | ||
636 | BUG_ON(!buffer_uptodate(bh)); | |
637 | ||
638 | ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check); | |
639 | if (ret) { | |
640 | mlog(ML_ERROR, | |
641 | "Checksum failed for dir index leaf block %llu\n", | |
642 | (unsigned long long)bh->b_blocknr); | |
643 | return ret; | |
644 | } | |
645 | ||
646 | if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) { | |
17a5b9ab | 647 | ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s", |
9b7895ef | 648 | 7, dx_leaf->dl_signature); |
9b7895ef MF |
649 | } |
650 | ||
17a5b9ab | 651 | return ret; |
9b7895ef MF |
652 | } |
653 | ||
654 | static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno, | |
655 | struct buffer_head **dx_leaf_bh) | |
656 | { | |
657 | int ret; | |
658 | struct buffer_head *tmp = *dx_leaf_bh; | |
659 | ||
8cb471e8 JB |
660 | ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp, |
661 | ocfs2_validate_dx_leaf); | |
9b7895ef MF |
662 | |
663 | /* If ocfs2_read_block() got us a new bh, pass it up. */ | |
664 | if (!ret && !*dx_leaf_bh) | |
665 | *dx_leaf_bh = tmp; | |
666 | ||
667 | return ret; | |
668 | } | |
669 | ||
670 | /* | |
671 | * Read a series of dx_leaf blocks. This expects all buffer_head | |
672 | * pointers to be NULL on function entry. | |
673 | */ | |
674 | static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num, | |
675 | struct buffer_head **dx_leaf_bhs) | |
676 | { | |
677 | int ret; | |
678 | ||
8cb471e8 | 679 | ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0, |
9b7895ef MF |
680 | ocfs2_validate_dx_leaf); |
681 | if (ret) | |
682 | mlog_errno(ret); | |
683 | ||
684 | return ret; | |
685 | } | |
686 | ||
0af4bd38 AB |
687 | static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen, |
688 | struct inode *dir, | |
689 | struct ocfs2_dir_entry **res_dir) | |
316f4b9f MF |
690 | { |
691 | struct super_block *sb; | |
692 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; | |
693 | struct buffer_head *bh, *ret = NULL; | |
694 | unsigned long start, block, b; | |
695 | int ra_max = 0; /* Number of bh's in the readahead | |
696 | buffer, bh_use[] */ | |
697 | int ra_ptr = 0; /* Current index into readahead | |
698 | buffer */ | |
699 | int num = 0; | |
700 | int nblocks, i, err; | |
701 | ||
316f4b9f MF |
702 | sb = dir->i_sb; |
703 | ||
704 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; | |
705 | start = OCFS2_I(dir)->ip_dir_start_lookup; | |
706 | if (start >= nblocks) | |
707 | start = 0; | |
708 | block = start; | |
709 | ||
710 | restart: | |
711 | do { | |
712 | /* | |
713 | * We deal with the read-ahead logic here. | |
714 | */ | |
715 | if (ra_ptr >= ra_max) { | |
716 | /* Refill the readahead buffer */ | |
717 | ra_ptr = 0; | |
718 | b = block; | |
719 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { | |
720 | /* | |
721 | * Terminate if we reach the end of the | |
722 | * directory and must wrap, or if our | |
723 | * search has finished at this block. | |
724 | */ | |
725 | if (b >= nblocks || (num && block == start)) { | |
726 | bh_use[ra_max] = NULL; | |
727 | break; | |
728 | } | |
729 | num++; | |
730 | ||
a22305cc JB |
731 | bh = NULL; |
732 | err = ocfs2_read_dir_block(dir, b++, &bh, | |
733 | OCFS2_BH_READAHEAD); | |
316f4b9f MF |
734 | bh_use[ra_max] = bh; |
735 | } | |
736 | } | |
737 | if ((bh = bh_use[ra_ptr++]) == NULL) | |
738 | goto next; | |
a22305cc | 739 | if (ocfs2_read_dir_block(dir, block, &bh, 0)) { |
5e0b3dec | 740 | /* read error, skip block & hope for the best. |
a22305cc | 741 | * ocfs2_read_dir_block() has released the bh. */ |
61fb9ea4 | 742 | mlog(ML_ERROR, "reading directory %llu, " |
316f4b9f MF |
743 | "offset %lu\n", |
744 | (unsigned long long)OCFS2_I(dir)->ip_blkno, | |
745 | block); | |
316f4b9f MF |
746 | goto next; |
747 | } | |
748 | i = ocfs2_search_dirblock(bh, dir, name, namelen, | |
749 | block << sb->s_blocksize_bits, | |
23193e51 | 750 | bh->b_data, sb->s_blocksize, |
316f4b9f MF |
751 | res_dir); |
752 | if (i == 1) { | |
753 | OCFS2_I(dir)->ip_dir_start_lookup = block; | |
754 | ret = bh; | |
755 | goto cleanup_and_exit; | |
756 | } else { | |
757 | brelse(bh); | |
758 | if (i < 0) | |
759 | goto cleanup_and_exit; | |
760 | } | |
761 | next: | |
762 | if (++block >= nblocks) | |
763 | block = 0; | |
764 | } while (block != start); | |
765 | ||
766 | /* | |
767 | * If the directory has grown while we were searching, then | |
768 | * search the last part of the directory before giving up. | |
769 | */ | |
770 | block = nblocks; | |
771 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; | |
772 | if (block < nblocks) { | |
773 | start = 0; | |
774 | goto restart; | |
775 | } | |
776 | ||
777 | cleanup_and_exit: | |
778 | /* Clean up the read-ahead blocks */ | |
779 | for (; ra_ptr < ra_max; ra_ptr++) | |
780 | brelse(bh_use[ra_ptr]); | |
781 | ||
f1088d47 | 782 | trace_ocfs2_find_entry_el(ret); |
316f4b9f MF |
783 | return ret; |
784 | } | |
785 | ||
9b7895ef MF |
786 | static int ocfs2_dx_dir_lookup_rec(struct inode *inode, |
787 | struct ocfs2_extent_list *el, | |
788 | u32 major_hash, | |
789 | u32 *ret_cpos, | |
790 | u64 *ret_phys_blkno, | |
791 | unsigned int *ret_clen) | |
23193e51 | 792 | { |
9b7895ef MF |
793 | int ret = 0, i, found; |
794 | struct buffer_head *eb_bh = NULL; | |
795 | struct ocfs2_extent_block *eb; | |
796 | struct ocfs2_extent_rec *rec = NULL; | |
23193e51 | 797 | |
9b7895ef | 798 | if (el->l_tree_depth) { |
facdb77f JB |
799 | ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash, |
800 | &eb_bh); | |
9b7895ef MF |
801 | if (ret) { |
802 | mlog_errno(ret); | |
803 | goto out; | |
804 | } | |
23193e51 | 805 | |
9b7895ef MF |
806 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
807 | el = &eb->h_list; | |
4a12ca3a | 808 | |
9b7895ef | 809 | if (el->l_tree_depth) { |
17a5b9ab | 810 | ret = ocfs2_error(inode->i_sb, |
9b7895ef MF |
811 | "Inode %lu has non zero tree depth in " |
812 | "btree tree block %llu\n", inode->i_ino, | |
813 | (unsigned long long)eb_bh->b_blocknr); | |
9b7895ef MF |
814 | goto out; |
815 | } | |
816 | } | |
817 | ||
818 | found = 0; | |
819 | for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { | |
820 | rec = &el->l_recs[i]; | |
821 | ||
822 | if (le32_to_cpu(rec->e_cpos) <= major_hash) { | |
823 | found = 1; | |
824 | break; | |
825 | } | |
826 | } | |
827 | ||
828 | if (!found) { | |
17a5b9ab | 829 | ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent " |
9b7895ef MF |
830 | "record (%u, %u, 0) in btree", inode->i_ino, |
831 | le32_to_cpu(rec->e_cpos), | |
832 | ocfs2_rec_clusters(el, rec)); | |
9b7895ef MF |
833 | goto out; |
834 | } | |
835 | ||
836 | if (ret_phys_blkno) | |
837 | *ret_phys_blkno = le64_to_cpu(rec->e_blkno); | |
838 | if (ret_cpos) | |
839 | *ret_cpos = le32_to_cpu(rec->e_cpos); | |
840 | if (ret_clen) | |
841 | *ret_clen = le16_to_cpu(rec->e_leaf_clusters); | |
842 | ||
843 | out: | |
844 | brelse(eb_bh); | |
845 | return ret; | |
23193e51 MF |
846 | } |
847 | ||
5b6a3a2b | 848 | /* |
9b7895ef MF |
849 | * Returns the block index, from the start of the cluster which this |
850 | * hash belongs too. | |
5b6a3a2b | 851 | */ |
4ed8a6bb MF |
852 | static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb, |
853 | u32 minor_hash) | |
38760e24 | 854 | { |
9b7895ef MF |
855 | return minor_hash & osb->osb_dx_mask; |
856 | } | |
5b6a3a2b | 857 | |
4ed8a6bb MF |
858 | static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb, |
859 | struct ocfs2_dx_hinfo *hinfo) | |
860 | { | |
861 | return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash); | |
862 | } | |
863 | ||
9b7895ef MF |
864 | static int ocfs2_dx_dir_lookup(struct inode *inode, |
865 | struct ocfs2_extent_list *el, | |
866 | struct ocfs2_dx_hinfo *hinfo, | |
867 | u32 *ret_cpos, | |
868 | u64 *ret_phys_blkno) | |
869 | { | |
870 | int ret = 0; | |
871 | unsigned int cend, uninitialized_var(clen); | |
872 | u32 uninitialized_var(cpos); | |
873 | u64 uninitialized_var(blkno); | |
874 | u32 name_hash = hinfo->major_hash; | |
13723d00 | 875 | |
9b7895ef MF |
876 | ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno, |
877 | &clen); | |
38760e24 MF |
878 | if (ret) { |
879 | mlog_errno(ret); | |
880 | goto out; | |
881 | } | |
882 | ||
9b7895ef MF |
883 | cend = cpos + clen; |
884 | if (name_hash >= cend) { | |
885 | /* We want the last cluster */ | |
886 | blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1); | |
887 | cpos += clen - 1; | |
888 | } else { | |
889 | blkno += ocfs2_clusters_to_blocks(inode->i_sb, | |
890 | name_hash - cpos); | |
891 | cpos = name_hash; | |
892 | } | |
38760e24 | 893 | |
9b7895ef MF |
894 | /* |
895 | * We now have the cluster which should hold our entry. To | |
896 | * find the exact block from the start of the cluster to | |
897 | * search, we take the lower bits of the hash. | |
898 | */ | |
899 | blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo); | |
900 | ||
901 | if (ret_phys_blkno) | |
902 | *ret_phys_blkno = blkno; | |
903 | if (ret_cpos) | |
904 | *ret_cpos = cpos; | |
38760e24 MF |
905 | |
906 | out: | |
9b7895ef | 907 | |
38760e24 MF |
908 | return ret; |
909 | } | |
910 | ||
9b7895ef MF |
911 | static int ocfs2_dx_dir_search(const char *name, int namelen, |
912 | struct inode *dir, | |
4ed8a6bb | 913 | struct ocfs2_dx_root_block *dx_root, |
9b7895ef | 914 | struct ocfs2_dir_lookup_result *res) |
316f4b9f | 915 | { |
9b7895ef MF |
916 | int ret, i, found; |
917 | u64 uninitialized_var(phys); | |
918 | struct buffer_head *dx_leaf_bh = NULL; | |
919 | struct ocfs2_dx_leaf *dx_leaf; | |
920 | struct ocfs2_dx_entry *dx_entry = NULL; | |
921 | struct buffer_head *dir_ent_bh = NULL; | |
922 | struct ocfs2_dir_entry *dir_ent = NULL; | |
923 | struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo; | |
4ed8a6bb MF |
924 | struct ocfs2_extent_list *dr_el; |
925 | struct ocfs2_dx_entry_list *entry_list; | |
9b7895ef MF |
926 | |
927 | ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo); | |
928 | ||
4ed8a6bb MF |
929 | if (ocfs2_dx_root_inline(dx_root)) { |
930 | entry_list = &dx_root->dr_entries; | |
931 | goto search; | |
932 | } | |
933 | ||
934 | dr_el = &dx_root->dr_list; | |
935 | ||
9b7895ef MF |
936 | ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys); |
937 | if (ret) { | |
938 | mlog_errno(ret); | |
939 | goto out; | |
940 | } | |
316f4b9f | 941 | |
f1088d47 TM |
942 | trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno, |
943 | namelen, name, hinfo->major_hash, | |
944 | hinfo->minor_hash, (unsigned long long)phys); | |
316f4b9f | 945 | |
9b7895ef MF |
946 | ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh); |
947 | if (ret) { | |
948 | mlog_errno(ret); | |
949 | goto out; | |
950 | } | |
13723d00 | 951 | |
9b7895ef MF |
952 | dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data; |
953 | ||
f1088d47 TM |
954 | trace_ocfs2_dx_dir_search_leaf_info( |
955 | le16_to_cpu(dx_leaf->dl_list.de_num_used), | |
956 | le16_to_cpu(dx_leaf->dl_list.de_count)); | |
9b7895ef | 957 | |
4ed8a6bb MF |
958 | entry_list = &dx_leaf->dl_list; |
959 | ||
960 | search: | |
9b7895ef MF |
961 | /* |
962 | * Empty leaf is legal, so no need to check for that. | |
963 | */ | |
964 | found = 0; | |
4ed8a6bb MF |
965 | for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) { |
966 | dx_entry = &entry_list->de_entries[i]; | |
9b7895ef MF |
967 | |
968 | if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash) | |
969 | || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash)) | |
970 | continue; | |
971 | ||
972 | /* | |
973 | * Search unindexed leaf block now. We're not | |
974 | * guaranteed to find anything. | |
975 | */ | |
976 | ret = ocfs2_read_dir_block_direct(dir, | |
977 | le64_to_cpu(dx_entry->dx_dirent_blk), | |
978 | &dir_ent_bh); | |
979 | if (ret) { | |
980 | mlog_errno(ret); | |
981 | goto out; | |
316f4b9f | 982 | } |
9b7895ef MF |
983 | |
984 | /* | |
985 | * XXX: We should check the unindexed block here, | |
986 | * before using it. | |
987 | */ | |
988 | ||
989 | found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen, | |
990 | 0, dir_ent_bh->b_data, | |
991 | dir->i_sb->s_blocksize, &dir_ent); | |
992 | if (found == 1) | |
993 | break; | |
994 | ||
995 | if (found == -1) { | |
996 | /* This means we found a bad directory entry. */ | |
997 | ret = -EIO; | |
998 | mlog_errno(ret); | |
999 | goto out; | |
1000 | } | |
1001 | ||
1002 | brelse(dir_ent_bh); | |
1003 | dir_ent_bh = NULL; | |
1004 | } | |
1005 | ||
1006 | if (found <= 0) { | |
1007 | ret = -ENOENT; | |
1008 | goto out; | |
1009 | } | |
1010 | ||
1011 | res->dl_leaf_bh = dir_ent_bh; | |
1012 | res->dl_entry = dir_ent; | |
1013 | res->dl_dx_leaf_bh = dx_leaf_bh; | |
1014 | res->dl_dx_entry = dx_entry; | |
1015 | ||
1016 | ret = 0; | |
1017 | out: | |
1018 | if (ret) { | |
1019 | brelse(dx_leaf_bh); | |
1020 | brelse(dir_ent_bh); | |
1021 | } | |
1022 | return ret; | |
1023 | } | |
1024 | ||
1025 | static int ocfs2_find_entry_dx(const char *name, int namelen, | |
1026 | struct inode *dir, | |
1027 | struct ocfs2_dir_lookup_result *lookup) | |
1028 | { | |
1029 | int ret; | |
1030 | struct buffer_head *di_bh = NULL; | |
1031 | struct ocfs2_dinode *di; | |
1032 | struct buffer_head *dx_root_bh = NULL; | |
1033 | struct ocfs2_dx_root_block *dx_root; | |
1034 | ||
1035 | ret = ocfs2_read_inode_block(dir, &di_bh); | |
1036 | if (ret) { | |
1037 | mlog_errno(ret); | |
1038 | goto out; | |
1039 | } | |
1040 | ||
1041 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
1042 | ||
1043 | ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); | |
1044 | if (ret) { | |
1045 | mlog_errno(ret); | |
1046 | goto out; | |
1047 | } | |
1048 | dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; | |
1049 | ||
4ed8a6bb | 1050 | ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup); |
9b7895ef MF |
1051 | if (ret) { |
1052 | if (ret != -ENOENT) | |
1053 | mlog_errno(ret); | |
1054 | goto out; | |
1055 | } | |
1056 | ||
4ed8a6bb MF |
1057 | lookup->dl_dx_root_bh = dx_root_bh; |
1058 | dx_root_bh = NULL; | |
9b7895ef MF |
1059 | out: |
1060 | brelse(di_bh); | |
1061 | brelse(dx_root_bh); | |
1062 | return ret; | |
1063 | } | |
1064 | ||
1065 | /* | |
1066 | * Try to find an entry of the provided name within 'dir'. | |
1067 | * | |
1068 | * If nothing was found, -ENOENT is returned. Otherwise, zero is | |
1069 | * returned and the struct 'res' will contain information useful to | |
1070 | * other directory manipulation functions. | |
1071 | * | |
1072 | * Caller can NOT assume anything about the contents of the | |
1073 | * buffer_heads - they are passed back only so that it can be passed | |
1074 | * into any one of the manipulation functions (add entry, delete | |
1075 | * entry, etc). As an example, bh in the extent directory case is a | |
1076 | * data block, in the inline-data case it actually points to an inode, | |
1077 | * in the indexed directory case, multiple buffers are involved. | |
1078 | */ | |
1079 | int ocfs2_find_entry(const char *name, int namelen, | |
1080 | struct inode *dir, struct ocfs2_dir_lookup_result *lookup) | |
1081 | { | |
1082 | struct buffer_head *bh; | |
1083 | struct ocfs2_dir_entry *res_dir = NULL; | |
1084 | ||
1085 | if (ocfs2_dir_indexed(dir)) | |
1086 | return ocfs2_find_entry_dx(name, namelen, dir, lookup); | |
1087 | ||
1088 | /* | |
1089 | * The unindexed dir code only uses part of the lookup | |
1090 | * structure, so there's no reason to push it down further | |
1091 | * than this. | |
1092 | */ | |
1093 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
1094 | bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir); | |
1095 | else | |
1096 | bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir); | |
1097 | ||
1098 | if (bh == NULL) | |
1099 | return -ENOENT; | |
1100 | ||
1101 | lookup->dl_leaf_bh = bh; | |
1102 | lookup->dl_entry = res_dir; | |
1103 | return 0; | |
1104 | } | |
1105 | ||
1106 | /* | |
1107 | * Update inode number and type of a previously found directory entry. | |
1108 | */ | |
1109 | int ocfs2_update_entry(struct inode *dir, handle_t *handle, | |
1110 | struct ocfs2_dir_lookup_result *res, | |
1111 | struct inode *new_entry_inode) | |
1112 | { | |
1113 | int ret; | |
1114 | ocfs2_journal_access_func access = ocfs2_journal_access_db; | |
1115 | struct ocfs2_dir_entry *de = res->dl_entry; | |
1116 | struct buffer_head *de_bh = res->dl_leaf_bh; | |
1117 | ||
1118 | /* | |
1119 | * The same code works fine for both inline-data and extent | |
1120 | * based directories, so no need to split this up. The only | |
1121 | * difference is the journal_access function. | |
1122 | */ | |
1123 | ||
1124 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
1125 | access = ocfs2_journal_access_di; | |
1126 | ||
0cf2f763 JB |
1127 | ret = access(handle, INODE_CACHE(dir), de_bh, |
1128 | OCFS2_JOURNAL_ACCESS_WRITE); | |
9b7895ef MF |
1129 | if (ret) { |
1130 | mlog_errno(ret); | |
1131 | goto out; | |
1132 | } | |
1133 | ||
1134 | de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno); | |
1135 | ocfs2_set_de_type(de, new_entry_inode->i_mode); | |
1136 | ||
1137 | ocfs2_journal_dirty(handle, de_bh); | |
1138 | ||
1139 | out: | |
1140 | return ret; | |
1141 | } | |
1142 | ||
1143 | /* | |
1144 | * __ocfs2_delete_entry deletes a directory entry by merging it with the | |
1145 | * previous entry | |
1146 | */ | |
1147 | static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir, | |
1148 | struct ocfs2_dir_entry *de_del, | |
1149 | struct buffer_head *bh, char *first_de, | |
1150 | unsigned int bytes) | |
1151 | { | |
1152 | struct ocfs2_dir_entry *de, *pde; | |
1153 | int i, status = -ENOENT; | |
1154 | ocfs2_journal_access_func access = ocfs2_journal_access_db; | |
1155 | ||
9b7895ef MF |
1156 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
1157 | access = ocfs2_journal_access_di; | |
1158 | ||
1159 | i = 0; | |
1160 | pde = NULL; | |
1161 | de = (struct ocfs2_dir_entry *) first_de; | |
1162 | while (i < bytes) { | |
1163 | if (!ocfs2_check_dir_entry(dir, de, bh, i)) { | |
1164 | status = -EIO; | |
1165 | mlog_errno(status); | |
1166 | goto bail; | |
1167 | } | |
1168 | if (de == de_del) { | |
0cf2f763 | 1169 | status = access(handle, INODE_CACHE(dir), bh, |
13723d00 | 1170 | OCFS2_JOURNAL_ACCESS_WRITE); |
316f4b9f MF |
1171 | if (status < 0) { |
1172 | status = -EIO; | |
1173 | mlog_errno(status); | |
1174 | goto bail; | |
1175 | } | |
1176 | if (pde) | |
0dd3256e MS |
1177 | le16_add_cpu(&pde->rec_len, |
1178 | le16_to_cpu(de->rec_len)); | |
82985248 | 1179 | de->inode = 0; |
316f4b9f | 1180 | dir->i_version++; |
ec20cec7 | 1181 | ocfs2_journal_dirty(handle, bh); |
316f4b9f MF |
1182 | goto bail; |
1183 | } | |
1184 | i += le16_to_cpu(de->rec_len); | |
1185 | pde = de; | |
1186 | de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len)); | |
1187 | } | |
1188 | bail: | |
316f4b9f MF |
1189 | return status; |
1190 | } | |
1191 | ||
e7c17e43 MF |
1192 | static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de) |
1193 | { | |
1194 | unsigned int hole; | |
1195 | ||
1196 | if (le64_to_cpu(de->inode) == 0) | |
1197 | hole = le16_to_cpu(de->rec_len); | |
1198 | else | |
1199 | hole = le16_to_cpu(de->rec_len) - | |
1200 | OCFS2_DIR_REC_LEN(de->name_len); | |
1201 | ||
1202 | return hole; | |
1203 | } | |
1204 | ||
1205 | static int ocfs2_find_max_rec_len(struct super_block *sb, | |
1206 | struct buffer_head *dirblock_bh) | |
1207 | { | |
1208 | int size, this_hole, largest_hole = 0; | |
1209 | char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data; | |
1210 | struct ocfs2_dir_entry *de; | |
1211 | ||
1212 | trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb); | |
1213 | size = ocfs2_dir_trailer_blk_off(sb); | |
1214 | limit = start + size; | |
1215 | de_buf = start; | |
1216 | de = (struct ocfs2_dir_entry *)de_buf; | |
1217 | do { | |
1218 | if (de_buf != trailer) { | |
1219 | this_hole = ocfs2_figure_dirent_hole(de); | |
1220 | if (this_hole > largest_hole) | |
1221 | largest_hole = this_hole; | |
1222 | } | |
1223 | ||
1224 | de_buf += le16_to_cpu(de->rec_len); | |
1225 | de = (struct ocfs2_dir_entry *)de_buf; | |
1226 | } while (de_buf < limit); | |
1227 | ||
1228 | if (largest_hole >= OCFS2_DIR_MIN_REC_LEN) | |
1229 | return largest_hole; | |
1230 | return 0; | |
1231 | } | |
1232 | ||
4ed8a6bb MF |
1233 | static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list, |
1234 | int index) | |
9b7895ef | 1235 | { |
4ed8a6bb | 1236 | int num_used = le16_to_cpu(entry_list->de_num_used); |
9b7895ef MF |
1237 | |
1238 | if (num_used == 1 || index == (num_used - 1)) | |
1239 | goto clear; | |
1240 | ||
4ed8a6bb MF |
1241 | memmove(&entry_list->de_entries[index], |
1242 | &entry_list->de_entries[index + 1], | |
9b7895ef MF |
1243 | (num_used - index - 1)*sizeof(struct ocfs2_dx_entry)); |
1244 | clear: | |
1245 | num_used--; | |
4ed8a6bb | 1246 | memset(&entry_list->de_entries[num_used], 0, |
9b7895ef | 1247 | sizeof(struct ocfs2_dx_entry)); |
4ed8a6bb | 1248 | entry_list->de_num_used = cpu_to_le16(num_used); |
9b7895ef MF |
1249 | } |
1250 | ||
1251 | static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir, | |
1252 | struct ocfs2_dir_lookup_result *lookup) | |
1253 | { | |
e7c17e43 | 1254 | int ret, index, max_rec_len, add_to_free_list = 0; |
4ed8a6bb | 1255 | struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; |
9b7895ef MF |
1256 | struct buffer_head *leaf_bh = lookup->dl_leaf_bh; |
1257 | struct ocfs2_dx_leaf *dx_leaf; | |
1258 | struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry; | |
e7c17e43 | 1259 | struct ocfs2_dir_block_trailer *trailer; |
4ed8a6bb MF |
1260 | struct ocfs2_dx_root_block *dx_root; |
1261 | struct ocfs2_dx_entry_list *entry_list; | |
1262 | ||
e7c17e43 MF |
1263 | /* |
1264 | * This function gets a bit messy because we might have to | |
1265 | * modify the root block, regardless of whether the indexed | |
1266 | * entries are stored inline. | |
1267 | */ | |
1268 | ||
1269 | /* | |
1270 | * *Only* set 'entry_list' here, based on where we're looking | |
1271 | * for the indexed entries. Later, we might still want to | |
1272 | * journal both blocks, based on free list state. | |
1273 | */ | |
4ed8a6bb MF |
1274 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
1275 | if (ocfs2_dx_root_inline(dx_root)) { | |
1276 | entry_list = &dx_root->dr_entries; | |
1277 | } else { | |
1278 | dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data; | |
1279 | entry_list = &dx_leaf->dl_list; | |
1280 | } | |
9b7895ef | 1281 | |
9b7895ef MF |
1282 | /* Neither of these are a disk corruption - that should have |
1283 | * been caught by lookup, before we got here. */ | |
4ed8a6bb MF |
1284 | BUG_ON(le16_to_cpu(entry_list->de_count) <= 0); |
1285 | BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0); | |
9b7895ef | 1286 | |
4ed8a6bb | 1287 | index = (char *)dx_entry - (char *)entry_list->de_entries; |
9b7895ef MF |
1288 | index /= sizeof(*dx_entry); |
1289 | ||
4ed8a6bb | 1290 | if (index >= le16_to_cpu(entry_list->de_num_used)) { |
9b7895ef | 1291 | mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n", |
4ed8a6bb MF |
1292 | (unsigned long long)OCFS2_I(dir)->ip_blkno, index, |
1293 | entry_list, dx_entry); | |
9b7895ef MF |
1294 | return -EIO; |
1295 | } | |
1296 | ||
e7c17e43 MF |
1297 | /* |
1298 | * We know that removal of this dirent will leave enough room | |
1299 | * for a new one, so add this block to the free list if it | |
1300 | * isn't already there. | |
1301 | */ | |
1302 | trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb); | |
1303 | if (trailer->db_free_rec_len == 0) | |
1304 | add_to_free_list = 1; | |
1305 | ||
9b7895ef | 1306 | /* |
4ed8a6bb MF |
1307 | * Add the block holding our index into the journal before |
1308 | * removing the unindexed entry. If we get an error return | |
1309 | * from __ocfs2_delete_entry(), then it hasn't removed the | |
1310 | * entry yet. Likewise, successful return means we *must* | |
1311 | * remove the indexed entry. | |
1312 | * | |
e3a93c2d MF |
1313 | * We're also careful to journal the root tree block here as |
1314 | * the entry count needs to be updated. Also, we might be | |
1315 | * adding to the start of the free list. | |
9b7895ef | 1316 | */ |
0cf2f763 | 1317 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
e3a93c2d MF |
1318 | OCFS2_JOURNAL_ACCESS_WRITE); |
1319 | if (ret) { | |
1320 | mlog_errno(ret); | |
1321 | goto out; | |
e7c17e43 MF |
1322 | } |
1323 | ||
1324 | if (!ocfs2_dx_root_inline(dx_root)) { | |
0cf2f763 | 1325 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), |
4ed8a6bb MF |
1326 | lookup->dl_dx_leaf_bh, |
1327 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1328 | if (ret) { | |
1329 | mlog_errno(ret); | |
1330 | goto out; | |
1331 | } | |
9b7895ef MF |
1332 | } |
1333 | ||
f1088d47 TM |
1334 | trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno, |
1335 | index); | |
4ed8a6bb | 1336 | |
9b7895ef MF |
1337 | ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry, |
1338 | leaf_bh, leaf_bh->b_data, leaf_bh->b_size); | |
1339 | if (ret) { | |
1340 | mlog_errno(ret); | |
1341 | goto out; | |
1342 | } | |
1343 | ||
e7c17e43 MF |
1344 | max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh); |
1345 | trailer->db_free_rec_len = cpu_to_le16(max_rec_len); | |
1346 | if (add_to_free_list) { | |
1347 | trailer->db_free_next = dx_root->dr_free_blk; | |
1348 | dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr); | |
1349 | ocfs2_journal_dirty(handle, dx_root_bh); | |
1350 | } | |
1351 | ||
1352 | /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */ | |
1353 | ocfs2_journal_dirty(handle, leaf_bh); | |
1354 | ||
e3a93c2d MF |
1355 | le32_add_cpu(&dx_root->dr_num_entries, -1); |
1356 | ocfs2_journal_dirty(handle, dx_root_bh); | |
1357 | ||
4ed8a6bb | 1358 | ocfs2_dx_list_remove_entry(entry_list, index); |
9b7895ef | 1359 | |
e3a93c2d | 1360 | if (!ocfs2_dx_root_inline(dx_root)) |
4ed8a6bb | 1361 | ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh); |
9b7895ef MF |
1362 | |
1363 | out: | |
1364 | return ret; | |
1365 | } | |
1366 | ||
5b6a3a2b MF |
1367 | static inline int ocfs2_delete_entry_id(handle_t *handle, |
1368 | struct inode *dir, | |
1369 | struct ocfs2_dir_entry *de_del, | |
1370 | struct buffer_head *bh) | |
1371 | { | |
1372 | int ret; | |
1373 | struct buffer_head *di_bh = NULL; | |
1374 | struct ocfs2_dinode *di; | |
1375 | struct ocfs2_inline_data *data; | |
1376 | ||
b657c95c | 1377 | ret = ocfs2_read_inode_block(dir, &di_bh); |
5b6a3a2b MF |
1378 | if (ret) { |
1379 | mlog_errno(ret); | |
1380 | goto out; | |
1381 | } | |
1382 | ||
1383 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
1384 | data = &di->id2.i_data; | |
1385 | ||
1386 | ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data, | |
1387 | i_size_read(dir)); | |
1388 | ||
1389 | brelse(di_bh); | |
1390 | out: | |
1391 | return ret; | |
1392 | } | |
1393 | ||
1394 | static inline int ocfs2_delete_entry_el(handle_t *handle, | |
1395 | struct inode *dir, | |
1396 | struct ocfs2_dir_entry *de_del, | |
1397 | struct buffer_head *bh) | |
1398 | { | |
1399 | return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data, | |
1400 | bh->b_size); | |
1401 | } | |
1402 | ||
1403 | /* | |
9b7895ef MF |
1404 | * Delete a directory entry. Hide the details of directory |
1405 | * implementation from the caller. | |
5b6a3a2b MF |
1406 | */ |
1407 | int ocfs2_delete_entry(handle_t *handle, | |
1408 | struct inode *dir, | |
4a12ca3a | 1409 | struct ocfs2_dir_lookup_result *res) |
5b6a3a2b | 1410 | { |
9b7895ef MF |
1411 | if (ocfs2_dir_indexed(dir)) |
1412 | return ocfs2_delete_entry_dx(handle, dir, res); | |
1413 | ||
5b6a3a2b | 1414 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
4a12ca3a MF |
1415 | return ocfs2_delete_entry_id(handle, dir, res->dl_entry, |
1416 | res->dl_leaf_bh); | |
5b6a3a2b | 1417 | |
4a12ca3a MF |
1418 | return ocfs2_delete_entry_el(handle, dir, res->dl_entry, |
1419 | res->dl_leaf_bh); | |
5b6a3a2b MF |
1420 | } |
1421 | ||
8553cf4f MF |
1422 | /* |
1423 | * Check whether 'de' has enough room to hold an entry of | |
1424 | * 'new_rec_len' bytes. | |
1425 | */ | |
1426 | static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de, | |
1427 | unsigned int new_rec_len) | |
1428 | { | |
1429 | unsigned int de_really_used; | |
1430 | ||
1431 | /* Check whether this is an empty record with enough space */ | |
1432 | if (le64_to_cpu(de->inode) == 0 && | |
1433 | le16_to_cpu(de->rec_len) >= new_rec_len) | |
1434 | return 1; | |
1435 | ||
1436 | /* | |
1437 | * Record might have free space at the end which we can | |
1438 | * use. | |
1439 | */ | |
1440 | de_really_used = OCFS2_DIR_REC_LEN(de->name_len); | |
1441 | if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len)) | |
1442 | return 1; | |
1443 | ||
1444 | return 0; | |
1445 | } | |
1446 | ||
9b7895ef MF |
1447 | static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf, |
1448 | struct ocfs2_dx_entry *dx_new_entry) | |
1449 | { | |
1450 | int i; | |
1451 | ||
1452 | i = le16_to_cpu(dx_leaf->dl_list.de_num_used); | |
1453 | dx_leaf->dl_list.de_entries[i] = *dx_new_entry; | |
1454 | ||
1455 | le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1); | |
1456 | } | |
1457 | ||
4ed8a6bb MF |
1458 | static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list, |
1459 | struct ocfs2_dx_hinfo *hinfo, | |
1460 | u64 dirent_blk) | |
1461 | { | |
1462 | int i; | |
1463 | struct ocfs2_dx_entry *dx_entry; | |
1464 | ||
1465 | i = le16_to_cpu(entry_list->de_num_used); | |
1466 | dx_entry = &entry_list->de_entries[i]; | |
1467 | ||
1468 | memset(dx_entry, 0, sizeof(*dx_entry)); | |
1469 | dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash); | |
1470 | dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash); | |
1471 | dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk); | |
1472 | ||
1473 | le16_add_cpu(&entry_list->de_num_used, 1); | |
1474 | } | |
1475 | ||
9b7895ef MF |
1476 | static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle, |
1477 | struct ocfs2_dx_hinfo *hinfo, | |
1478 | u64 dirent_blk, | |
1479 | struct buffer_head *dx_leaf_bh) | |
1480 | { | |
4ed8a6bb | 1481 | int ret; |
9b7895ef MF |
1482 | struct ocfs2_dx_leaf *dx_leaf; |
1483 | ||
0cf2f763 | 1484 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh, |
9b7895ef MF |
1485 | OCFS2_JOURNAL_ACCESS_WRITE); |
1486 | if (ret) { | |
1487 | mlog_errno(ret); | |
1488 | goto out; | |
1489 | } | |
1490 | ||
1491 | dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; | |
4ed8a6bb MF |
1492 | ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk); |
1493 | ocfs2_journal_dirty(handle, dx_leaf_bh); | |
9b7895ef | 1494 | |
4ed8a6bb MF |
1495 | out: |
1496 | return ret; | |
1497 | } | |
9b7895ef | 1498 | |
e3a93c2d MF |
1499 | static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle, |
1500 | struct ocfs2_dx_hinfo *hinfo, | |
1501 | u64 dirent_blk, | |
1502 | struct ocfs2_dx_root_block *dx_root) | |
4ed8a6bb | 1503 | { |
e3a93c2d MF |
1504 | ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk); |
1505 | } | |
1506 | ||
1507 | static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle, | |
1508 | struct ocfs2_dir_lookup_result *lookup) | |
1509 | { | |
1510 | int ret = 0; | |
4ed8a6bb | 1511 | struct ocfs2_dx_root_block *dx_root; |
e3a93c2d | 1512 | struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; |
9b7895ef | 1513 | |
0cf2f763 | 1514 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
4ed8a6bb MF |
1515 | OCFS2_JOURNAL_ACCESS_WRITE); |
1516 | if (ret) { | |
1517 | mlog_errno(ret); | |
1518 | goto out; | |
1519 | } | |
1520 | ||
e3a93c2d MF |
1521 | dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data; |
1522 | if (ocfs2_dx_root_inline(dx_root)) { | |
1523 | ocfs2_dx_inline_root_insert(dir, handle, | |
1524 | &lookup->dl_hinfo, | |
1525 | lookup->dl_leaf_bh->b_blocknr, | |
1526 | dx_root); | |
1527 | } else { | |
1528 | ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo, | |
1529 | lookup->dl_leaf_bh->b_blocknr, | |
1530 | lookup->dl_dx_leaf_bh); | |
1531 | if (ret) | |
1532 | goto out; | |
1533 | } | |
1534 | ||
1535 | le32_add_cpu(&dx_root->dr_num_entries, 1); | |
4ed8a6bb | 1536 | ocfs2_journal_dirty(handle, dx_root_bh); |
9b7895ef MF |
1537 | |
1538 | out: | |
1539 | return ret; | |
1540 | } | |
1541 | ||
e7c17e43 MF |
1542 | static void ocfs2_remove_block_from_free_list(struct inode *dir, |
1543 | handle_t *handle, | |
1544 | struct ocfs2_dir_lookup_result *lookup) | |
1545 | { | |
1546 | struct ocfs2_dir_block_trailer *trailer, *prev; | |
1547 | struct ocfs2_dx_root_block *dx_root; | |
1548 | struct buffer_head *bh; | |
1549 | ||
1550 | trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb); | |
1551 | ||
1552 | if (ocfs2_free_list_at_root(lookup)) { | |
1553 | bh = lookup->dl_dx_root_bh; | |
1554 | dx_root = (struct ocfs2_dx_root_block *)bh->b_data; | |
1555 | dx_root->dr_free_blk = trailer->db_free_next; | |
1556 | } else { | |
1557 | bh = lookup->dl_prev_leaf_bh; | |
1558 | prev = ocfs2_trailer_from_bh(bh, dir->i_sb); | |
1559 | prev->db_free_next = trailer->db_free_next; | |
1560 | } | |
1561 | ||
1562 | trailer->db_free_rec_len = cpu_to_le16(0); | |
1563 | trailer->db_free_next = cpu_to_le64(0); | |
1564 | ||
1565 | ocfs2_journal_dirty(handle, bh); | |
1566 | ocfs2_journal_dirty(handle, lookup->dl_leaf_bh); | |
1567 | } | |
1568 | ||
1569 | /* | |
1570 | * This expects that a journal write has been reserved on | |
1571 | * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh | |
1572 | */ | |
1573 | static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle, | |
1574 | struct ocfs2_dir_lookup_result *lookup) | |
1575 | { | |
1576 | int max_rec_len; | |
1577 | struct ocfs2_dir_block_trailer *trailer; | |
1578 | ||
1579 | /* Walk dl_leaf_bh to figure out what the new free rec_len is. */ | |
1580 | max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh); | |
1581 | if (max_rec_len) { | |
1582 | /* | |
1583 | * There's still room in this block, so no need to remove it | |
1584 | * from the free list. In this case, we just want to update | |
1585 | * the rec len accounting. | |
1586 | */ | |
1587 | trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb); | |
1588 | trailer->db_free_rec_len = cpu_to_le16(max_rec_len); | |
1589 | ocfs2_journal_dirty(handle, lookup->dl_leaf_bh); | |
1590 | } else { | |
1591 | ocfs2_remove_block_from_free_list(dir, handle, lookup); | |
1592 | } | |
1593 | } | |
1594 | ||
316f4b9f MF |
1595 | /* we don't always have a dentry for what we want to add, so people |
1596 | * like orphan dir can call this instead. | |
1597 | * | |
4a12ca3a MF |
1598 | * The lookup context must have been filled from |
1599 | * ocfs2_prepare_dir_for_insert. | |
316f4b9f MF |
1600 | */ |
1601 | int __ocfs2_add_entry(handle_t *handle, | |
1602 | struct inode *dir, | |
1603 | const char *name, int namelen, | |
1604 | struct inode *inode, u64 blkno, | |
1605 | struct buffer_head *parent_fe_bh, | |
4a12ca3a | 1606 | struct ocfs2_dir_lookup_result *lookup) |
316f4b9f MF |
1607 | { |
1608 | unsigned long offset; | |
1609 | unsigned short rec_len; | |
1610 | struct ocfs2_dir_entry *de, *de1; | |
5b6a3a2b MF |
1611 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data; |
1612 | struct super_block *sb = dir->i_sb; | |
2e173152 | 1613 | int retval; |
5b6a3a2b | 1614 | unsigned int size = sb->s_blocksize; |
4a12ca3a | 1615 | struct buffer_head *insert_bh = lookup->dl_leaf_bh; |
5b6a3a2b | 1616 | char *data_start = insert_bh->b_data; |
316f4b9f | 1617 | |
316f4b9f MF |
1618 | if (!namelen) |
1619 | return -EINVAL; | |
1620 | ||
e7c17e43 MF |
1621 | if (ocfs2_dir_indexed(dir)) { |
1622 | struct buffer_head *bh; | |
1623 | ||
1624 | /* | |
1625 | * An indexed dir may require that we update the free space | |
1626 | * list. Reserve a write to the previous node in the list so | |
1627 | * that we don't fail later. | |
1628 | * | |
1629 | * XXX: This can be either a dx_root_block, or an unindexed | |
1630 | * directory tree leaf block. | |
1631 | */ | |
1632 | if (ocfs2_free_list_at_root(lookup)) { | |
1633 | bh = lookup->dl_dx_root_bh; | |
0cf2f763 JB |
1634 | retval = ocfs2_journal_access_dr(handle, |
1635 | INODE_CACHE(dir), bh, | |
e7c17e43 MF |
1636 | OCFS2_JOURNAL_ACCESS_WRITE); |
1637 | } else { | |
1638 | bh = lookup->dl_prev_leaf_bh; | |
0cf2f763 JB |
1639 | retval = ocfs2_journal_access_db(handle, |
1640 | INODE_CACHE(dir), bh, | |
e7c17e43 MF |
1641 | OCFS2_JOURNAL_ACCESS_WRITE); |
1642 | } | |
1643 | if (retval) { | |
1644 | mlog_errno(retval); | |
1645 | return retval; | |
1646 | } | |
1647 | } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | |
5b6a3a2b MF |
1648 | data_start = di->id2.i_data.id_data; |
1649 | size = i_size_read(dir); | |
1650 | ||
1651 | BUG_ON(insert_bh != parent_fe_bh); | |
1652 | } | |
1653 | ||
316f4b9f MF |
1654 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
1655 | offset = 0; | |
5b6a3a2b | 1656 | de = (struct ocfs2_dir_entry *) data_start; |
316f4b9f | 1657 | while (1) { |
5b6a3a2b MF |
1658 | BUG_ON((char *)de >= (size + data_start)); |
1659 | ||
316f4b9f MF |
1660 | /* These checks should've already been passed by the |
1661 | * prepare function, but I guess we can leave them | |
1662 | * here anyway. */ | |
1663 | if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) { | |
1664 | retval = -ENOENT; | |
1665 | goto bail; | |
1666 | } | |
1667 | if (ocfs2_match(namelen, name, de)) { | |
1668 | retval = -EEXIST; | |
1669 | goto bail; | |
1670 | } | |
8553cf4f | 1671 | |
87d35a74 MF |
1672 | /* We're guaranteed that we should have space, so we |
1673 | * can't possibly have hit the trailer...right? */ | |
1674 | mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size), | |
1675 | "Hit dir trailer trying to insert %.*s " | |
1676 | "(namelen %d) into directory %llu. " | |
1677 | "offset is %lu, trailer offset is %d\n", | |
1678 | namelen, name, namelen, | |
1679 | (unsigned long long)parent_fe_bh->b_blocknr, | |
1680 | offset, ocfs2_dir_trailer_blk_off(dir->i_sb)); | |
1681 | ||
8553cf4f | 1682 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
316f4b9f MF |
1683 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
1684 | retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); | |
1685 | if (retval < 0) { | |
1686 | mlog_errno(retval); | |
1687 | goto bail; | |
1688 | } | |
1689 | ||
13723d00 | 1690 | if (insert_bh == parent_fe_bh) |
2e173152 | 1691 | retval = ocfs2_journal_access_di(handle, |
0cf2f763 | 1692 | INODE_CACHE(dir), |
13723d00 JB |
1693 | insert_bh, |
1694 | OCFS2_JOURNAL_ACCESS_WRITE); | |
9b7895ef | 1695 | else { |
2e173152 | 1696 | retval = ocfs2_journal_access_db(handle, |
0cf2f763 | 1697 | INODE_CACHE(dir), |
13723d00 | 1698 | insert_bh, |
4ed8a6bb MF |
1699 | OCFS2_JOURNAL_ACCESS_WRITE); |
1700 | ||
2e173152 DY |
1701 | if (!retval && ocfs2_dir_indexed(dir)) |
1702 | retval = ocfs2_dx_dir_insert(dir, | |
4ed8a6bb MF |
1703 | handle, |
1704 | lookup); | |
2e173152 DY |
1705 | } |
1706 | ||
1707 | if (retval) { | |
1708 | mlog_errno(retval); | |
1709 | goto bail; | |
9b7895ef MF |
1710 | } |
1711 | ||
316f4b9f MF |
1712 | /* By now the buffer is marked for journaling */ |
1713 | offset += le16_to_cpu(de->rec_len); | |
1714 | if (le64_to_cpu(de->inode)) { | |
1715 | de1 = (struct ocfs2_dir_entry *)((char *) de + | |
1716 | OCFS2_DIR_REC_LEN(de->name_len)); | |
1717 | de1->rec_len = | |
1718 | cpu_to_le16(le16_to_cpu(de->rec_len) - | |
1719 | OCFS2_DIR_REC_LEN(de->name_len)); | |
1720 | de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); | |
1721 | de = de1; | |
1722 | } | |
1723 | de->file_type = OCFS2_FT_UNKNOWN; | |
1724 | if (blkno) { | |
1725 | de->inode = cpu_to_le64(blkno); | |
1726 | ocfs2_set_de_type(de, inode->i_mode); | |
1727 | } else | |
1728 | de->inode = 0; | |
1729 | de->name_len = namelen; | |
1730 | memcpy(de->name, name, namelen); | |
1731 | ||
e7c17e43 MF |
1732 | if (ocfs2_dir_indexed(dir)) |
1733 | ocfs2_recalc_free_list(dir, handle, lookup); | |
1734 | ||
316f4b9f | 1735 | dir->i_version++; |
ec20cec7 | 1736 | ocfs2_journal_dirty(handle, insert_bh); |
316f4b9f MF |
1737 | retval = 0; |
1738 | goto bail; | |
1739 | } | |
87d35a74 | 1740 | |
316f4b9f MF |
1741 | offset += le16_to_cpu(de->rec_len); |
1742 | de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len)); | |
1743 | } | |
1744 | ||
1745 | /* when you think about it, the assert above should prevent us | |
1746 | * from ever getting here. */ | |
1747 | retval = -ENOSPC; | |
1748 | bail: | |
c1e8d35e TM |
1749 | if (retval) |
1750 | mlog_errno(retval); | |
316f4b9f | 1751 | |
316f4b9f MF |
1752 | return retval; |
1753 | } | |
1754 | ||
23193e51 | 1755 | static int ocfs2_dir_foreach_blk_id(struct inode *inode, |
2b47c361 | 1756 | u64 *f_version, |
3704412b | 1757 | struct dir_context *ctx) |
23193e51 | 1758 | { |
3704412b AV |
1759 | int ret, i; |
1760 | unsigned long offset = ctx->pos; | |
23193e51 MF |
1761 | struct buffer_head *di_bh = NULL; |
1762 | struct ocfs2_dinode *di; | |
1763 | struct ocfs2_inline_data *data; | |
1764 | struct ocfs2_dir_entry *de; | |
1765 | ||
b657c95c | 1766 | ret = ocfs2_read_inode_block(inode, &di_bh); |
23193e51 MF |
1767 | if (ret) { |
1768 | mlog(ML_ERROR, "Unable to read inode block for dir %llu\n", | |
1769 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
1770 | goto out; | |
1771 | } | |
1772 | ||
1773 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
1774 | data = &di->id2.i_data; | |
1775 | ||
3704412b | 1776 | while (ctx->pos < i_size_read(inode)) { |
23193e51 MF |
1777 | /* If the dir block has changed since the last call to |
1778 | * readdir(2), then we might be pointing to an invalid | |
1779 | * dirent right now. Scan from the start of the block | |
1780 | * to make sure. */ | |
1781 | if (*f_version != inode->i_version) { | |
1782 | for (i = 0; i < i_size_read(inode) && i < offset; ) { | |
1783 | de = (struct ocfs2_dir_entry *) | |
1784 | (data->id_data + i); | |
1785 | /* It's too expensive to do a full | |
1786 | * dirent test each time round this | |
1787 | * loop, but we do have to test at | |
1788 | * least that it is non-zero. A | |
1789 | * failure will be detected in the | |
1790 | * dirent test below. */ | |
1791 | if (le16_to_cpu(de->rec_len) < | |
1792 | OCFS2_DIR_REC_LEN(1)) | |
1793 | break; | |
1794 | i += le16_to_cpu(de->rec_len); | |
1795 | } | |
3704412b | 1796 | ctx->pos = offset = i; |
23193e51 MF |
1797 | *f_version = inode->i_version; |
1798 | } | |
1799 | ||
3704412b AV |
1800 | de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos); |
1801 | if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) { | |
23193e51 | 1802 | /* On error, skip the f_pos to the end. */ |
3704412b AV |
1803 | ctx->pos = i_size_read(inode); |
1804 | break; | |
23193e51 MF |
1805 | } |
1806 | offset += le16_to_cpu(de->rec_len); | |
1807 | if (le64_to_cpu(de->inode)) { | |
23193e51 MF |
1808 | unsigned char d_type = DT_UNKNOWN; |
1809 | ||
1810 | if (de->file_type < OCFS2_FT_MAX) | |
1811 | d_type = ocfs2_filetype_table[de->file_type]; | |
1812 | ||
3704412b AV |
1813 | if (!dir_emit(ctx, de->name, de->name_len, |
1814 | le64_to_cpu(de->inode), d_type)) | |
1815 | goto out; | |
23193e51 | 1816 | } |
3704412b | 1817 | ctx->pos += le16_to_cpu(de->rec_len); |
23193e51 | 1818 | } |
23193e51 MF |
1819 | out: |
1820 | brelse(di_bh); | |
23193e51 MF |
1821 | return 0; |
1822 | } | |
1823 | ||
9b7895ef MF |
1824 | /* |
1825 | * NOTE: This function can be called against unindexed directories, | |
1826 | * and indexed ones. | |
1827 | */ | |
23193e51 | 1828 | static int ocfs2_dir_foreach_blk_el(struct inode *inode, |
2b47c361 | 1829 | u64 *f_version, |
3704412b AV |
1830 | struct dir_context *ctx, |
1831 | bool persist) | |
ccd979bd | 1832 | { |
aa958874 | 1833 | unsigned long offset, blk, last_ra_blk = 0; |
3704412b | 1834 | int i; |
ccd979bd MF |
1835 | struct buffer_head * bh, * tmp; |
1836 | struct ocfs2_dir_entry * de; | |
ccd979bd | 1837 | struct super_block * sb = inode->i_sb; |
aa958874 | 1838 | unsigned int ra_sectors = 16; |
3704412b | 1839 | int stored = 0; |
ccd979bd | 1840 | |
ccd979bd MF |
1841 | bh = NULL; |
1842 | ||
3704412b | 1843 | offset = ctx->pos & (sb->s_blocksize - 1); |
ccd979bd | 1844 | |
3704412b AV |
1845 | while (ctx->pos < i_size_read(inode)) { |
1846 | blk = ctx->pos >> sb->s_blocksize_bits; | |
a22305cc JB |
1847 | if (ocfs2_read_dir_block(inode, blk, &bh, 0)) { |
1848 | /* Skip the corrupt dirblock and keep trying */ | |
3704412b | 1849 | ctx->pos += sb->s_blocksize - offset; |
ccd979bd MF |
1850 | continue; |
1851 | } | |
1852 | ||
aa958874 MF |
1853 | /* The idea here is to begin with 8k read-ahead and to stay |
1854 | * 4k ahead of our current position. | |
1855 | * | |
1856 | * TODO: Use the pagecache for this. We just need to | |
1857 | * make sure it's cluster-safe... */ | |
1858 | if (!last_ra_blk | |
1859 | || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { | |
1860 | for (i = ra_sectors >> (sb->s_blocksize_bits - 9); | |
ccd979bd | 1861 | i > 0; i--) { |
a22305cc JB |
1862 | tmp = NULL; |
1863 | if (!ocfs2_read_dir_block(inode, ++blk, &tmp, | |
1864 | OCFS2_BH_READAHEAD)) | |
1865 | brelse(tmp); | |
ccd979bd | 1866 | } |
aa958874 MF |
1867 | last_ra_blk = blk; |
1868 | ra_sectors = 8; | |
ccd979bd MF |
1869 | } |
1870 | ||
ccd979bd MF |
1871 | /* If the dir block has changed since the last call to |
1872 | * readdir(2), then we might be pointing to an invalid | |
1873 | * dirent right now. Scan from the start of the block | |
1874 | * to make sure. */ | |
b8bc5f4f | 1875 | if (*f_version != inode->i_version) { |
ccd979bd MF |
1876 | for (i = 0; i < sb->s_blocksize && i < offset; ) { |
1877 | de = (struct ocfs2_dir_entry *) (bh->b_data + i); | |
1878 | /* It's too expensive to do a full | |
1879 | * dirent test each time round this | |
1880 | * loop, but we do have to test at | |
1881 | * least that it is non-zero. A | |
1882 | * failure will be detected in the | |
1883 | * dirent test below. */ | |
1884 | if (le16_to_cpu(de->rec_len) < | |
1885 | OCFS2_DIR_REC_LEN(1)) | |
1886 | break; | |
1887 | i += le16_to_cpu(de->rec_len); | |
1888 | } | |
1889 | offset = i; | |
3704412b | 1890 | ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1)) |
ccd979bd | 1891 | | offset; |
b8bc5f4f | 1892 | *f_version = inode->i_version; |
ccd979bd MF |
1893 | } |
1894 | ||
3704412b | 1895 | while (ctx->pos < i_size_read(inode) |
ccd979bd MF |
1896 | && offset < sb->s_blocksize) { |
1897 | de = (struct ocfs2_dir_entry *) (bh->b_data + offset); | |
1898 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { | |
1899 | /* On error, skip the f_pos to the | |
1900 | next block. */ | |
3704412b | 1901 | ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1; |
ccd979bd | 1902 | brelse(bh); |
3704412b | 1903 | continue; |
ccd979bd | 1904 | } |
ccd979bd | 1905 | if (le64_to_cpu(de->inode)) { |
ccd979bd MF |
1906 | unsigned char d_type = DT_UNKNOWN; |
1907 | ||
1908 | if (de->file_type < OCFS2_FT_MAX) | |
1909 | d_type = ocfs2_filetype_table[de->file_type]; | |
3704412b | 1910 | if (!dir_emit(ctx, de->name, |
ccd979bd | 1911 | de->name_len, |
7e853679 | 1912 | le64_to_cpu(de->inode), |
3704412b AV |
1913 | d_type)) { |
1914 | brelse(bh); | |
1915 | return 0; | |
e7b34019 | 1916 | } |
3704412b | 1917 | stored++; |
ccd979bd | 1918 | } |
3704412b AV |
1919 | offset += le16_to_cpu(de->rec_len); |
1920 | ctx->pos += le16_to_cpu(de->rec_len); | |
ccd979bd MF |
1921 | } |
1922 | offset = 0; | |
1923 | brelse(bh); | |
a22305cc | 1924 | bh = NULL; |
3704412b AV |
1925 | if (!persist && stored) |
1926 | break; | |
ccd979bd | 1927 | } |
3704412b | 1928 | return 0; |
b8bc5f4f MF |
1929 | } |
1930 | ||
2b47c361 | 1931 | static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version, |
3704412b AV |
1932 | struct dir_context *ctx, |
1933 | bool persist) | |
23193e51 MF |
1934 | { |
1935 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
3704412b AV |
1936 | return ocfs2_dir_foreach_blk_id(inode, f_version, ctx); |
1937 | return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist); | |
23193e51 MF |
1938 | } |
1939 | ||
5eae5b96 MF |
1940 | /* |
1941 | * This is intended to be called from inside other kernel functions, | |
1942 | * so we fake some arguments. | |
1943 | */ | |
3704412b | 1944 | int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx) |
5eae5b96 | 1945 | { |
2b47c361 | 1946 | u64 version = inode->i_version; |
3704412b | 1947 | ocfs2_dir_foreach_blk(inode, &version, ctx, true); |
5eae5b96 MF |
1948 | return 0; |
1949 | } | |
1950 | ||
b8bc5f4f MF |
1951 | /* |
1952 | * ocfs2_readdir() | |
1953 | * | |
1954 | */ | |
3704412b | 1955 | int ocfs2_readdir(struct file *file, struct dir_context *ctx) |
b8bc5f4f MF |
1956 | { |
1957 | int error = 0; | |
3704412b | 1958 | struct inode *inode = file_inode(file); |
b8bc5f4f MF |
1959 | int lock_level = 0; |
1960 | ||
f1088d47 | 1961 | trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno); |
b8bc5f4f | 1962 | |
3704412b | 1963 | error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level); |
b8bc5f4f MF |
1964 | if (lock_level && error >= 0) { |
1965 | /* We release EX lock which used to update atime | |
1966 | * and get PR lock again to reduce contention | |
1967 | * on commonly accessed directories. */ | |
e63aecb6 | 1968 | ocfs2_inode_unlock(inode, 1); |
b8bc5f4f | 1969 | lock_level = 0; |
e63aecb6 | 1970 | error = ocfs2_inode_lock(inode, NULL, 0); |
b8bc5f4f MF |
1971 | } |
1972 | if (error < 0) { | |
1973 | if (error != -ENOENT) | |
1974 | mlog_errno(error); | |
1975 | /* we haven't got any yet, so propagate the error. */ | |
1976 | goto bail_nolock; | |
1977 | } | |
1978 | ||
3704412b | 1979 | error = ocfs2_dir_foreach_blk(inode, &file->f_version, ctx, false); |
b8bc5f4f | 1980 | |
e63aecb6 | 1981 | ocfs2_inode_unlock(inode, lock_level); |
c1e8d35e TM |
1982 | if (error) |
1983 | mlog_errno(error); | |
ccd979bd | 1984 | |
aa958874 | 1985 | bail_nolock: |
ccd979bd | 1986 | |
b8bc5f4f | 1987 | return error; |
ccd979bd MF |
1988 | } |
1989 | ||
1990 | /* | |
1b1dcc1b | 1991 | * NOTE: this should always be called with parent dir i_mutex taken. |
ccd979bd MF |
1992 | */ |
1993 | int ocfs2_find_files_on_disk(const char *name, | |
1994 | int namelen, | |
1995 | u64 *blkno, | |
1996 | struct inode *inode, | |
4a12ca3a | 1997 | struct ocfs2_dir_lookup_result *lookup) |
ccd979bd MF |
1998 | { |
1999 | int status = -ENOENT; | |
ccd979bd | 2000 | |
f1088d47 TM |
2001 | trace_ocfs2_find_files_on_disk(namelen, name, blkno, |
2002 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd | 2003 | |
4a12ca3a MF |
2004 | status = ocfs2_find_entry(name, namelen, inode, lookup); |
2005 | if (status) | |
ccd979bd | 2006 | goto leave; |
ccd979bd | 2007 | |
4a12ca3a | 2008 | *blkno = le64_to_cpu(lookup->dl_entry->inode); |
ccd979bd MF |
2009 | |
2010 | status = 0; | |
2011 | leave: | |
ccd979bd | 2012 | |
ccd979bd MF |
2013 | return status; |
2014 | } | |
2015 | ||
be94d117 MF |
2016 | /* |
2017 | * Convenience function for callers which just want the block number | |
2018 | * mapped to a name and don't require the full dirent info, etc. | |
2019 | */ | |
2020 | int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name, | |
2021 | int namelen, u64 *blkno) | |
2022 | { | |
2023 | int ret; | |
4a12ca3a | 2024 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
be94d117 | 2025 | |
4a12ca3a MF |
2026 | ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup); |
2027 | ocfs2_free_dir_lookup_result(&lookup); | |
be94d117 MF |
2028 | |
2029 | return ret; | |
2030 | } | |
2031 | ||
ccd979bd MF |
2032 | /* Check for a name within a directory. |
2033 | * | |
2034 | * Return 0 if the name does not exist | |
2035 | * Return -EEXIST if the directory contains the name | |
2036 | * | |
1b1dcc1b | 2037 | * Callers should have i_mutex + a cluster lock on dir |
ccd979bd MF |
2038 | */ |
2039 | int ocfs2_check_dir_for_entry(struct inode *dir, | |
2040 | const char *name, | |
2041 | int namelen) | |
2042 | { | |
7c01ad8f | 2043 | int ret = 0; |
4a12ca3a | 2044 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
ccd979bd | 2045 | |
f1088d47 TM |
2046 | trace_ocfs2_check_dir_for_entry( |
2047 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); | |
ccd979bd | 2048 | |
7c01ad8f DY |
2049 | if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0) { |
2050 | ret = -EEXIST; | |
2051 | mlog_errno(ret); | |
2052 | } | |
ccd979bd | 2053 | |
4a12ca3a | 2054 | ocfs2_free_dir_lookup_result(&lookup); |
ccd979bd | 2055 | |
ccd979bd MF |
2056 | return ret; |
2057 | } | |
2058 | ||
0bfbbf62 | 2059 | struct ocfs2_empty_dir_priv { |
3704412b | 2060 | struct dir_context ctx; |
0bfbbf62 MF |
2061 | unsigned seen_dot; |
2062 | unsigned seen_dot_dot; | |
2063 | unsigned seen_other; | |
e3a93c2d | 2064 | unsigned dx_dir; |
0bfbbf62 | 2065 | }; |
ac7576f4 MS |
2066 | static int ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name, |
2067 | int name_len, loff_t pos, u64 ino, | |
2068 | unsigned type) | |
0bfbbf62 | 2069 | { |
ac7576f4 MS |
2070 | struct ocfs2_empty_dir_priv *p = |
2071 | container_of(ctx, struct ocfs2_empty_dir_priv, ctx); | |
0bfbbf62 MF |
2072 | |
2073 | /* | |
2074 | * Check the positions of "." and ".." records to be sure | |
2075 | * they're in the correct place. | |
e3a93c2d MF |
2076 | * |
2077 | * Indexed directories don't need to proceed past the first | |
2078 | * two entries, so we end the scan after seeing '..'. Despite | |
2079 | * that, we allow the scan to proceed In the event that we | |
2080 | * have a corrupted indexed directory (no dot or dot dot | |
2081 | * entries). This allows us to double check for existing | |
2082 | * entries which might not have been found in the index. | |
0bfbbf62 MF |
2083 | */ |
2084 | if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) { | |
2085 | p->seen_dot = 1; | |
2086 | return 0; | |
2087 | } | |
2088 | ||
2089 | if (name_len == 2 && !strncmp("..", name, 2) && | |
2090 | pos == OCFS2_DIR_REC_LEN(1)) { | |
2091 | p->seen_dot_dot = 1; | |
e3a93c2d MF |
2092 | |
2093 | if (p->dx_dir && p->seen_dot) | |
2094 | return 1; | |
2095 | ||
0bfbbf62 MF |
2096 | return 0; |
2097 | } | |
2098 | ||
2099 | p->seen_other = 1; | |
2100 | return 1; | |
2101 | } | |
e3a93c2d MF |
2102 | |
2103 | static int ocfs2_empty_dir_dx(struct inode *inode, | |
2104 | struct ocfs2_empty_dir_priv *priv) | |
2105 | { | |
2106 | int ret; | |
2107 | struct buffer_head *di_bh = NULL; | |
2108 | struct buffer_head *dx_root_bh = NULL; | |
2109 | struct ocfs2_dinode *di; | |
2110 | struct ocfs2_dx_root_block *dx_root; | |
2111 | ||
2112 | priv->dx_dir = 1; | |
2113 | ||
2114 | ret = ocfs2_read_inode_block(inode, &di_bh); | |
2115 | if (ret) { | |
2116 | mlog_errno(ret); | |
2117 | goto out; | |
2118 | } | |
2119 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
2120 | ||
2121 | ret = ocfs2_read_dx_root(inode, di, &dx_root_bh); | |
2122 | if (ret) { | |
2123 | mlog_errno(ret); | |
2124 | goto out; | |
2125 | } | |
2126 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; | |
2127 | ||
2128 | if (le32_to_cpu(dx_root->dr_num_entries) != 2) | |
2129 | priv->seen_other = 1; | |
2130 | ||
2131 | out: | |
2132 | brelse(di_bh); | |
2133 | brelse(dx_root_bh); | |
2134 | return ret; | |
2135 | } | |
2136 | ||
ccd979bd MF |
2137 | /* |
2138 | * routine to check that the specified directory is empty (for rmdir) | |
0bfbbf62 MF |
2139 | * |
2140 | * Returns 1 if dir is empty, zero otherwise. | |
9b7895ef | 2141 | * |
e3a93c2d | 2142 | * XXX: This is a performance problem for unindexed directories. |
ccd979bd MF |
2143 | */ |
2144 | int ocfs2_empty_dir(struct inode *inode) | |
2145 | { | |
0bfbbf62 | 2146 | int ret; |
3704412b | 2147 | struct ocfs2_empty_dir_priv priv = { |
d6394b59 | 2148 | .ctx.actor = ocfs2_empty_dir_filldir, |
3704412b | 2149 | }; |
ccd979bd | 2150 | |
e3a93c2d MF |
2151 | if (ocfs2_dir_indexed(inode)) { |
2152 | ret = ocfs2_empty_dir_dx(inode, &priv); | |
2153 | if (ret) | |
2154 | mlog_errno(ret); | |
2155 | /* | |
2156 | * We still run ocfs2_dir_foreach to get the checks | |
2157 | * for "." and "..". | |
2158 | */ | |
2159 | } | |
2160 | ||
3704412b | 2161 | ret = ocfs2_dir_foreach(inode, &priv.ctx); |
0bfbbf62 MF |
2162 | if (ret) |
2163 | mlog_errno(ret); | |
2164 | ||
2165 | if (!priv.seen_dot || !priv.seen_dot_dot) { | |
2166 | mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", | |
b0697053 | 2167 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
0bfbbf62 MF |
2168 | /* |
2169 | * XXX: Is it really safe to allow an unlink to continue? | |
2170 | */ | |
ccd979bd MF |
2171 | return 1; |
2172 | } | |
0bfbbf62 MF |
2173 | |
2174 | return !priv.seen_other; | |
ccd979bd MF |
2175 | } |
2176 | ||
87d35a74 MF |
2177 | /* |
2178 | * Fills "." and ".." dirents in a new directory block. Returns dirent for | |
2179 | * "..", which might be used during creation of a directory with a trailing | |
2180 | * header. It is otherwise safe to ignore the return code. | |
2181 | */ | |
2182 | static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode, | |
2183 | struct inode *parent, | |
2184 | char *start, | |
2185 | unsigned int size) | |
5b6a3a2b MF |
2186 | { |
2187 | struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start; | |
2188 | ||
2189 | de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); | |
2190 | de->name_len = 1; | |
2191 | de->rec_len = | |
2192 | cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); | |
2193 | strcpy(de->name, "."); | |
2194 | ocfs2_set_de_type(de, S_IFDIR); | |
2195 | ||
2196 | de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len)); | |
2197 | de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno); | |
2198 | de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1)); | |
2199 | de->name_len = 2; | |
2200 | strcpy(de->name, ".."); | |
2201 | ocfs2_set_de_type(de, S_IFDIR); | |
87d35a74 MF |
2202 | |
2203 | return de; | |
5b6a3a2b MF |
2204 | } |
2205 | ||
2206 | /* | |
2207 | * This works together with code in ocfs2_mknod_locked() which sets | |
2208 | * the inline-data flag and initializes the inline-data section. | |
2209 | */ | |
2210 | static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb, | |
2211 | handle_t *handle, | |
2212 | struct inode *parent, | |
2213 | struct inode *inode, | |
2214 | struct buffer_head *di_bh) | |
2215 | { | |
2216 | int ret; | |
2217 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
2218 | struct ocfs2_inline_data *data = &di->id2.i_data; | |
2219 | unsigned int size = le16_to_cpu(data->id_count); | |
2220 | ||
0cf2f763 | 2221 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
13723d00 | 2222 | OCFS2_JOURNAL_ACCESS_WRITE); |
5b6a3a2b MF |
2223 | if (ret) { |
2224 | mlog_errno(ret); | |
2225 | goto out; | |
2226 | } | |
2227 | ||
2228 | ocfs2_fill_initial_dirents(inode, parent, data->id_data, size); | |
5b6a3a2b | 2229 | ocfs2_journal_dirty(handle, di_bh); |
5b6a3a2b MF |
2230 | |
2231 | i_size_write(inode, size); | |
bfe86848 | 2232 | set_nlink(inode, 2); |
5b6a3a2b MF |
2233 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
2234 | ||
2235 | ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); | |
2236 | if (ret < 0) | |
2237 | mlog_errno(ret); | |
2238 | ||
2239 | out: | |
2240 | return ret; | |
2241 | } | |
2242 | ||
2243 | static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb, | |
2244 | handle_t *handle, | |
2245 | struct inode *parent, | |
2246 | struct inode *inode, | |
2247 | struct buffer_head *fe_bh, | |
9b7895ef MF |
2248 | struct ocfs2_alloc_context *data_ac, |
2249 | struct buffer_head **ret_new_bh) | |
316f4b9f MF |
2250 | { |
2251 | int status; | |
87d35a74 | 2252 | unsigned int size = osb->sb->s_blocksize; |
316f4b9f | 2253 | struct buffer_head *new_bh = NULL; |
87d35a74 | 2254 | struct ocfs2_dir_entry *de; |
316f4b9f | 2255 | |
e7c17e43 | 2256 | if (ocfs2_new_dir_wants_trailer(inode)) |
87d35a74 MF |
2257 | size = ocfs2_dir_trailer_blk_off(parent->i_sb); |
2258 | ||
316f4b9f MF |
2259 | status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh, |
2260 | data_ac, NULL, &new_bh); | |
2261 | if (status < 0) { | |
2262 | mlog_errno(status); | |
2263 | goto bail; | |
2264 | } | |
2265 | ||
8cb471e8 | 2266 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh); |
316f4b9f | 2267 | |
0cf2f763 | 2268 | status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh, |
13723d00 | 2269 | OCFS2_JOURNAL_ACCESS_CREATE); |
316f4b9f MF |
2270 | if (status < 0) { |
2271 | mlog_errno(status); | |
2272 | goto bail; | |
2273 | } | |
2274 | memset(new_bh->b_data, 0, osb->sb->s_blocksize); | |
2275 | ||
87d35a74 | 2276 | de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size); |
e7c17e43 MF |
2277 | if (ocfs2_new_dir_wants_trailer(inode)) { |
2278 | int size = le16_to_cpu(de->rec_len); | |
2279 | ||
2280 | /* | |
2281 | * Figure out the size of the hole left over after | |
2282 | * insertion of '.' and '..'. The trailer wants this | |
2283 | * information. | |
2284 | */ | |
2285 | size -= OCFS2_DIR_REC_LEN(2); | |
2286 | size -= sizeof(struct ocfs2_dir_block_trailer); | |
2287 | ||
2288 | ocfs2_init_dir_trailer(inode, new_bh, size); | |
2289 | } | |
316f4b9f | 2290 | |
ec20cec7 | 2291 | ocfs2_journal_dirty(handle, new_bh); |
316f4b9f MF |
2292 | |
2293 | i_size_write(inode, inode->i_sb->s_blocksize); | |
bfe86848 | 2294 | set_nlink(inode, 2); |
316f4b9f MF |
2295 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
2296 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | |
2297 | if (status < 0) { | |
2298 | mlog_errno(status); | |
2299 | goto bail; | |
2300 | } | |
2301 | ||
2302 | status = 0; | |
9b7895ef MF |
2303 | if (ret_new_bh) { |
2304 | *ret_new_bh = new_bh; | |
2305 | new_bh = NULL; | |
2306 | } | |
316f4b9f | 2307 | bail: |
a81cb88b | 2308 | brelse(new_bh); |
316f4b9f | 2309 | |
316f4b9f MF |
2310 | return status; |
2311 | } | |
2312 | ||
9b7895ef MF |
2313 | static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb, |
2314 | handle_t *handle, struct inode *dir, | |
2315 | struct buffer_head *di_bh, | |
e7c17e43 | 2316 | struct buffer_head *dirdata_bh, |
9b7895ef | 2317 | struct ocfs2_alloc_context *meta_ac, |
e3a93c2d | 2318 | int dx_inline, u32 num_entries, |
9b7895ef | 2319 | struct buffer_head **ret_dx_root_bh) |
5b6a3a2b | 2320 | { |
9b7895ef MF |
2321 | int ret; |
2322 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; | |
2323 | u16 dr_suballoc_bit; | |
2b6cb576 | 2324 | u64 suballoc_loc, dr_blkno; |
9b7895ef MF |
2325 | unsigned int num_bits; |
2326 | struct buffer_head *dx_root_bh = NULL; | |
2327 | struct ocfs2_dx_root_block *dx_root; | |
e7c17e43 MF |
2328 | struct ocfs2_dir_block_trailer *trailer = |
2329 | ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb); | |
9b7895ef | 2330 | |
2b6cb576 JB |
2331 | ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, |
2332 | &dr_suballoc_bit, &num_bits, &dr_blkno); | |
9b7895ef MF |
2333 | if (ret) { |
2334 | mlog_errno(ret); | |
2335 | goto out; | |
2336 | } | |
5b6a3a2b | 2337 | |
f1088d47 TM |
2338 | trace_ocfs2_dx_dir_attach_index( |
2339 | (unsigned long long)OCFS2_I(dir)->ip_blkno, | |
2340 | (unsigned long long)dr_blkno); | |
5b6a3a2b | 2341 | |
9b7895ef MF |
2342 | dx_root_bh = sb_getblk(osb->sb, dr_blkno); |
2343 | if (dx_root_bh == NULL) { | |
7391a294 | 2344 | ret = -ENOMEM; |
9b7895ef MF |
2345 | goto out; |
2346 | } | |
8cb471e8 | 2347 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh); |
87d35a74 | 2348 | |
0cf2f763 | 2349 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
9b7895ef MF |
2350 | OCFS2_JOURNAL_ACCESS_CREATE); |
2351 | if (ret < 0) { | |
2352 | mlog_errno(ret); | |
2353 | goto out; | |
2354 | } | |
87d35a74 | 2355 | |
9b7895ef MF |
2356 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
2357 | memset(dx_root, 0, osb->sb->s_blocksize); | |
2358 | strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE); | |
b89c5428 | 2359 | dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); |
2b6cb576 | 2360 | dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc); |
9b7895ef MF |
2361 | dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit); |
2362 | dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation); | |
2363 | dx_root->dr_blkno = cpu_to_le64(dr_blkno); | |
2364 | dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno); | |
e3a93c2d | 2365 | dx_root->dr_num_entries = cpu_to_le32(num_entries); |
e7c17e43 MF |
2366 | if (le16_to_cpu(trailer->db_free_rec_len)) |
2367 | dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr); | |
2368 | else | |
2369 | dx_root->dr_free_blk = cpu_to_le64(0); | |
4ed8a6bb MF |
2370 | |
2371 | if (dx_inline) { | |
2372 | dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE; | |
2373 | dx_root->dr_entries.de_count = | |
2374 | cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb)); | |
2375 | } else { | |
2376 | dx_root->dr_list.l_count = | |
2377 | cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb)); | |
2378 | } | |
ec20cec7 | 2379 | ocfs2_journal_dirty(handle, dx_root_bh); |
5b6a3a2b | 2380 | |
0cf2f763 | 2381 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, |
9b7895ef MF |
2382 | OCFS2_JOURNAL_ACCESS_CREATE); |
2383 | if (ret) { | |
2384 | mlog_errno(ret); | |
2385 | goto out; | |
2386 | } | |
2387 | ||
2388 | di->i_dx_root = cpu_to_le64(dr_blkno); | |
2389 | ||
8ac33dc8 | 2390 | spin_lock(&OCFS2_I(dir)->ip_lock); |
9b7895ef MF |
2391 | OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL; |
2392 | di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features); | |
8ac33dc8 | 2393 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
9b7895ef | 2394 | |
ec20cec7 | 2395 | ocfs2_journal_dirty(handle, di_bh); |
9b7895ef MF |
2396 | |
2397 | *ret_dx_root_bh = dx_root_bh; | |
2398 | dx_root_bh = NULL; | |
2399 | ||
2400 | out: | |
2401 | brelse(dx_root_bh); | |
2402 | return ret; | |
2403 | } | |
2404 | ||
2405 | static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb, | |
2406 | handle_t *handle, struct inode *dir, | |
2407 | struct buffer_head **dx_leaves, | |
2408 | int num_dx_leaves, u64 start_blk) | |
2409 | { | |
2410 | int ret, i; | |
2411 | struct ocfs2_dx_leaf *dx_leaf; | |
2412 | struct buffer_head *bh; | |
2413 | ||
2414 | for (i = 0; i < num_dx_leaves; i++) { | |
2415 | bh = sb_getblk(osb->sb, start_blk + i); | |
2416 | if (bh == NULL) { | |
7391a294 | 2417 | ret = -ENOMEM; |
9b7895ef MF |
2418 | goto out; |
2419 | } | |
2420 | dx_leaves[i] = bh; | |
2421 | ||
8cb471e8 | 2422 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh); |
9b7895ef | 2423 | |
0cf2f763 | 2424 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh, |
9b7895ef MF |
2425 | OCFS2_JOURNAL_ACCESS_CREATE); |
2426 | if (ret < 0) { | |
2427 | mlog_errno(ret); | |
2428 | goto out; | |
2429 | } | |
2430 | ||
2431 | dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data; | |
2432 | ||
2433 | memset(dx_leaf, 0, osb->sb->s_blocksize); | |
2434 | strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE); | |
2435 | dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation); | |
2436 | dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr); | |
2437 | dx_leaf->dl_list.de_count = | |
2438 | cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb)); | |
2439 | ||
f1088d47 TM |
2440 | trace_ocfs2_dx_dir_format_cluster( |
2441 | (unsigned long long)OCFS2_I(dir)->ip_blkno, | |
2442 | (unsigned long long)bh->b_blocknr, | |
2443 | le16_to_cpu(dx_leaf->dl_list.de_count)); | |
9b7895ef MF |
2444 | |
2445 | ocfs2_journal_dirty(handle, bh); | |
2446 | } | |
2447 | ||
2448 | ret = 0; | |
2449 | out: | |
2450 | return ret; | |
2451 | } | |
2452 | ||
2453 | /* | |
2454 | * Allocates and formats a new cluster for use in an indexed dir | |
2455 | * leaf. This version will not do the extent insert, so that it can be | |
2456 | * used by operations which need careful ordering. | |
2457 | */ | |
2458 | static int __ocfs2_dx_dir_new_cluster(struct inode *dir, | |
2459 | u32 cpos, handle_t *handle, | |
2460 | struct ocfs2_alloc_context *data_ac, | |
2461 | struct buffer_head **dx_leaves, | |
2462 | int num_dx_leaves, u64 *ret_phys_blkno) | |
2463 | { | |
2464 | int ret; | |
2465 | u32 phys, num; | |
2466 | u64 phys_blkno; | |
2467 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); | |
2468 | ||
2469 | /* | |
2470 | * XXX: For create, this should claim cluster for the index | |
2471 | * *before* the unindexed insert so that we have a better | |
2472 | * chance of contiguousness as the directory grows in number | |
2473 | * of entries. | |
2474 | */ | |
1ed9b777 | 2475 | ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num); |
9b7895ef MF |
2476 | if (ret) { |
2477 | mlog_errno(ret); | |
2478 | goto out; | |
2479 | } | |
2480 | ||
2481 | /* | |
2482 | * Format the new cluster first. That way, we're inserting | |
2483 | * valid data. | |
2484 | */ | |
2485 | phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys); | |
2486 | ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves, | |
2487 | num_dx_leaves, phys_blkno); | |
2488 | if (ret) { | |
2489 | mlog_errno(ret); | |
2490 | goto out; | |
2491 | } | |
2492 | ||
2493 | *ret_phys_blkno = phys_blkno; | |
2494 | out: | |
2495 | return ret; | |
2496 | } | |
2497 | ||
2498 | static int ocfs2_dx_dir_new_cluster(struct inode *dir, | |
2499 | struct ocfs2_extent_tree *et, | |
2500 | u32 cpos, handle_t *handle, | |
2501 | struct ocfs2_alloc_context *data_ac, | |
2502 | struct ocfs2_alloc_context *meta_ac, | |
2503 | struct buffer_head **dx_leaves, | |
2504 | int num_dx_leaves) | |
2505 | { | |
2506 | int ret; | |
2507 | u64 phys_blkno; | |
9b7895ef MF |
2508 | |
2509 | ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves, | |
2510 | num_dx_leaves, &phys_blkno); | |
2511 | if (ret) { | |
2512 | mlog_errno(ret); | |
2513 | goto out; | |
2514 | } | |
2515 | ||
cc79d8c1 | 2516 | ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0, |
9b7895ef MF |
2517 | meta_ac); |
2518 | if (ret) | |
2519 | mlog_errno(ret); | |
2520 | out: | |
2521 | return ret; | |
2522 | } | |
2523 | ||
2524 | static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb, | |
2525 | int *ret_num_leaves) | |
2526 | { | |
2527 | int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1); | |
2528 | struct buffer_head **dx_leaves; | |
2529 | ||
2530 | dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *), | |
2531 | GFP_NOFS); | |
2532 | if (dx_leaves && ret_num_leaves) | |
2533 | *ret_num_leaves = num_dx_leaves; | |
2534 | ||
2535 | return dx_leaves; | |
2536 | } | |
2537 | ||
2538 | static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb, | |
2539 | handle_t *handle, | |
2540 | struct inode *parent, | |
2541 | struct inode *inode, | |
2542 | struct buffer_head *di_bh, | |
2543 | struct ocfs2_alloc_context *data_ac, | |
2544 | struct ocfs2_alloc_context *meta_ac) | |
2545 | { | |
4ed8a6bb | 2546 | int ret; |
9b7895ef MF |
2547 | struct buffer_head *leaf_bh = NULL; |
2548 | struct buffer_head *dx_root_bh = NULL; | |
9b7895ef | 2549 | struct ocfs2_dx_hinfo hinfo; |
4ed8a6bb MF |
2550 | struct ocfs2_dx_root_block *dx_root; |
2551 | struct ocfs2_dx_entry_list *entry_list; | |
9b7895ef MF |
2552 | |
2553 | /* | |
2554 | * Our strategy is to create the directory as though it were | |
2555 | * unindexed, then add the index block. This works with very | |
2556 | * little complication since the state of a new directory is a | |
2557 | * very well known quantity. | |
2558 | * | |
2559 | * Essentially, we have two dirents ("." and ".."), in the 1st | |
4ed8a6bb MF |
2560 | * block which need indexing. These are easily inserted into |
2561 | * the index block. | |
9b7895ef MF |
2562 | */ |
2563 | ||
2564 | ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh, | |
2565 | data_ac, &leaf_bh); | |
2566 | if (ret) { | |
2567 | mlog_errno(ret); | |
2568 | goto out; | |
2569 | } | |
2570 | ||
e7c17e43 | 2571 | ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh, |
e3a93c2d | 2572 | meta_ac, 1, 2, &dx_root_bh); |
9b7895ef MF |
2573 | if (ret) { |
2574 | mlog_errno(ret); | |
2575 | goto out; | |
2576 | } | |
4ed8a6bb MF |
2577 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
2578 | entry_list = &dx_root->dr_entries; | |
9b7895ef | 2579 | |
4ed8a6bb | 2580 | /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */ |
e7c17e43 | 2581 | ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo); |
4ed8a6bb | 2582 | ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr); |
9b7895ef MF |
2583 | |
2584 | ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo); | |
4ed8a6bb | 2585 | ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr); |
9b7895ef MF |
2586 | |
2587 | out: | |
9b7895ef MF |
2588 | brelse(dx_root_bh); |
2589 | brelse(leaf_bh); | |
2590 | return ret; | |
2591 | } | |
2592 | ||
2593 | int ocfs2_fill_new_dir(struct ocfs2_super *osb, | |
2594 | handle_t *handle, | |
2595 | struct inode *parent, | |
2596 | struct inode *inode, | |
2597 | struct buffer_head *fe_bh, | |
2598 | struct ocfs2_alloc_context *data_ac, | |
2599 | struct ocfs2_alloc_context *meta_ac) | |
2600 | ||
2601 | { | |
2602 | BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL); | |
2603 | ||
2604 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
2605 | return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh); | |
2606 | ||
2607 | if (ocfs2_supports_indexed_dirs(osb)) | |
2608 | return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh, | |
2609 | data_ac, meta_ac); | |
2610 | ||
2611 | return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh, | |
2612 | data_ac, NULL); | |
2613 | } | |
2614 | ||
2615 | static int ocfs2_dx_dir_index_block(struct inode *dir, | |
2616 | handle_t *handle, | |
2617 | struct buffer_head **dx_leaves, | |
2618 | int num_dx_leaves, | |
e3a93c2d | 2619 | u32 *num_dx_entries, |
9b7895ef MF |
2620 | struct buffer_head *dirent_bh) |
2621 | { | |
0fba8137 | 2622 | int ret = 0, namelen, i; |
9b7895ef MF |
2623 | char *de_buf, *limit; |
2624 | struct ocfs2_dir_entry *de; | |
2625 | struct buffer_head *dx_leaf_bh; | |
2626 | struct ocfs2_dx_hinfo hinfo; | |
2627 | u64 dirent_blk = dirent_bh->b_blocknr; | |
2628 | ||
2629 | de_buf = dirent_bh->b_data; | |
2630 | limit = de_buf + dir->i_sb->s_blocksize; | |
2631 | ||
2632 | while (de_buf < limit) { | |
2633 | de = (struct ocfs2_dir_entry *)de_buf; | |
2634 | ||
2635 | namelen = de->name_len; | |
2636 | if (!namelen || !de->inode) | |
2637 | goto inc; | |
2638 | ||
2639 | ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo); | |
2640 | ||
2641 | i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo); | |
2642 | dx_leaf_bh = dx_leaves[i]; | |
2643 | ||
2644 | ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo, | |
2645 | dirent_blk, dx_leaf_bh); | |
2646 | if (ret) { | |
2647 | mlog_errno(ret); | |
2648 | goto out; | |
2649 | } | |
2650 | ||
e3a93c2d MF |
2651 | *num_dx_entries = *num_dx_entries + 1; |
2652 | ||
9b7895ef MF |
2653 | inc: |
2654 | de_buf += le16_to_cpu(de->rec_len); | |
2655 | } | |
2656 | ||
2657 | out: | |
2658 | return ret; | |
2659 | } | |
e7c17e43 MF |
2660 | |
2661 | /* | |
4ed8a6bb MF |
2662 | * XXX: This expects dx_root_bh to already be part of the transaction. |
2663 | */ | |
2664 | static void ocfs2_dx_dir_index_root_block(struct inode *dir, | |
2665 | struct buffer_head *dx_root_bh, | |
2666 | struct buffer_head *dirent_bh) | |
2667 | { | |
2668 | char *de_buf, *limit; | |
2669 | struct ocfs2_dx_root_block *dx_root; | |
2670 | struct ocfs2_dir_entry *de; | |
2671 | struct ocfs2_dx_hinfo hinfo; | |
2672 | u64 dirent_blk = dirent_bh->b_blocknr; | |
2673 | ||
2674 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; | |
2675 | ||
2676 | de_buf = dirent_bh->b_data; | |
2677 | limit = de_buf + dir->i_sb->s_blocksize; | |
2678 | ||
2679 | while (de_buf < limit) { | |
2680 | de = (struct ocfs2_dir_entry *)de_buf; | |
2681 | ||
2682 | if (!de->name_len || !de->inode) | |
2683 | goto inc; | |
2684 | ||
2685 | ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo); | |
2686 | ||
f1088d47 TM |
2687 | trace_ocfs2_dx_dir_index_root_block( |
2688 | (unsigned long long)dir->i_ino, | |
2689 | hinfo.major_hash, hinfo.minor_hash, | |
2690 | de->name_len, de->name, | |
2691 | le16_to_cpu(dx_root->dr_entries.de_num_used)); | |
4ed8a6bb MF |
2692 | |
2693 | ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo, | |
2694 | dirent_blk); | |
e3a93c2d MF |
2695 | |
2696 | le32_add_cpu(&dx_root->dr_num_entries, 1); | |
4ed8a6bb MF |
2697 | inc: |
2698 | de_buf += le16_to_cpu(de->rec_len); | |
2699 | } | |
2700 | } | |
2701 | ||
2702 | /* | |
2703 | * Count the number of inline directory entries in di_bh and compare | |
2704 | * them against the number of entries we can hold in an inline dx root | |
2705 | * block. | |
2706 | */ | |
2707 | static int ocfs2_new_dx_should_be_inline(struct inode *dir, | |
2708 | struct buffer_head *di_bh) | |
2709 | { | |
2710 | int dirent_count = 0; | |
2711 | char *de_buf, *limit; | |
2712 | struct ocfs2_dir_entry *de; | |
2713 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
2714 | ||
2715 | de_buf = di->id2.i_data.id_data; | |
2716 | limit = de_buf + i_size_read(dir); | |
2717 | ||
2718 | while (de_buf < limit) { | |
2719 | de = (struct ocfs2_dir_entry *)de_buf; | |
2720 | ||
2721 | if (de->name_len && de->inode) | |
2722 | dirent_count++; | |
2723 | ||
2724 | de_buf += le16_to_cpu(de->rec_len); | |
2725 | } | |
2726 | ||
2727 | /* We are careful to leave room for one extra record. */ | |
2728 | return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb); | |
2729 | } | |
9b7895ef MF |
2730 | |
2731 | /* | |
2732 | * Expand rec_len of the rightmost dirent in a directory block so that it | |
2733 | * contains the end of our valid space for dirents. We do this during | |
2734 | * expansion from an inline directory to one with extents. The first dir block | |
2735 | * in that case is taken from the inline data portion of the inode block. | |
2736 | * | |
e7c17e43 MF |
2737 | * This will also return the largest amount of contiguous space for a dirent |
2738 | * in the block. That value is *not* necessarily the last dirent, even after | |
2739 | * expansion. The directory indexing code wants this value for free space | |
2740 | * accounting. We do this here since we're already walking the entire dir | |
2741 | * block. | |
2742 | * | |
9b7895ef MF |
2743 | * We add the dir trailer if this filesystem wants it. |
2744 | */ | |
e7c17e43 MF |
2745 | static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size, |
2746 | struct inode *dir) | |
9b7895ef | 2747 | { |
e7c17e43 | 2748 | struct super_block *sb = dir->i_sb; |
9b7895ef MF |
2749 | struct ocfs2_dir_entry *de; |
2750 | struct ocfs2_dir_entry *prev_de; | |
2751 | char *de_buf, *limit; | |
2752 | unsigned int new_size = sb->s_blocksize; | |
e7c17e43 MF |
2753 | unsigned int bytes, this_hole; |
2754 | unsigned int largest_hole = 0; | |
9b7895ef | 2755 | |
e7c17e43 | 2756 | if (ocfs2_new_dir_wants_trailer(dir)) |
9b7895ef MF |
2757 | new_size = ocfs2_dir_trailer_blk_off(sb); |
2758 | ||
2759 | bytes = new_size - old_size; | |
2760 | ||
2761 | limit = start + old_size; | |
2762 | de_buf = start; | |
2763 | de = (struct ocfs2_dir_entry *)de_buf; | |
5b6a3a2b | 2764 | do { |
e7c17e43 MF |
2765 | this_hole = ocfs2_figure_dirent_hole(de); |
2766 | if (this_hole > largest_hole) | |
2767 | largest_hole = this_hole; | |
2768 | ||
5b6a3a2b MF |
2769 | prev_de = de; |
2770 | de_buf += le16_to_cpu(de->rec_len); | |
2771 | de = (struct ocfs2_dir_entry *)de_buf; | |
2772 | } while (de_buf < limit); | |
2773 | ||
2774 | le16_add_cpu(&prev_de->rec_len, bytes); | |
e7c17e43 MF |
2775 | |
2776 | /* We need to double check this after modification of the final | |
2777 | * dirent. */ | |
2778 | this_hole = ocfs2_figure_dirent_hole(prev_de); | |
2779 | if (this_hole > largest_hole) | |
2780 | largest_hole = this_hole; | |
2781 | ||
2782 | if (largest_hole >= OCFS2_DIR_MIN_REC_LEN) | |
2783 | return largest_hole; | |
2784 | return 0; | |
5b6a3a2b MF |
2785 | } |
2786 | ||
2787 | /* | |
2788 | * We allocate enough clusters to fulfill "blocks_wanted", but set | |
2789 | * i_size to exactly one block. Ocfs2_extend_dir() will handle the | |
2790 | * rest automatically for us. | |
2791 | * | |
2792 | * *first_block_bh is a pointer to the 1st data block allocated to the | |
2793 | * directory. | |
2794 | */ | |
2795 | static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh, | |
2796 | unsigned int blocks_wanted, | |
9b7895ef | 2797 | struct ocfs2_dir_lookup_result *lookup, |
5b6a3a2b MF |
2798 | struct buffer_head **first_block_bh) |
2799 | { | |
e3a93c2d | 2800 | u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0; |
5b6a3a2b | 2801 | struct super_block *sb = dir->i_sb; |
4ed8a6bb | 2802 | int ret, i, num_dx_leaves = 0, dx_inline = 0, |
9b7895ef MF |
2803 | credits = ocfs2_inline_to_extents_credits(sb); |
2804 | u64 dx_insert_blkno, blkno, | |
2805 | bytes = blocks_wanted << sb->s_blocksize_bits; | |
5b6a3a2b MF |
2806 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
2807 | struct ocfs2_inode_info *oi = OCFS2_I(dir); | |
5d44670f | 2808 | struct ocfs2_alloc_context *data_ac = NULL; |
9b7895ef | 2809 | struct ocfs2_alloc_context *meta_ac = NULL; |
5b6a3a2b | 2810 | struct buffer_head *dirdata_bh = NULL; |
9b7895ef MF |
2811 | struct buffer_head *dx_root_bh = NULL; |
2812 | struct buffer_head **dx_leaves = NULL; | |
5b6a3a2b MF |
2813 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
2814 | handle_t *handle; | |
f99b9b7c | 2815 | struct ocfs2_extent_tree et; |
9b7895ef MF |
2816 | struct ocfs2_extent_tree dx_et; |
2817 | int did_quota = 0, bytes_allocated = 0; | |
f99b9b7c | 2818 | |
5e404e9e | 2819 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh); |
5b6a3a2b MF |
2820 | |
2821 | alloc = ocfs2_clusters_for_bytes(sb, bytes); | |
9b7895ef MF |
2822 | dx_alloc = 0; |
2823 | ||
edd45c08 JK |
2824 | down_write(&oi->ip_alloc_sem); |
2825 | ||
9b7895ef | 2826 | if (ocfs2_supports_indexed_dirs(osb)) { |
9b7895ef MF |
2827 | credits += ocfs2_add_dir_index_credits(sb); |
2828 | ||
4ed8a6bb MF |
2829 | dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh); |
2830 | if (!dx_inline) { | |
2831 | /* Add one more cluster for an index leaf */ | |
2832 | dx_alloc++; | |
2833 | dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb, | |
2834 | &num_dx_leaves); | |
2835 | if (!dx_leaves) { | |
2836 | ret = -ENOMEM; | |
2837 | mlog_errno(ret); | |
2838 | goto out; | |
2839 | } | |
9b7895ef MF |
2840 | } |
2841 | ||
2842 | /* This gets us the dx_root */ | |
2843 | ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac); | |
2844 | if (ret) { | |
2845 | mlog_errno(ret); | |
2846 | goto out; | |
2847 | } | |
2848 | } | |
5b6a3a2b MF |
2849 | |
2850 | /* | |
9b7895ef MF |
2851 | * We should never need more than 2 clusters for the unindexed |
2852 | * tree - maximum dirent size is far less than one block. In | |
2853 | * fact, the only time we'd need more than one cluster is if | |
5b6a3a2b MF |
2854 | * blocksize == clustersize and the dirent won't fit in the |
2855 | * extra space that the expansion to a single block gives. As | |
2856 | * of today, that only happens on 4k/4k file systems. | |
2857 | */ | |
2858 | BUG_ON(alloc > 2); | |
2859 | ||
035a5711 | 2860 | ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac); |
5b6a3a2b MF |
2861 | if (ret) { |
2862 | mlog_errno(ret); | |
2863 | goto out; | |
2864 | } | |
2865 | ||
5b6a3a2b | 2866 | /* |
c78bad11 | 2867 | * Prepare for worst case allocation scenario of two separate |
9b7895ef | 2868 | * extents in the unindexed tree. |
5b6a3a2b MF |
2869 | */ |
2870 | if (alloc == 2) | |
2871 | credits += OCFS2_SUBALLOC_ALLOC; | |
2872 | ||
2873 | handle = ocfs2_start_trans(osb, credits); | |
2874 | if (IS_ERR(handle)) { | |
2875 | ret = PTR_ERR(handle); | |
2876 | mlog_errno(ret); | |
edd45c08 | 2877 | goto out; |
5b6a3a2b MF |
2878 | } |
2879 | ||
5dd4056d CH |
2880 | ret = dquot_alloc_space_nodirty(dir, |
2881 | ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc)); | |
2882 | if (ret) | |
a90714c1 | 2883 | goto out_commit; |
a90714c1 | 2884 | did_quota = 1; |
9b7895ef | 2885 | |
4ed8a6bb | 2886 | if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) { |
9b7895ef MF |
2887 | /* |
2888 | * Allocate our index cluster first, to maximize the | |
2889 | * possibility that unindexed leaves grow | |
2890 | * contiguously. | |
2891 | */ | |
2892 | ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, | |
2893 | dx_leaves, num_dx_leaves, | |
2894 | &dx_insert_blkno); | |
2895 | if (ret) { | |
2896 | mlog_errno(ret); | |
2897 | goto out_commit; | |
2898 | } | |
2899 | bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); | |
2900 | } | |
2901 | ||
5b6a3a2b MF |
2902 | /* |
2903 | * Try to claim as many clusters as the bitmap can give though | |
2904 | * if we only get one now, that's enough to continue. The rest | |
2905 | * will be claimed after the conversion to extents. | |
2906 | */ | |
83f92318 MF |
2907 | if (ocfs2_dir_resv_allowed(osb)) |
2908 | data_ac->ac_resv = &oi->ip_la_data_resv; | |
1ed9b777 | 2909 | ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len); |
5b6a3a2b MF |
2910 | if (ret) { |
2911 | mlog_errno(ret); | |
2912 | goto out_commit; | |
2913 | } | |
9b7895ef | 2914 | bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); |
5b6a3a2b MF |
2915 | |
2916 | /* | |
2917 | * Operations are carefully ordered so that we set up the new | |
2918 | * data block first. The conversion from inline data to | |
2919 | * extents follows. | |
2920 | */ | |
2921 | blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); | |
2922 | dirdata_bh = sb_getblk(sb, blkno); | |
2923 | if (!dirdata_bh) { | |
7391a294 | 2924 | ret = -ENOMEM; |
5b6a3a2b MF |
2925 | mlog_errno(ret); |
2926 | goto out_commit; | |
2927 | } | |
2928 | ||
8cb471e8 | 2929 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh); |
5b6a3a2b | 2930 | |
0cf2f763 | 2931 | ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh, |
13723d00 | 2932 | OCFS2_JOURNAL_ACCESS_CREATE); |
5b6a3a2b MF |
2933 | if (ret) { |
2934 | mlog_errno(ret); | |
2935 | goto out_commit; | |
2936 | } | |
2937 | ||
2938 | memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir)); | |
2939 | memset(dirdata_bh->b_data + i_size_read(dir), 0, | |
2940 | sb->s_blocksize - i_size_read(dir)); | |
e7c17e43 MF |
2941 | i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir); |
2942 | if (ocfs2_new_dir_wants_trailer(dir)) { | |
2943 | /* | |
2944 | * Prepare the dir trailer up front. It will otherwise look | |
2945 | * like a valid dirent. Even if inserting the index fails | |
2946 | * (unlikely), then all we'll have done is given first dir | |
2947 | * block a small amount of fragmentation. | |
2948 | */ | |
2949 | ocfs2_init_dir_trailer(dir, dirdata_bh, i); | |
2950 | } | |
5b6a3a2b | 2951 | |
2931cdcb | 2952 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
ec20cec7 | 2953 | ocfs2_journal_dirty(handle, dirdata_bh); |
5b6a3a2b | 2954 | |
4ed8a6bb MF |
2955 | if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) { |
2956 | /* | |
2957 | * Dx dirs with an external cluster need to do this up | |
2958 | * front. Inline dx root's get handled later, after | |
e3a93c2d MF |
2959 | * we've allocated our root block. We get passed back |
2960 | * a total number of items so that dr_num_entries can | |
2961 | * be correctly set once the dx_root has been | |
2962 | * allocated. | |
4ed8a6bb | 2963 | */ |
9b7895ef | 2964 | ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves, |
e3a93c2d MF |
2965 | num_dx_leaves, &num_dx_entries, |
2966 | dirdata_bh); | |
9b7895ef MF |
2967 | if (ret) { |
2968 | mlog_errno(ret); | |
2969 | goto out_commit; | |
2970 | } | |
2971 | } | |
2972 | ||
5b6a3a2b MF |
2973 | /* |
2974 | * Set extent, i_size, etc on the directory. After this, the | |
2975 | * inode should contain the same exact dirents as before and | |
2976 | * be fully accessible from system calls. | |
2977 | * | |
2978 | * We let the later dirent insert modify c/mtime - to the user | |
2979 | * the data hasn't changed. | |
2980 | */ | |
0cf2f763 | 2981 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, |
13723d00 | 2982 | OCFS2_JOURNAL_ACCESS_CREATE); |
5b6a3a2b MF |
2983 | if (ret) { |
2984 | mlog_errno(ret); | |
2985 | goto out_commit; | |
2986 | } | |
2987 | ||
2988 | spin_lock(&oi->ip_lock); | |
2989 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; | |
2990 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); | |
2991 | spin_unlock(&oi->ip_lock); | |
2992 | ||
2993 | ocfs2_dinode_new_extent_list(dir, di); | |
2994 | ||
2995 | i_size_write(dir, sb->s_blocksize); | |
2996 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; | |
2997 | ||
2998 | di->i_size = cpu_to_le64(sb->s_blocksize); | |
2999 | di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec); | |
3000 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec); | |
6fdb702d | 3001 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
5b6a3a2b MF |
3002 | |
3003 | /* | |
3004 | * This should never fail as our extent list is empty and all | |
3005 | * related blocks have been journaled already. | |
3006 | */ | |
cc79d8c1 | 3007 | ret = ocfs2_insert_extent(handle, &et, 0, blkno, len, |
f99b9b7c | 3008 | 0, NULL); |
5b6a3a2b MF |
3009 | if (ret) { |
3010 | mlog_errno(ret); | |
83cab533 | 3011 | goto out_commit; |
5b6a3a2b MF |
3012 | } |
3013 | ||
9780eb6c MF |
3014 | /* |
3015 | * Set i_blocks after the extent insert for the most up to | |
3016 | * date ip_clusters value. | |
3017 | */ | |
3018 | dir->i_blocks = ocfs2_inode_sector_count(dir); | |
3019 | ||
ec20cec7 | 3020 | ocfs2_journal_dirty(handle, di_bh); |
5b6a3a2b | 3021 | |
9b7895ef MF |
3022 | if (ocfs2_supports_indexed_dirs(osb)) { |
3023 | ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh, | |
e7c17e43 | 3024 | dirdata_bh, meta_ac, dx_inline, |
e3a93c2d | 3025 | num_dx_entries, &dx_root_bh); |
9b7895ef MF |
3026 | if (ret) { |
3027 | mlog_errno(ret); | |
3028 | goto out_commit; | |
3029 | } | |
3030 | ||
4ed8a6bb MF |
3031 | if (dx_inline) { |
3032 | ocfs2_dx_dir_index_root_block(dir, dx_root_bh, | |
3033 | dirdata_bh); | |
3034 | } else { | |
5e404e9e JB |
3035 | ocfs2_init_dx_root_extent_tree(&dx_et, |
3036 | INODE_CACHE(dir), | |
3037 | dx_root_bh); | |
cc79d8c1 | 3038 | ret = ocfs2_insert_extent(handle, &dx_et, 0, |
4ed8a6bb MF |
3039 | dx_insert_blkno, 1, 0, NULL); |
3040 | if (ret) | |
3041 | mlog_errno(ret); | |
3042 | } | |
9b7895ef MF |
3043 | } |
3044 | ||
5b6a3a2b MF |
3045 | /* |
3046 | * We asked for two clusters, but only got one in the 1st | |
3047 | * pass. Claim the 2nd cluster as a separate extent. | |
3048 | */ | |
3049 | if (alloc > len) { | |
1ed9b777 | 3050 | ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, |
5b6a3a2b MF |
3051 | &len); |
3052 | if (ret) { | |
3053 | mlog_errno(ret); | |
3054 | goto out_commit; | |
3055 | } | |
3056 | blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); | |
3057 | ||
cc79d8c1 | 3058 | ret = ocfs2_insert_extent(handle, &et, 1, |
f99b9b7c | 3059 | blkno, len, 0, NULL); |
5b6a3a2b MF |
3060 | if (ret) { |
3061 | mlog_errno(ret); | |
83cab533 | 3062 | goto out_commit; |
5b6a3a2b | 3063 | } |
9b7895ef | 3064 | bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); |
5b6a3a2b MF |
3065 | } |
3066 | ||
3067 | *first_block_bh = dirdata_bh; | |
3068 | dirdata_bh = NULL; | |
9b7895ef MF |
3069 | if (ocfs2_supports_indexed_dirs(osb)) { |
3070 | unsigned int off; | |
3071 | ||
4ed8a6bb MF |
3072 | if (!dx_inline) { |
3073 | /* | |
3074 | * We need to return the correct block within the | |
3075 | * cluster which should hold our entry. | |
3076 | */ | |
3077 | off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), | |
3078 | &lookup->dl_hinfo); | |
3079 | get_bh(dx_leaves[off]); | |
3080 | lookup->dl_dx_leaf_bh = dx_leaves[off]; | |
3081 | } | |
3082 | lookup->dl_dx_root_bh = dx_root_bh; | |
3083 | dx_root_bh = NULL; | |
9b7895ef | 3084 | } |
5b6a3a2b MF |
3085 | |
3086 | out_commit: | |
a90714c1 | 3087 | if (ret < 0 && did_quota) |
5dd4056d | 3088 | dquot_free_space_nodirty(dir, bytes_allocated); |
9b7895ef | 3089 | |
5b6a3a2b MF |
3090 | ocfs2_commit_trans(osb, handle); |
3091 | ||
5b6a3a2b | 3092 | out: |
edd45c08 | 3093 | up_write(&oi->ip_alloc_sem); |
5b6a3a2b MF |
3094 | if (data_ac) |
3095 | ocfs2_free_alloc_context(data_ac); | |
9b7895ef MF |
3096 | if (meta_ac) |
3097 | ocfs2_free_alloc_context(meta_ac); | |
3098 | ||
3099 | if (dx_leaves) { | |
3100 | for (i = 0; i < num_dx_leaves; i++) | |
3101 | brelse(dx_leaves[i]); | |
3102 | kfree(dx_leaves); | |
3103 | } | |
5b6a3a2b MF |
3104 | |
3105 | brelse(dirdata_bh); | |
9b7895ef | 3106 | brelse(dx_root_bh); |
5b6a3a2b MF |
3107 | |
3108 | return ret; | |
3109 | } | |
3110 | ||
ccd979bd | 3111 | /* returns a bh of the 1st new block in the allocation. */ |
316f4b9f MF |
3112 | static int ocfs2_do_extend_dir(struct super_block *sb, |
3113 | handle_t *handle, | |
3114 | struct inode *dir, | |
3115 | struct buffer_head *parent_fe_bh, | |
3116 | struct ocfs2_alloc_context *data_ac, | |
3117 | struct ocfs2_alloc_context *meta_ac, | |
3118 | struct buffer_head **new_bh) | |
ccd979bd MF |
3119 | { |
3120 | int status; | |
a90714c1 | 3121 | int extend, did_quota = 0; |
8110b073 | 3122 | u64 p_blkno, v_blkno; |
ccd979bd MF |
3123 | |
3124 | spin_lock(&OCFS2_I(dir)->ip_lock); | |
3125 | extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); | |
3126 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
3127 | ||
3128 | if (extend) { | |
dcd0538f MF |
3129 | u32 offset = OCFS2_I(dir)->ip_clusters; |
3130 | ||
5dd4056d CH |
3131 | status = dquot_alloc_space_nodirty(dir, |
3132 | ocfs2_clusters_to_bytes(sb, 1)); | |
3133 | if (status) | |
a90714c1 | 3134 | goto bail; |
a90714c1 JK |
3135 | did_quota = 1; |
3136 | ||
0eb8d47e TM |
3137 | status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset, |
3138 | 1, 0, parent_fe_bh, handle, | |
3139 | data_ac, meta_ac, NULL); | |
ccd979bd MF |
3140 | BUG_ON(status == -EAGAIN); |
3141 | if (status < 0) { | |
3142 | mlog_errno(status); | |
3143 | goto bail; | |
3144 | } | |
3145 | } | |
3146 | ||
8110b073 MF |
3147 | v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir)); |
3148 | status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL); | |
ccd979bd MF |
3149 | if (status < 0) { |
3150 | mlog_errno(status); | |
3151 | goto bail; | |
3152 | } | |
3153 | ||
3154 | *new_bh = sb_getblk(sb, p_blkno); | |
3155 | if (!*new_bh) { | |
7391a294 | 3156 | status = -ENOMEM; |
ccd979bd MF |
3157 | mlog_errno(status); |
3158 | goto bail; | |
3159 | } | |
3160 | status = 0; | |
3161 | bail: | |
a90714c1 | 3162 | if (did_quota && status < 0) |
5dd4056d | 3163 | dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1)); |
ccd979bd MF |
3164 | return status; |
3165 | } | |
3166 | ||
5b6a3a2b MF |
3167 | /* |
3168 | * Assumes you already have a cluster lock on the directory. | |
3169 | * | |
3170 | * 'blocks_wanted' is only used if we have an inline directory which | |
3171 | * is to be turned into an extent based one. The size of the dirent to | |
3172 | * insert might be larger than the space gained by growing to just one | |
3173 | * block, so we may have to grow the inode by two blocks in that case. | |
e7c17e43 MF |
3174 | * |
3175 | * If the directory is already indexed, dx_root_bh must be provided. | |
5b6a3a2b | 3176 | */ |
ccd979bd MF |
3177 | static int ocfs2_extend_dir(struct ocfs2_super *osb, |
3178 | struct inode *dir, | |
3179 | struct buffer_head *parent_fe_bh, | |
5b6a3a2b | 3180 | unsigned int blocks_wanted, |
9b7895ef | 3181 | struct ocfs2_dir_lookup_result *lookup, |
ccd979bd MF |
3182 | struct buffer_head **new_de_bh) |
3183 | { | |
3184 | int status = 0; | |
ee19a779 | 3185 | int credits, num_free_extents, drop_alloc_sem = 0; |
ccd979bd MF |
3186 | loff_t dir_i_size; |
3187 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; | |
811f933d | 3188 | struct ocfs2_extent_list *el = &fe->id2.i_list; |
ccd979bd MF |
3189 | struct ocfs2_alloc_context *data_ac = NULL; |
3190 | struct ocfs2_alloc_context *meta_ac = NULL; | |
1fabe148 | 3191 | handle_t *handle = NULL; |
ccd979bd MF |
3192 | struct buffer_head *new_bh = NULL; |
3193 | struct ocfs2_dir_entry * de; | |
3194 | struct super_block *sb = osb->sb; | |
f99b9b7c | 3195 | struct ocfs2_extent_tree et; |
e7c17e43 | 3196 | struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; |
ccd979bd | 3197 | |
5b6a3a2b | 3198 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
e7c17e43 MF |
3199 | /* |
3200 | * This would be a code error as an inline directory should | |
3201 | * never have an index root. | |
3202 | */ | |
3203 | BUG_ON(dx_root_bh); | |
3204 | ||
5b6a3a2b | 3205 | status = ocfs2_expand_inline_dir(dir, parent_fe_bh, |
9b7895ef MF |
3206 | blocks_wanted, lookup, |
3207 | &new_bh); | |
5b6a3a2b MF |
3208 | if (status) { |
3209 | mlog_errno(status); | |
3210 | goto bail; | |
3211 | } | |
3212 | ||
e7c17e43 MF |
3213 | /* Expansion from inline to an indexed directory will |
3214 | * have given us this. */ | |
3215 | dx_root_bh = lookup->dl_dx_root_bh; | |
3216 | ||
5b6a3a2b MF |
3217 | if (blocks_wanted == 1) { |
3218 | /* | |
3219 | * If the new dirent will fit inside the space | |
3220 | * created by pushing out to one block, then | |
3221 | * we can complete the operation | |
3222 | * here. Otherwise we have to expand i_size | |
3223 | * and format the 2nd block below. | |
3224 | */ | |
3225 | BUG_ON(new_bh == NULL); | |
3226 | goto bail_bh; | |
3227 | } | |
3228 | ||
3229 | /* | |
3230 | * Get rid of 'new_bh' - we want to format the 2nd | |
3231 | * data block and return that instead. | |
3232 | */ | |
3233 | brelse(new_bh); | |
3234 | new_bh = NULL; | |
3235 | ||
edd45c08 JK |
3236 | down_write(&OCFS2_I(dir)->ip_alloc_sem); |
3237 | drop_alloc_sem = 1; | |
5b6a3a2b MF |
3238 | dir_i_size = i_size_read(dir); |
3239 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; | |
3240 | goto do_extend; | |
3241 | } | |
3242 | ||
edd45c08 JK |
3243 | down_write(&OCFS2_I(dir)->ip_alloc_sem); |
3244 | drop_alloc_sem = 1; | |
ccd979bd | 3245 | dir_i_size = i_size_read(dir); |
f1088d47 TM |
3246 | trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno, |
3247 | dir_i_size); | |
ccd979bd | 3248 | |
ccd979bd MF |
3249 | /* dir->i_size is always block aligned. */ |
3250 | spin_lock(&OCFS2_I(dir)->ip_lock); | |
3251 | if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { | |
3252 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
5e404e9e JB |
3253 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), |
3254 | parent_fe_bh); | |
3d03a305 | 3255 | num_free_extents = ocfs2_num_free_extents(osb, &et); |
ccd979bd MF |
3256 | if (num_free_extents < 0) { |
3257 | status = num_free_extents; | |
3258 | mlog_errno(status); | |
3259 | goto bail; | |
3260 | } | |
3261 | ||
3262 | if (!num_free_extents) { | |
811f933d | 3263 | status = ocfs2_reserve_new_metadata(osb, el, &meta_ac); |
ccd979bd MF |
3264 | if (status < 0) { |
3265 | if (status != -ENOSPC) | |
3266 | mlog_errno(status); | |
3267 | goto bail; | |
3268 | } | |
3269 | } | |
3270 | ||
da5cbf2f | 3271 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
ccd979bd MF |
3272 | if (status < 0) { |
3273 | if (status != -ENOSPC) | |
3274 | mlog_errno(status); | |
3275 | goto bail; | |
3276 | } | |
3277 | ||
83f92318 MF |
3278 | if (ocfs2_dir_resv_allowed(osb)) |
3279 | data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv; | |
e3b4a97d | 3280 | |
06f9da6e | 3281 | credits = ocfs2_calc_extend_credits(sb, el); |
ccd979bd MF |
3282 | } else { |
3283 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
3284 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; | |
3285 | } | |
3286 | ||
5b6a3a2b | 3287 | do_extend: |
e7c17e43 MF |
3288 | if (ocfs2_dir_indexed(dir)) |
3289 | credits++; /* For attaching the new dirent block to the | |
3290 | * dx_root */ | |
3291 | ||
65eff9cc | 3292 | handle = ocfs2_start_trans(osb, credits); |
ccd979bd MF |
3293 | if (IS_ERR(handle)) { |
3294 | status = PTR_ERR(handle); | |
3295 | handle = NULL; | |
3296 | mlog_errno(status); | |
3297 | goto bail; | |
3298 | } | |
3299 | ||
3300 | status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, | |
3301 | data_ac, meta_ac, &new_bh); | |
3302 | if (status < 0) { | |
3303 | mlog_errno(status); | |
3304 | goto bail; | |
3305 | } | |
3306 | ||
8cb471e8 | 3307 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh); |
ccd979bd | 3308 | |
0cf2f763 | 3309 | status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh, |
13723d00 | 3310 | OCFS2_JOURNAL_ACCESS_CREATE); |
ccd979bd MF |
3311 | if (status < 0) { |
3312 | mlog_errno(status); | |
3313 | goto bail; | |
3314 | } | |
3315 | memset(new_bh->b_data, 0, sb->s_blocksize); | |
87d35a74 | 3316 | |
ccd979bd MF |
3317 | de = (struct ocfs2_dir_entry *) new_bh->b_data; |
3318 | de->inode = 0; | |
e7c17e43 | 3319 | if (ocfs2_supports_dir_trailer(dir)) { |
87d35a74 | 3320 | de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb)); |
e7c17e43 MF |
3321 | |
3322 | ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len)); | |
3323 | ||
3324 | if (ocfs2_dir_indexed(dir)) { | |
3325 | status = ocfs2_dx_dir_link_trailer(dir, handle, | |
3326 | dx_root_bh, new_bh); | |
3327 | if (status) { | |
3328 | mlog_errno(status); | |
3329 | goto bail; | |
3330 | } | |
3331 | } | |
87d35a74 MF |
3332 | } else { |
3333 | de->rec_len = cpu_to_le16(sb->s_blocksize); | |
3334 | } | |
2931cdcb | 3335 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
ec20cec7 | 3336 | ocfs2_journal_dirty(handle, new_bh); |
ccd979bd MF |
3337 | |
3338 | dir_i_size += dir->i_sb->s_blocksize; | |
3339 | i_size_write(dir, dir_i_size); | |
8110b073 | 3340 | dir->i_blocks = ocfs2_inode_sector_count(dir); |
ccd979bd MF |
3341 | status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
3342 | if (status < 0) { | |
3343 | mlog_errno(status); | |
3344 | goto bail; | |
3345 | } | |
3346 | ||
5b6a3a2b | 3347 | bail_bh: |
ccd979bd MF |
3348 | *new_de_bh = new_bh; |
3349 | get_bh(*new_de_bh); | |
3350 | bail: | |
3351 | if (handle) | |
02dc1af4 | 3352 | ocfs2_commit_trans(osb, handle); |
edd45c08 JK |
3353 | if (drop_alloc_sem) |
3354 | up_write(&OCFS2_I(dir)->ip_alloc_sem); | |
ccd979bd MF |
3355 | |
3356 | if (data_ac) | |
3357 | ocfs2_free_alloc_context(data_ac); | |
3358 | if (meta_ac) | |
3359 | ocfs2_free_alloc_context(meta_ac); | |
3360 | ||
a81cb88b | 3361 | brelse(new_bh); |
ccd979bd | 3362 | |
ccd979bd MF |
3363 | return status; |
3364 | } | |
3365 | ||
5b6a3a2b MF |
3366 | static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh, |
3367 | const char *name, int namelen, | |
3368 | struct buffer_head **ret_de_bh, | |
3369 | unsigned int *blocks_wanted) | |
ccd979bd | 3370 | { |
5b6a3a2b | 3371 | int ret; |
87d35a74 | 3372 | struct super_block *sb = dir->i_sb; |
5b6a3a2b MF |
3373 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
3374 | struct ocfs2_dir_entry *de, *last_de = NULL; | |
3375 | char *de_buf, *limit; | |
3376 | unsigned long offset = 0; | |
87d35a74 MF |
3377 | unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize; |
3378 | ||
3379 | /* | |
3380 | * This calculates how many free bytes we'd have in block zero, should | |
3381 | * this function force expansion to an extent tree. | |
3382 | */ | |
e7c17e43 | 3383 | if (ocfs2_new_dir_wants_trailer(dir)) |
87d35a74 MF |
3384 | free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir); |
3385 | else | |
3386 | free_space = dir->i_sb->s_blocksize - i_size_read(dir); | |
5b6a3a2b MF |
3387 | |
3388 | de_buf = di->id2.i_data.id_data; | |
3389 | limit = de_buf + i_size_read(dir); | |
3390 | rec_len = OCFS2_DIR_REC_LEN(namelen); | |
ccd979bd | 3391 | |
5b6a3a2b MF |
3392 | while (de_buf < limit) { |
3393 | de = (struct ocfs2_dir_entry *)de_buf; | |
ccd979bd | 3394 | |
5b6a3a2b MF |
3395 | if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) { |
3396 | ret = -ENOENT; | |
3397 | goto out; | |
3398 | } | |
3399 | if (ocfs2_match(namelen, name, de)) { | |
3400 | ret = -EEXIST; | |
3401 | goto out; | |
3402 | } | |
87d35a74 MF |
3403 | /* |
3404 | * No need to check for a trailing dirent record here as | |
3405 | * they're not used for inline dirs. | |
3406 | */ | |
3407 | ||
5b6a3a2b MF |
3408 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
3409 | /* Ok, we found a spot. Return this bh and let | |
3410 | * the caller actually fill it in. */ | |
3411 | *ret_de_bh = di_bh; | |
3412 | get_bh(*ret_de_bh); | |
3413 | ret = 0; | |
3414 | goto out; | |
3415 | } | |
ccd979bd | 3416 | |
5b6a3a2b MF |
3417 | last_de = de; |
3418 | de_buf += le16_to_cpu(de->rec_len); | |
3419 | offset += le16_to_cpu(de->rec_len); | |
3420 | } | |
ccd979bd | 3421 | |
5b6a3a2b MF |
3422 | /* |
3423 | * We're going to require expansion of the directory - figure | |
3424 | * out how many blocks we'll need so that a place for the | |
3425 | * dirent can be found. | |
3426 | */ | |
3427 | *blocks_wanted = 1; | |
87d35a74 | 3428 | new_rec_len = le16_to_cpu(last_de->rec_len) + free_space; |
5b6a3a2b MF |
3429 | if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len))) |
3430 | *blocks_wanted = 2; | |
ccd979bd | 3431 | |
5b6a3a2b MF |
3432 | ret = -ENOSPC; |
3433 | out: | |
3434 | return ret; | |
3435 | } | |
3436 | ||
3437 | static int ocfs2_find_dir_space_el(struct inode *dir, const char *name, | |
3438 | int namelen, struct buffer_head **ret_de_bh) | |
3439 | { | |
3440 | unsigned long offset; | |
3441 | struct buffer_head *bh = NULL; | |
3442 | unsigned short rec_len; | |
3443 | struct ocfs2_dir_entry *de; | |
3444 | struct super_block *sb = dir->i_sb; | |
3445 | int status; | |
87d35a74 | 3446 | int blocksize = dir->i_sb->s_blocksize; |
ccd979bd | 3447 | |
a22305cc | 3448 | status = ocfs2_read_dir_block(dir, 0, &bh, 0); |
9b572691 | 3449 | if (status) |
ccd979bd | 3450 | goto bail; |
ccd979bd MF |
3451 | |
3452 | rec_len = OCFS2_DIR_REC_LEN(namelen); | |
3453 | offset = 0; | |
3454 | de = (struct ocfs2_dir_entry *) bh->b_data; | |
3455 | while (1) { | |
3456 | if ((char *)de >= sb->s_blocksize + bh->b_data) { | |
3457 | brelse(bh); | |
3458 | bh = NULL; | |
3459 | ||
3460 | if (i_size_read(dir) <= offset) { | |
5b6a3a2b MF |
3461 | /* |
3462 | * Caller will have to expand this | |
3463 | * directory. | |
3464 | */ | |
3465 | status = -ENOSPC; | |
ccd979bd MF |
3466 | goto bail; |
3467 | } | |
a22305cc JB |
3468 | status = ocfs2_read_dir_block(dir, |
3469 | offset >> sb->s_blocksize_bits, | |
3470 | &bh, 0); | |
9b572691 | 3471 | if (status) |
ccd979bd | 3472 | goto bail; |
9b572691 | 3473 | |
ccd979bd MF |
3474 | /* move to next block */ |
3475 | de = (struct ocfs2_dir_entry *) bh->b_data; | |
3476 | } | |
3477 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { | |
3478 | status = -ENOENT; | |
3479 | goto bail; | |
3480 | } | |
3481 | if (ocfs2_match(namelen, name, de)) { | |
3482 | status = -EEXIST; | |
3483 | goto bail; | |
3484 | } | |
87d35a74 MF |
3485 | |
3486 | if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize, | |
3487 | blocksize)) | |
3488 | goto next; | |
3489 | ||
8553cf4f | 3490 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
ccd979bd MF |
3491 | /* Ok, we found a spot. Return this bh and let |
3492 | * the caller actually fill it in. */ | |
3493 | *ret_de_bh = bh; | |
3494 | get_bh(*ret_de_bh); | |
3495 | status = 0; | |
3496 | goto bail; | |
3497 | } | |
87d35a74 | 3498 | next: |
ccd979bd MF |
3499 | offset += le16_to_cpu(de->rec_len); |
3500 | de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); | |
3501 | } | |
3502 | ||
ccd979bd | 3503 | bail: |
a81cb88b | 3504 | brelse(bh); |
c1e8d35e TM |
3505 | if (status) |
3506 | mlog_errno(status); | |
ccd979bd | 3507 | |
ccd979bd MF |
3508 | return status; |
3509 | } | |
5b6a3a2b | 3510 | |
9b7895ef | 3511 | static int dx_leaf_sort_cmp(const void *a, const void *b) |
5b6a3a2b | 3512 | { |
9b7895ef MF |
3513 | const struct ocfs2_dx_entry *entry1 = a; |
3514 | const struct ocfs2_dx_entry *entry2 = b; | |
3515 | u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash); | |
3516 | u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash); | |
3517 | u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash); | |
3518 | u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash); | |
3519 | ||
3520 | if (major_hash1 > major_hash2) | |
3521 | return 1; | |
3522 | if (major_hash1 < major_hash2) | |
3523 | return -1; | |
3524 | ||
3525 | /* | |
3526 | * It is not strictly necessary to sort by minor | |
3527 | */ | |
3528 | if (minor_hash1 > minor_hash2) | |
3529 | return 1; | |
3530 | if (minor_hash1 < minor_hash2) | |
3531 | return -1; | |
3532 | return 0; | |
3533 | } | |
3534 | ||
3535 | static void dx_leaf_sort_swap(void *a, void *b, int size) | |
3536 | { | |
3537 | struct ocfs2_dx_entry *entry1 = a; | |
3538 | struct ocfs2_dx_entry *entry2 = b; | |
9b7895ef MF |
3539 | |
3540 | BUG_ON(size != sizeof(*entry1)); | |
3541 | ||
2a28f98c | 3542 | swap(*entry1, *entry2); |
9b7895ef MF |
3543 | } |
3544 | ||
3545 | static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf) | |
3546 | { | |
3547 | struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list; | |
3548 | int i, num = le16_to_cpu(dl_list->de_num_used); | |
3549 | ||
3550 | for (i = 0; i < (num - 1); i++) { | |
3551 | if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) != | |
3552 | le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash)) | |
3553 | return 0; | |
3554 | } | |
3555 | ||
3556 | return 1; | |
3557 | } | |
3558 | ||
3559 | /* | |
3560 | * Find the optimal value to split this leaf on. This expects the leaf | |
3561 | * entries to be in sorted order. | |
3562 | * | |
3563 | * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is | |
3564 | * the hash we want to insert. | |
3565 | * | |
3566 | * This function is only concerned with the major hash - that which | |
3567 | * determines which cluster an item belongs to. | |
3568 | */ | |
3569 | static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf, | |
3570 | u32 leaf_cpos, u32 insert_hash, | |
3571 | u32 *split_hash) | |
3572 | { | |
3573 | struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list; | |
3574 | int i, num_used = le16_to_cpu(dl_list->de_num_used); | |
3575 | int allsame; | |
3576 | ||
3577 | /* | |
3578 | * There's a couple rare, but nasty corner cases we have to | |
3579 | * check for here. All of them involve a leaf where all value | |
3580 | * have the same hash, which is what we look for first. | |
3581 | * | |
3582 | * Most of the time, all of the above is false, and we simply | |
3583 | * pick the median value for a split. | |
3584 | */ | |
3585 | allsame = ocfs2_dx_leaf_same_major(dx_leaf); | |
3586 | if (allsame) { | |
3587 | u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash); | |
3588 | ||
3589 | if (val == insert_hash) { | |
3590 | /* | |
3591 | * No matter where we would choose to split, | |
3592 | * the new entry would want to occupy the same | |
3593 | * block as these. Since there's no space left | |
3594 | * in their existing block, we know there | |
3595 | * won't be space after the split. | |
3596 | */ | |
3597 | return -ENOSPC; | |
3598 | } | |
3599 | ||
3600 | if (val == leaf_cpos) { | |
3601 | /* | |
3602 | * Because val is the same as leaf_cpos (which | |
3603 | * is the smallest value this leaf can have), | |
3604 | * yet is not equal to insert_hash, then we | |
3605 | * know that insert_hash *must* be larger than | |
3606 | * val (and leaf_cpos). At least cpos+1 in value. | |
3607 | * | |
3608 | * We also know then, that there cannot be an | |
3609 | * adjacent extent (otherwise we'd be looking | |
3610 | * at it). Choosing this value gives us a | |
3611 | * chance to get some contiguousness. | |
3612 | */ | |
3613 | *split_hash = leaf_cpos + 1; | |
3614 | return 0; | |
3615 | } | |
3616 | ||
3617 | if (val > insert_hash) { | |
3618 | /* | |
3619 | * val can not be the same as insert hash, and | |
3620 | * also must be larger than leaf_cpos. Also, | |
3621 | * we know that there can't be a leaf between | |
3622 | * cpos and val, otherwise the entries with | |
3623 | * hash 'val' would be there. | |
3624 | */ | |
3625 | *split_hash = val; | |
3626 | return 0; | |
3627 | } | |
3628 | ||
3629 | *split_hash = insert_hash; | |
3630 | return 0; | |
3631 | } | |
3632 | ||
3633 | /* | |
3634 | * Since the records are sorted and the checks above | |
3635 | * guaranteed that not all records in this block are the same, | |
3636 | * we simple travel forward, from the median, and pick the 1st | |
3637 | * record whose value is larger than leaf_cpos. | |
3638 | */ | |
3639 | for (i = (num_used / 2); i < num_used; i++) | |
3640 | if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) > | |
3641 | leaf_cpos) | |
3642 | break; | |
3643 | ||
3644 | BUG_ON(i == num_used); /* Should be impossible */ | |
3645 | *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash); | |
3646 | return 0; | |
3647 | } | |
3648 | ||
3649 | /* | |
3650 | * Transfer all entries in orig_dx_leaves whose major hash is equal to or | |
3651 | * larger than split_hash into new_dx_leaves. We use a temporary | |
3652 | * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks. | |
3653 | * | |
3654 | * Since the block offset inside a leaf (cluster) is a constant mask | |
3655 | * of minor_hash, we can optimize - an item at block offset X within | |
3656 | * the original cluster, will be at offset X within the new cluster. | |
3657 | */ | |
3658 | static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash, | |
3659 | handle_t *handle, | |
3660 | struct ocfs2_dx_leaf *tmp_dx_leaf, | |
3661 | struct buffer_head **orig_dx_leaves, | |
3662 | struct buffer_head **new_dx_leaves, | |
3663 | int num_dx_leaves) | |
3664 | { | |
3665 | int i, j, num_used; | |
3666 | u32 major_hash; | |
3667 | struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf; | |
3668 | struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list; | |
3669 | struct ocfs2_dx_entry *dx_entry; | |
3670 | ||
3671 | tmp_list = &tmp_dx_leaf->dl_list; | |
3672 | ||
3673 | for (i = 0; i < num_dx_leaves; i++) { | |
3674 | orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data; | |
3675 | orig_list = &orig_dx_leaf->dl_list; | |
3676 | new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data; | |
3677 | new_list = &new_dx_leaf->dl_list; | |
3678 | ||
3679 | num_used = le16_to_cpu(orig_list->de_num_used); | |
3680 | ||
3681 | memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize); | |
3682 | tmp_list->de_num_used = cpu_to_le16(0); | |
3683 | memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used); | |
3684 | ||
3685 | for (j = 0; j < num_used; j++) { | |
3686 | dx_entry = &orig_list->de_entries[j]; | |
3687 | major_hash = le32_to_cpu(dx_entry->dx_major_hash); | |
3688 | if (major_hash >= split_hash) | |
3689 | ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf, | |
3690 | dx_entry); | |
3691 | else | |
3692 | ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf, | |
3693 | dx_entry); | |
3694 | } | |
3695 | memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize); | |
3696 | ||
3697 | ocfs2_journal_dirty(handle, orig_dx_leaves[i]); | |
3698 | ocfs2_journal_dirty(handle, new_dx_leaves[i]); | |
3699 | } | |
3700 | } | |
3701 | ||
3702 | static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb, | |
3703 | struct ocfs2_dx_root_block *dx_root) | |
3704 | { | |
3705 | int credits = ocfs2_clusters_to_blocks(osb->sb, 2); | |
3706 | ||
06f9da6e | 3707 | credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list); |
9b7895ef MF |
3708 | credits += ocfs2_quota_trans_credits(osb->sb); |
3709 | return credits; | |
3710 | } | |
3711 | ||
3712 | /* | |
3713 | * Find the median value in dx_leaf_bh and allocate a new leaf to move | |
3714 | * half our entries into. | |
3715 | */ | |
3716 | static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir, | |
3717 | struct buffer_head *dx_root_bh, | |
3718 | struct buffer_head *dx_leaf_bh, | |
3719 | struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos, | |
3720 | u64 leaf_blkno) | |
3721 | { | |
3722 | struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; | |
3723 | int credits, ret, i, num_used, did_quota = 0; | |
3724 | u32 cpos, split_hash, insert_hash = hinfo->major_hash; | |
3725 | u64 orig_leaves_start; | |
3726 | int num_dx_leaves; | |
3727 | struct buffer_head **orig_dx_leaves = NULL; | |
3728 | struct buffer_head **new_dx_leaves = NULL; | |
3729 | struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL; | |
3730 | struct ocfs2_extent_tree et; | |
3731 | handle_t *handle = NULL; | |
3732 | struct ocfs2_dx_root_block *dx_root; | |
3733 | struct ocfs2_dx_leaf *tmp_dx_leaf = NULL; | |
3734 | ||
f1088d47 TM |
3735 | trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno, |
3736 | (unsigned long long)leaf_blkno, | |
3737 | insert_hash); | |
9b7895ef | 3738 | |
5e404e9e | 3739 | ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); |
9b7895ef MF |
3740 | |
3741 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; | |
3742 | /* | |
3743 | * XXX: This is a rather large limit. We should use a more | |
3744 | * realistic value. | |
3745 | */ | |
3746 | if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX) | |
3747 | return -ENOSPC; | |
3748 | ||
3749 | num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used); | |
3750 | if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) { | |
3751 | mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: " | |
3752 | "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno, | |
3753 | (unsigned long long)leaf_blkno, num_used); | |
3754 | ret = -EIO; | |
3755 | goto out; | |
3756 | } | |
3757 | ||
3758 | orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves); | |
3759 | if (!orig_dx_leaves) { | |
3760 | ret = -ENOMEM; | |
3761 | mlog_errno(ret); | |
3762 | goto out; | |
3763 | } | |
3764 | ||
3765 | new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL); | |
3766 | if (!new_dx_leaves) { | |
3767 | ret = -ENOMEM; | |
3768 | mlog_errno(ret); | |
3769 | goto out; | |
3770 | } | |
3771 | ||
3772 | ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac); | |
3773 | if (ret) { | |
3774 | if (ret != -ENOSPC) | |
3775 | mlog_errno(ret); | |
3776 | goto out; | |
3777 | } | |
3778 | ||
3779 | credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root); | |
3780 | handle = ocfs2_start_trans(osb, credits); | |
3781 | if (IS_ERR(handle)) { | |
3782 | ret = PTR_ERR(handle); | |
3783 | handle = NULL; | |
3784 | mlog_errno(ret); | |
3785 | goto out; | |
3786 | } | |
3787 | ||
5dd4056d CH |
3788 | ret = dquot_alloc_space_nodirty(dir, |
3789 | ocfs2_clusters_to_bytes(dir->i_sb, 1)); | |
3790 | if (ret) | |
9b7895ef | 3791 | goto out_commit; |
9b7895ef MF |
3792 | did_quota = 1; |
3793 | ||
0cf2f763 | 3794 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh, |
9b7895ef MF |
3795 | OCFS2_JOURNAL_ACCESS_WRITE); |
3796 | if (ret) { | |
3797 | mlog_errno(ret); | |
3798 | goto out_commit; | |
3799 | } | |
3800 | ||
3801 | /* | |
3802 | * This block is changing anyway, so we can sort it in place. | |
3803 | */ | |
3804 | sort(dx_leaf->dl_list.de_entries, num_used, | |
3805 | sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp, | |
3806 | dx_leaf_sort_swap); | |
3807 | ||
ec20cec7 | 3808 | ocfs2_journal_dirty(handle, dx_leaf_bh); |
9b7895ef MF |
3809 | |
3810 | ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash, | |
3811 | &split_hash); | |
3812 | if (ret) { | |
3813 | mlog_errno(ret); | |
3814 | goto out_commit; | |
3815 | } | |
3816 | ||
f1088d47 | 3817 | trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash); |
9b7895ef MF |
3818 | |
3819 | /* | |
3820 | * We have to carefully order operations here. There are items | |
3821 | * which want to be in the new cluster before insert, but in | |
3822 | * order to put those items in the new cluster, we alter the | |
3823 | * old cluster. A failure to insert gets nasty. | |
3824 | * | |
3825 | * So, start by reserving writes to the old | |
3826 | * cluster. ocfs2_dx_dir_new_cluster will reserve writes on | |
3827 | * the new cluster for us, before inserting it. The insert | |
3828 | * won't happen if there's an error before that. Once the | |
3829 | * insert is done then, we can transfer from one leaf into the | |
3830 | * other without fear of hitting any error. | |
3831 | */ | |
3832 | ||
3833 | /* | |
3834 | * The leaf transfer wants some scratch space so that we don't | |
3835 | * wind up doing a bunch of expensive memmove(). | |
3836 | */ | |
3837 | tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS); | |
3838 | if (!tmp_dx_leaf) { | |
3839 | ret = -ENOMEM; | |
3840 | mlog_errno(ret); | |
3841 | goto out_commit; | |
3842 | } | |
3843 | ||
1d46dc08 | 3844 | orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno); |
9b7895ef MF |
3845 | ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves, |
3846 | orig_dx_leaves); | |
3847 | if (ret) { | |
3848 | mlog_errno(ret); | |
3849 | goto out_commit; | |
3850 | } | |
3851 | ||
0f4da216 TY |
3852 | cpos = split_hash; |
3853 | ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle, | |
3854 | data_ac, meta_ac, new_dx_leaves, | |
3855 | num_dx_leaves); | |
3856 | if (ret) { | |
3857 | mlog_errno(ret); | |
3858 | goto out_commit; | |
3859 | } | |
3860 | ||
9b7895ef | 3861 | for (i = 0; i < num_dx_leaves; i++) { |
0cf2f763 JB |
3862 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), |
3863 | orig_dx_leaves[i], | |
9b7895ef MF |
3864 | OCFS2_JOURNAL_ACCESS_WRITE); |
3865 | if (ret) { | |
3866 | mlog_errno(ret); | |
3867 | goto out_commit; | |
3868 | } | |
9b7895ef | 3869 | |
0f4da216 TY |
3870 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), |
3871 | new_dx_leaves[i], | |
3872 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3873 | if (ret) { | |
3874 | mlog_errno(ret); | |
3875 | goto out_commit; | |
3876 | } | |
9b7895ef MF |
3877 | } |
3878 | ||
3879 | ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf, | |
3880 | orig_dx_leaves, new_dx_leaves, num_dx_leaves); | |
3881 | ||
3882 | out_commit: | |
3883 | if (ret < 0 && did_quota) | |
5dd4056d | 3884 | dquot_free_space_nodirty(dir, |
9b7895ef MF |
3885 | ocfs2_clusters_to_bytes(dir->i_sb, 1)); |
3886 | ||
2931cdcb | 3887 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
9b7895ef MF |
3888 | ocfs2_commit_trans(osb, handle); |
3889 | ||
3890 | out: | |
3891 | if (orig_dx_leaves || new_dx_leaves) { | |
3892 | for (i = 0; i < num_dx_leaves; i++) { | |
3893 | if (orig_dx_leaves) | |
3894 | brelse(orig_dx_leaves[i]); | |
3895 | if (new_dx_leaves) | |
3896 | brelse(new_dx_leaves[i]); | |
3897 | } | |
3898 | kfree(orig_dx_leaves); | |
3899 | kfree(new_dx_leaves); | |
3900 | } | |
3901 | ||
3902 | if (meta_ac) | |
3903 | ocfs2_free_alloc_context(meta_ac); | |
3904 | if (data_ac) | |
3905 | ocfs2_free_alloc_context(data_ac); | |
3906 | ||
3907 | kfree(tmp_dx_leaf); | |
3908 | return ret; | |
3909 | } | |
3910 | ||
e7c17e43 MF |
3911 | static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir, |
3912 | struct buffer_head *di_bh, | |
3913 | struct buffer_head *dx_root_bh, | |
3914 | const char *name, int namelen, | |
3915 | struct ocfs2_dir_lookup_result *lookup) | |
3916 | { | |
3917 | int ret, rebalanced = 0; | |
3918 | struct ocfs2_dx_root_block *dx_root; | |
3919 | struct buffer_head *dx_leaf_bh = NULL; | |
3920 | struct ocfs2_dx_leaf *dx_leaf; | |
3921 | u64 blkno; | |
3922 | u32 leaf_cpos; | |
3923 | ||
3924 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; | |
3925 | ||
3926 | restart_search: | |
3927 | ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo, | |
3928 | &leaf_cpos, &blkno); | |
3929 | if (ret) { | |
3930 | mlog_errno(ret); | |
3931 | goto out; | |
3932 | } | |
3933 | ||
3934 | ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh); | |
3935 | if (ret) { | |
3936 | mlog_errno(ret); | |
3937 | goto out; | |
3938 | } | |
3939 | ||
3940 | dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; | |
3941 | ||
3942 | if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >= | |
3943 | le16_to_cpu(dx_leaf->dl_list.de_count)) { | |
3944 | if (rebalanced) { | |
3945 | /* | |
3946 | * Rebalancing should have provided us with | |
3947 | * space in an appropriate leaf. | |
3948 | * | |
3949 | * XXX: Is this an abnormal condition then? | |
3950 | * Should we print a message here? | |
3951 | */ | |
3952 | ret = -ENOSPC; | |
3953 | goto out; | |
3954 | } | |
3955 | ||
3956 | ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh, | |
3957 | &lookup->dl_hinfo, leaf_cpos, | |
3958 | blkno); | |
3959 | if (ret) { | |
3960 | if (ret != -ENOSPC) | |
3961 | mlog_errno(ret); | |
3962 | goto out; | |
3963 | } | |
3964 | ||
3965 | /* | |
3966 | * Restart the lookup. The rebalance might have | |
3967 | * changed which block our item fits into. Mark our | |
3968 | * progress, so we only execute this once. | |
3969 | */ | |
3970 | brelse(dx_leaf_bh); | |
3971 | dx_leaf_bh = NULL; | |
3972 | rebalanced = 1; | |
3973 | goto restart_search; | |
3974 | } | |
3975 | ||
3976 | lookup->dl_dx_leaf_bh = dx_leaf_bh; | |
3977 | dx_leaf_bh = NULL; | |
3978 | ||
3979 | out: | |
3980 | brelse(dx_leaf_bh); | |
3981 | return ret; | |
3982 | } | |
3983 | ||
3984 | static int ocfs2_search_dx_free_list(struct inode *dir, | |
3985 | struct buffer_head *dx_root_bh, | |
3986 | int namelen, | |
3987 | struct ocfs2_dir_lookup_result *lookup) | |
3988 | { | |
3989 | int ret = -ENOSPC; | |
3990 | struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL; | |
3991 | struct ocfs2_dir_block_trailer *db; | |
3992 | u64 next_block; | |
3993 | int rec_len = OCFS2_DIR_REC_LEN(namelen); | |
3994 | struct ocfs2_dx_root_block *dx_root; | |
3995 | ||
3996 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; | |
3997 | next_block = le64_to_cpu(dx_root->dr_free_blk); | |
3998 | ||
3999 | while (next_block) { | |
4000 | brelse(prev_leaf_bh); | |
4001 | prev_leaf_bh = leaf_bh; | |
4002 | leaf_bh = NULL; | |
4003 | ||
4004 | ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh); | |
4005 | if (ret) { | |
4006 | mlog_errno(ret); | |
4007 | goto out; | |
4008 | } | |
4009 | ||
4010 | db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb); | |
4011 | if (rec_len <= le16_to_cpu(db->db_free_rec_len)) { | |
4012 | lookup->dl_leaf_bh = leaf_bh; | |
4013 | lookup->dl_prev_leaf_bh = prev_leaf_bh; | |
4014 | leaf_bh = NULL; | |
4015 | prev_leaf_bh = NULL; | |
4016 | break; | |
4017 | } | |
4018 | ||
4019 | next_block = le64_to_cpu(db->db_free_next); | |
4020 | } | |
4021 | ||
4022 | if (!next_block) | |
4023 | ret = -ENOSPC; | |
4024 | ||
4025 | out: | |
4026 | ||
4027 | brelse(leaf_bh); | |
4028 | brelse(prev_leaf_bh); | |
4029 | return ret; | |
4030 | } | |
4031 | ||
4ed8a6bb MF |
4032 | static int ocfs2_expand_inline_dx_root(struct inode *dir, |
4033 | struct buffer_head *dx_root_bh) | |
4034 | { | |
4035 | int ret, num_dx_leaves, i, j, did_quota = 0; | |
4036 | struct buffer_head **dx_leaves = NULL; | |
4037 | struct ocfs2_extent_tree et; | |
4038 | u64 insert_blkno; | |
4039 | struct ocfs2_alloc_context *data_ac = NULL; | |
4040 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); | |
4041 | handle_t *handle = NULL; | |
4042 | struct ocfs2_dx_root_block *dx_root; | |
4043 | struct ocfs2_dx_entry_list *entry_list; | |
4044 | struct ocfs2_dx_entry *dx_entry; | |
4045 | struct ocfs2_dx_leaf *target_leaf; | |
4046 | ||
4047 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); | |
4048 | if (ret) { | |
4049 | mlog_errno(ret); | |
4050 | goto out; | |
4051 | } | |
4052 | ||
4053 | dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves); | |
4054 | if (!dx_leaves) { | |
4055 | ret = -ENOMEM; | |
4056 | mlog_errno(ret); | |
4057 | goto out; | |
4058 | } | |
4059 | ||
4060 | handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb)); | |
4061 | if (IS_ERR(handle)) { | |
4062 | ret = PTR_ERR(handle); | |
4063 | mlog_errno(ret); | |
4064 | goto out; | |
4065 | } | |
4066 | ||
5dd4056d CH |
4067 | ret = dquot_alloc_space_nodirty(dir, |
4068 | ocfs2_clusters_to_bytes(osb->sb, 1)); | |
4069 | if (ret) | |
4ed8a6bb | 4070 | goto out_commit; |
4ed8a6bb MF |
4071 | did_quota = 1; |
4072 | ||
4073 | /* | |
4074 | * We do this up front, before the allocation, so that a | |
4075 | * failure to add the dx_root_bh to the journal won't result | |
4076 | * us losing clusters. | |
4077 | */ | |
0cf2f763 | 4078 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
4ed8a6bb MF |
4079 | OCFS2_JOURNAL_ACCESS_WRITE); |
4080 | if (ret) { | |
4081 | mlog_errno(ret); | |
4082 | goto out_commit; | |
4083 | } | |
4084 | ||
4085 | ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves, | |
4086 | num_dx_leaves, &insert_blkno); | |
4087 | if (ret) { | |
4088 | mlog_errno(ret); | |
4089 | goto out_commit; | |
4090 | } | |
4091 | ||
4092 | /* | |
4093 | * Transfer the entries from our dx_root into the appropriate | |
4094 | * block | |
4095 | */ | |
4096 | dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; | |
4097 | entry_list = &dx_root->dr_entries; | |
4098 | ||
4099 | for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) { | |
4100 | dx_entry = &entry_list->de_entries[i]; | |
4101 | ||
4102 | j = __ocfs2_dx_dir_hash_idx(osb, | |
4103 | le32_to_cpu(dx_entry->dx_minor_hash)); | |
4104 | target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data; | |
4105 | ||
4106 | ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry); | |
4107 | ||
4108 | /* Each leaf has been passed to the journal already | |
4109 | * via __ocfs2_dx_dir_new_cluster() */ | |
4110 | } | |
4111 | ||
4112 | dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE; | |
4113 | memset(&dx_root->dr_list, 0, osb->sb->s_blocksize - | |
4114 | offsetof(struct ocfs2_dx_root_block, dr_list)); | |
4115 | dx_root->dr_list.l_count = | |
4116 | cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb)); | |
4117 | ||
4118 | /* This should never fail considering we start with an empty | |
4119 | * dx_root. */ | |
5e404e9e | 4120 | ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); |
cc79d8c1 | 4121 | ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL); |
4ed8a6bb MF |
4122 | if (ret) |
4123 | mlog_errno(ret); | |
4124 | did_quota = 0; | |
4125 | ||
2931cdcb | 4126 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
4ed8a6bb MF |
4127 | ocfs2_journal_dirty(handle, dx_root_bh); |
4128 | ||
4129 | out_commit: | |
4130 | if (ret < 0 && did_quota) | |
5dd4056d | 4131 | dquot_free_space_nodirty(dir, |
4ed8a6bb MF |
4132 | ocfs2_clusters_to_bytes(dir->i_sb, 1)); |
4133 | ||
4134 | ocfs2_commit_trans(osb, handle); | |
4135 | ||
4136 | out: | |
4137 | if (data_ac) | |
4138 | ocfs2_free_alloc_context(data_ac); | |
4139 | ||
4140 | if (dx_leaves) { | |
4141 | for (i = 0; i < num_dx_leaves; i++) | |
4142 | brelse(dx_leaves[i]); | |
4143 | kfree(dx_leaves); | |
4144 | } | |
4145 | return ret; | |
4146 | } | |
4147 | ||
4148 | static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh) | |
4149 | { | |
4150 | struct ocfs2_dx_root_block *dx_root; | |
4151 | struct ocfs2_dx_entry_list *entry_list; | |
4152 | ||
4153 | dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; | |
4154 | entry_list = &dx_root->dr_entries; | |
4155 | ||
4156 | if (le16_to_cpu(entry_list->de_num_used) >= | |
4157 | le16_to_cpu(entry_list->de_count)) | |
4158 | return -ENOSPC; | |
4159 | ||
4160 | return 0; | |
4161 | } | |
4162 | ||
e7c17e43 MF |
4163 | static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir, |
4164 | struct buffer_head *di_bh, | |
4165 | const char *name, | |
4166 | int namelen, | |
4167 | struct ocfs2_dir_lookup_result *lookup) | |
9b7895ef | 4168 | { |
e7c17e43 MF |
4169 | int ret, free_dx_root = 1; |
4170 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); | |
9b7895ef | 4171 | struct buffer_head *dx_root_bh = NULL; |
e7c17e43 | 4172 | struct buffer_head *leaf_bh = NULL; |
9b7895ef | 4173 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
e7c17e43 | 4174 | struct ocfs2_dx_root_block *dx_root; |
9b7895ef MF |
4175 | |
4176 | ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); | |
4177 | if (ret) { | |
4178 | mlog_errno(ret); | |
4179 | goto out; | |
4180 | } | |
4181 | ||
4182 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; | |
e3a93c2d MF |
4183 | if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) { |
4184 | ret = -ENOSPC; | |
4185 | mlog_errno(ret); | |
4186 | goto out; | |
4187 | } | |
4188 | ||
4ed8a6bb MF |
4189 | if (ocfs2_dx_root_inline(dx_root)) { |
4190 | ret = ocfs2_inline_dx_has_space(dx_root_bh); | |
4191 | ||
4192 | if (ret == 0) | |
4193 | goto search_el; | |
4194 | ||
4195 | /* | |
4196 | * We ran out of room in the root block. Expand it to | |
4197 | * an extent, then allow ocfs2_find_dir_space_dx to do | |
4198 | * the rest. | |
4199 | */ | |
4200 | ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh); | |
4201 | if (ret) { | |
4202 | mlog_errno(ret); | |
4203 | goto out; | |
4204 | } | |
4205 | } | |
9b7895ef | 4206 | |
e7c17e43 MF |
4207 | /* |
4208 | * Insert preparation for an indexed directory is split into two | |
4209 | * steps. The call to find_dir_space_dx reserves room in the index for | |
4210 | * an additional item. If we run out of space there, it's a real error | |
4211 | * we can't continue on. | |
4212 | */ | |
4213 | ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name, | |
4214 | namelen, lookup); | |
9b7895ef MF |
4215 | if (ret) { |
4216 | mlog_errno(ret); | |
4217 | goto out; | |
4218 | } | |
4219 | ||
e7c17e43 MF |
4220 | search_el: |
4221 | /* | |
4222 | * Next, we need to find space in the unindexed tree. This call | |
4223 | * searches using the free space linked list. If the unindexed tree | |
4224 | * lacks sufficient space, we'll expand it below. The expansion code | |
4225 | * is smart enough to add any new blocks to the free space list. | |
4226 | */ | |
4227 | ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup); | |
4228 | if (ret && ret != -ENOSPC) { | |
9b7895ef MF |
4229 | mlog_errno(ret); |
4230 | goto out; | |
4231 | } | |
4232 | ||
e7c17e43 MF |
4233 | /* Do this up here - ocfs2_extend_dir might need the dx_root */ |
4234 | lookup->dl_dx_root_bh = dx_root_bh; | |
4235 | free_dx_root = 0; | |
9b7895ef | 4236 | |
e7c17e43 MF |
4237 | if (ret == -ENOSPC) { |
4238 | ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh); | |
9b7895ef | 4239 | |
9b7895ef | 4240 | if (ret) { |
e7c17e43 | 4241 | mlog_errno(ret); |
9b7895ef MF |
4242 | goto out; |
4243 | } | |
4244 | ||
4245 | /* | |
e7c17e43 MF |
4246 | * We make the assumption here that new leaf blocks are added |
4247 | * to the front of our free list. | |
9b7895ef | 4248 | */ |
e7c17e43 MF |
4249 | lookup->dl_prev_leaf_bh = NULL; |
4250 | lookup->dl_leaf_bh = leaf_bh; | |
9b7895ef MF |
4251 | } |
4252 | ||
9b7895ef | 4253 | out: |
e7c17e43 MF |
4254 | if (free_dx_root) |
4255 | brelse(dx_root_bh); | |
9b7895ef MF |
4256 | return ret; |
4257 | } | |
4258 | ||
4259 | /* | |
4260 | * Get a directory ready for insert. Any directory allocation required | |
4261 | * happens here. Success returns zero, and enough context in the dir | |
4262 | * lookup result that ocfs2_add_entry() will be able complete the task | |
4263 | * with minimal performance impact. | |
4264 | */ | |
4265 | int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, | |
4266 | struct inode *dir, | |
4267 | struct buffer_head *parent_fe_bh, | |
4268 | const char *name, | |
4269 | int namelen, | |
4270 | struct ocfs2_dir_lookup_result *lookup) | |
4271 | { | |
4272 | int ret; | |
5b6a3a2b MF |
4273 | unsigned int blocks_wanted = 1; |
4274 | struct buffer_head *bh = NULL; | |
4275 | ||
f1088d47 TM |
4276 | trace_ocfs2_prepare_dir_for_insert( |
4277 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen); | |
5b6a3a2b | 4278 | |
5b6a3a2b MF |
4279 | if (!namelen) { |
4280 | ret = -EINVAL; | |
4281 | mlog_errno(ret); | |
4282 | goto out; | |
4283 | } | |
4284 | ||
9b7895ef MF |
4285 | /* |
4286 | * Do this up front to reduce confusion. | |
4287 | * | |
4288 | * The directory might start inline, then be turned into an | |
4289 | * indexed one, in which case we'd need to hash deep inside | |
4290 | * ocfs2_find_dir_space_id(). Since | |
4291 | * ocfs2_prepare_dx_dir_for_insert() also needs this hash | |
4292 | * done, there seems no point in spreading out the calls. We | |
4293 | * can optimize away the case where the file system doesn't | |
4294 | * support indexing. | |
4295 | */ | |
4296 | if (ocfs2_supports_indexed_dirs(osb)) | |
4297 | ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo); | |
4298 | ||
4299 | if (ocfs2_dir_indexed(dir)) { | |
e7c17e43 MF |
4300 | ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh, |
4301 | name, namelen, lookup); | |
4302 | if (ret) | |
9b7895ef | 4303 | mlog_errno(ret); |
e7c17e43 | 4304 | goto out; |
9b7895ef MF |
4305 | } |
4306 | ||
5b6a3a2b MF |
4307 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
4308 | ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name, | |
4309 | namelen, &bh, &blocks_wanted); | |
4310 | } else | |
4311 | ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh); | |
4312 | ||
4313 | if (ret && ret != -ENOSPC) { | |
4314 | mlog_errno(ret); | |
4315 | goto out; | |
4316 | } | |
4317 | ||
4318 | if (ret == -ENOSPC) { | |
4319 | /* | |
4320 | * We have to expand the directory to add this name. | |
4321 | */ | |
4322 | BUG_ON(bh); | |
4323 | ||
4324 | ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted, | |
9b7895ef | 4325 | lookup, &bh); |
5b6a3a2b MF |
4326 | if (ret) { |
4327 | if (ret != -ENOSPC) | |
4328 | mlog_errno(ret); | |
4329 | goto out; | |
4330 | } | |
4331 | ||
4332 | BUG_ON(!bh); | |
4333 | } | |
4334 | ||
4a12ca3a | 4335 | lookup->dl_leaf_bh = bh; |
5b6a3a2b MF |
4336 | bh = NULL; |
4337 | out: | |
a81cb88b | 4338 | brelse(bh); |
5b6a3a2b MF |
4339 | return ret; |
4340 | } | |
9b7895ef MF |
4341 | |
4342 | static int ocfs2_dx_dir_remove_index(struct inode *dir, | |
4343 | struct buffer_head *di_bh, | |
4344 | struct buffer_head *dx_root_bh) | |
4345 | { | |
4346 | int ret; | |
4347 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); | |
4348 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
4349 | struct ocfs2_dx_root_block *dx_root; | |
4350 | struct inode *dx_alloc_inode = NULL; | |
4351 | struct buffer_head *dx_alloc_bh = NULL; | |
4352 | handle_t *handle; | |
4353 | u64 blk; | |
4354 | u16 bit; | |
4355 | u64 bg_blkno; | |
4356 | ||
4357 | dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; | |
4358 | ||
4359 | dx_alloc_inode = ocfs2_get_system_file_inode(osb, | |
4360 | EXTENT_ALLOC_SYSTEM_INODE, | |
4361 | le16_to_cpu(dx_root->dr_suballoc_slot)); | |
4362 | if (!dx_alloc_inode) { | |
4363 | ret = -ENOMEM; | |
4364 | mlog_errno(ret); | |
4365 | goto out; | |
4366 | } | |
4367 | mutex_lock(&dx_alloc_inode->i_mutex); | |
4368 | ||
4369 | ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1); | |
4370 | if (ret) { | |
4371 | mlog_errno(ret); | |
4372 | goto out_mutex; | |
4373 | } | |
4374 | ||
4375 | handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS); | |
4376 | if (IS_ERR(handle)) { | |
4377 | ret = PTR_ERR(handle); | |
4378 | mlog_errno(ret); | |
4379 | goto out_unlock; | |
4380 | } | |
4381 | ||
0cf2f763 | 4382 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, |
9b7895ef MF |
4383 | OCFS2_JOURNAL_ACCESS_WRITE); |
4384 | if (ret) { | |
4385 | mlog_errno(ret); | |
4386 | goto out_commit; | |
4387 | } | |
4388 | ||
8ac33dc8 | 4389 | spin_lock(&OCFS2_I(dir)->ip_lock); |
9b7895ef MF |
4390 | OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL; |
4391 | di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features); | |
8ac33dc8 | 4392 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
9b7895ef | 4393 | di->i_dx_root = cpu_to_le64(0ULL); |
6fdb702d | 4394 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
9b7895ef MF |
4395 | |
4396 | ocfs2_journal_dirty(handle, di_bh); | |
4397 | ||
4398 | blk = le64_to_cpu(dx_root->dr_blkno); | |
4399 | bit = le16_to_cpu(dx_root->dr_suballoc_bit); | |
74380c47 TM |
4400 | if (dx_root->dr_suballoc_loc) |
4401 | bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc); | |
4402 | else | |
4403 | bg_blkno = ocfs2_which_suballoc_group(blk, bit); | |
9b7895ef MF |
4404 | ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh, |
4405 | bit, bg_blkno, 1); | |
4406 | if (ret) | |
4407 | mlog_errno(ret); | |
4408 | ||
4409 | out_commit: | |
4410 | ocfs2_commit_trans(osb, handle); | |
4411 | ||
4412 | out_unlock: | |
4413 | ocfs2_inode_unlock(dx_alloc_inode, 1); | |
4414 | ||
4415 | out_mutex: | |
4416 | mutex_unlock(&dx_alloc_inode->i_mutex); | |
4417 | brelse(dx_alloc_bh); | |
4418 | out: | |
4419 | iput(dx_alloc_inode); | |
4420 | return ret; | |
4421 | } | |
4422 | ||
4423 | int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh) | |
4424 | { | |
4425 | int ret; | |
4426 | unsigned int uninitialized_var(clen); | |
4427 | u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos); | |
4428 | u64 uninitialized_var(blkno); | |
4429 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); | |
4430 | struct buffer_head *dx_root_bh = NULL; | |
4431 | struct ocfs2_dx_root_block *dx_root; | |
4432 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
4433 | struct ocfs2_cached_dealloc_ctxt dealloc; | |
4434 | struct ocfs2_extent_tree et; | |
4435 | ||
4436 | ocfs2_init_dealloc_ctxt(&dealloc); | |
4437 | ||
4438 | if (!ocfs2_dir_indexed(dir)) | |
4439 | return 0; | |
4440 | ||
4441 | ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); | |
4442 | if (ret) { | |
4443 | mlog_errno(ret); | |
4444 | goto out; | |
4445 | } | |
4ed8a6bb | 4446 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
9b7895ef | 4447 | |
4ed8a6bb MF |
4448 | if (ocfs2_dx_root_inline(dx_root)) |
4449 | goto remove_index; | |
9b7895ef | 4450 | |
5e404e9e | 4451 | ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); |
9b7895ef MF |
4452 | |
4453 | /* XXX: What if dr_clusters is too large? */ | |
4454 | while (le32_to_cpu(dx_root->dr_clusters)) { | |
4455 | ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list, | |
4456 | major_hash, &cpos, &blkno, &clen); | |
4457 | if (ret) { | |
4458 | mlog_errno(ret); | |
4459 | goto out; | |
4460 | } | |
4461 | ||
4462 | p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno); | |
4463 | ||
78f94673 | 4464 | ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0, |
f62f12b3 | 4465 | &dealloc, 0, false); |
9b7895ef MF |
4466 | if (ret) { |
4467 | mlog_errno(ret); | |
4468 | goto out; | |
4469 | } | |
4470 | ||
4471 | if (cpos == 0) | |
4472 | break; | |
4473 | ||
4474 | major_hash = cpos - 1; | |
4475 | } | |
4476 | ||
4ed8a6bb | 4477 | remove_index: |
9b7895ef MF |
4478 | ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh); |
4479 | if (ret) { | |
4480 | mlog_errno(ret); | |
4481 | goto out; | |
4482 | } | |
4483 | ||
8cb471e8 | 4484 | ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh); |
9b7895ef MF |
4485 | out: |
4486 | ocfs2_schedule_truncate_log_flush(osb, 1); | |
4487 | ocfs2_run_deallocs(osb, &dealloc); | |
4488 | ||
4489 | brelse(dx_root_bh); | |
4490 | return ret; | |
4491 | } |