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 | * | |
21 | * Copyright (C) 1991, 1992 Linux Torvalds | |
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> |
ccd979bd MF |
44 | |
45 | #define MLOG_MASK_PREFIX ML_NAMEI | |
46 | #include <cluster/masklog.h> | |
47 | ||
48 | #include "ocfs2.h" | |
49 | ||
50 | #include "alloc.h" | |
51 | #include "dir.h" | |
52 | #include "dlmglue.h" | |
53 | #include "extent_map.h" | |
54 | #include "file.h" | |
55 | #include "inode.h" | |
56 | #include "journal.h" | |
57 | #include "namei.h" | |
58 | #include "suballoc.h" | |
316f4b9f | 59 | #include "super.h" |
ccd979bd MF |
60 | #include "uptodate.h" |
61 | ||
62 | #include "buffer_head_io.h" | |
63 | ||
316f4b9f MF |
64 | #define NAMEI_RA_CHUNKS 2 |
65 | #define NAMEI_RA_BLOCKS 4 | |
66 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) | |
67 | #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b)) | |
68 | ||
ccd979bd MF |
69 | static unsigned char ocfs2_filetype_table[] = { |
70 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | |
71 | }; | |
72 | ||
73 | static int ocfs2_extend_dir(struct ocfs2_super *osb, | |
74 | struct inode *dir, | |
75 | struct buffer_head *parent_fe_bh, | |
5b6a3a2b | 76 | unsigned int blocks_wanted, |
ccd979bd | 77 | struct buffer_head **new_de_bh); |
316f4b9f MF |
78 | static int ocfs2_do_extend_dir(struct super_block *sb, |
79 | handle_t *handle, | |
80 | struct inode *dir, | |
81 | struct buffer_head *parent_fe_bh, | |
82 | struct ocfs2_alloc_context *data_ac, | |
83 | struct ocfs2_alloc_context *meta_ac, | |
84 | struct buffer_head **new_bh); | |
85 | ||
23193e51 MF |
86 | /* |
87 | * bh passed here can be an inode block or a dir data block, depending | |
88 | * on the inode inline data flag. | |
89 | */ | |
5eae5b96 MF |
90 | static int ocfs2_check_dir_entry(struct inode * dir, |
91 | struct ocfs2_dir_entry * de, | |
92 | struct buffer_head * bh, | |
93 | unsigned long offset) | |
316f4b9f MF |
94 | { |
95 | const char *error_msg = NULL; | |
96 | const int rlen = le16_to_cpu(de->rec_len); | |
97 | ||
98 | if (rlen < OCFS2_DIR_REC_LEN(1)) | |
99 | error_msg = "rec_len is smaller than minimal"; | |
100 | else if (rlen % 4 != 0) | |
101 | error_msg = "rec_len % 4 != 0"; | |
102 | else if (rlen < OCFS2_DIR_REC_LEN(de->name_len)) | |
103 | error_msg = "rec_len is too small for name_len"; | |
104 | else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize) | |
105 | error_msg = "directory entry across blocks"; | |
106 | ||
107 | if (error_msg != NULL) | |
108 | mlog(ML_ERROR, "bad entry in directory #%llu: %s - " | |
109 | "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n", | |
110 | (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg, | |
111 | offset, (unsigned long long)le64_to_cpu(de->inode), rlen, | |
112 | de->name_len); | |
113 | return error_msg == NULL ? 1 : 0; | |
114 | } | |
115 | ||
116 | static inline int ocfs2_match(int len, | |
117 | const char * const name, | |
118 | struct ocfs2_dir_entry *de) | |
119 | { | |
120 | if (len != de->name_len) | |
121 | return 0; | |
122 | if (!de->inode) | |
123 | return 0; | |
124 | return !memcmp(name, de->name, len); | |
125 | } | |
126 | ||
127 | /* | |
128 | * Returns 0 if not found, -1 on failure, and 1 on success | |
129 | */ | |
130 | static int inline ocfs2_search_dirblock(struct buffer_head *bh, | |
131 | struct inode *dir, | |
132 | const char *name, int namelen, | |
133 | unsigned long offset, | |
23193e51 MF |
134 | char *first_de, |
135 | unsigned int bytes, | |
316f4b9f MF |
136 | struct ocfs2_dir_entry **res_dir) |
137 | { | |
138 | struct ocfs2_dir_entry *de; | |
139 | char *dlimit, *de_buf; | |
140 | int de_len; | |
141 | int ret = 0; | |
142 | ||
143 | mlog_entry_void(); | |
144 | ||
23193e51 MF |
145 | de_buf = first_de; |
146 | dlimit = de_buf + bytes; | |
316f4b9f MF |
147 | |
148 | while (de_buf < dlimit) { | |
149 | /* this code is executed quadratically often */ | |
150 | /* do minimal checking `by hand' */ | |
151 | ||
152 | de = (struct ocfs2_dir_entry *) de_buf; | |
153 | ||
154 | if (de_buf + namelen <= dlimit && | |
155 | ocfs2_match(namelen, name, de)) { | |
156 | /* found a match - just to be sure, do a full check */ | |
157 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { | |
158 | ret = -1; | |
159 | goto bail; | |
160 | } | |
161 | *res_dir = de; | |
162 | ret = 1; | |
163 | goto bail; | |
164 | } | |
165 | ||
166 | /* prevent looping on a bad block */ | |
167 | de_len = le16_to_cpu(de->rec_len); | |
168 | if (de_len <= 0) { | |
169 | ret = -1; | |
170 | goto bail; | |
171 | } | |
172 | ||
173 | de_buf += de_len; | |
174 | offset += de_len; | |
175 | } | |
176 | ||
177 | bail: | |
178 | mlog_exit(ret); | |
179 | return ret; | |
180 | } | |
181 | ||
23193e51 MF |
182 | static struct buffer_head *ocfs2_find_entry_id(const char *name, |
183 | int namelen, | |
184 | struct inode *dir, | |
185 | struct ocfs2_dir_entry **res_dir) | |
186 | { | |
187 | int ret, found; | |
188 | struct buffer_head *di_bh = NULL; | |
189 | struct ocfs2_dinode *di; | |
190 | struct ocfs2_inline_data *data; | |
191 | ||
b657c95c | 192 | ret = ocfs2_read_inode_block(dir, &di_bh); |
23193e51 MF |
193 | if (ret) { |
194 | mlog_errno(ret); | |
195 | goto out; | |
196 | } | |
197 | ||
198 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
199 | data = &di->id2.i_data; | |
200 | ||
201 | found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0, | |
202 | data->id_data, i_size_read(dir), res_dir); | |
203 | if (found == 1) | |
204 | return di_bh; | |
205 | ||
206 | brelse(di_bh); | |
207 | out: | |
208 | return NULL; | |
209 | } | |
210 | ||
a22305cc JB |
211 | static int ocfs2_validate_dir_block(struct super_block *sb, |
212 | struct buffer_head *bh) | |
213 | { | |
214 | /* | |
215 | * Nothing yet. We don't validate dirents here, that's handled | |
216 | * in-place when the code walks them. | |
217 | */ | |
970e4936 JB |
218 | mlog(0, "Validating dirblock %llu\n", |
219 | (unsigned long long)bh->b_blocknr); | |
a22305cc JB |
220 | |
221 | return 0; | |
222 | } | |
223 | ||
224 | /* | |
225 | * This function forces all errors to -EIO for consistency with its | |
226 | * predecessor, ocfs2_bread(). We haven't audited what returning the | |
227 | * real error codes would do to callers. We log the real codes with | |
228 | * mlog_errno() before we squash them. | |
229 | */ | |
230 | static int ocfs2_read_dir_block(struct inode *inode, u64 v_block, | |
231 | struct buffer_head **bh, int flags) | |
232 | { | |
233 | int rc = 0; | |
234 | struct buffer_head *tmp = *bh; | |
a22305cc | 235 | |
511308d9 JB |
236 | rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags, |
237 | ocfs2_validate_dir_block); | |
238 | if (rc) | |
a22305cc | 239 | mlog_errno(rc); |
a22305cc | 240 | |
511308d9 JB |
241 | /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */ |
242 | if (!rc && !*bh) | |
a22305cc JB |
243 | *bh = tmp; |
244 | ||
a22305cc JB |
245 | return rc ? -EIO : 0; |
246 | } | |
247 | ||
0af4bd38 AB |
248 | static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen, |
249 | struct inode *dir, | |
250 | struct ocfs2_dir_entry **res_dir) | |
316f4b9f MF |
251 | { |
252 | struct super_block *sb; | |
253 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; | |
254 | struct buffer_head *bh, *ret = NULL; | |
255 | unsigned long start, block, b; | |
256 | int ra_max = 0; /* Number of bh's in the readahead | |
257 | buffer, bh_use[] */ | |
258 | int ra_ptr = 0; /* Current index into readahead | |
259 | buffer */ | |
260 | int num = 0; | |
261 | int nblocks, i, err; | |
262 | ||
263 | mlog_entry_void(); | |
264 | ||
316f4b9f MF |
265 | sb = dir->i_sb; |
266 | ||
267 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; | |
268 | start = OCFS2_I(dir)->ip_dir_start_lookup; | |
269 | if (start >= nblocks) | |
270 | start = 0; | |
271 | block = start; | |
272 | ||
273 | restart: | |
274 | do { | |
275 | /* | |
276 | * We deal with the read-ahead logic here. | |
277 | */ | |
278 | if (ra_ptr >= ra_max) { | |
279 | /* Refill the readahead buffer */ | |
280 | ra_ptr = 0; | |
281 | b = block; | |
282 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { | |
283 | /* | |
284 | * Terminate if we reach the end of the | |
285 | * directory and must wrap, or if our | |
286 | * search has finished at this block. | |
287 | */ | |
288 | if (b >= nblocks || (num && block == start)) { | |
289 | bh_use[ra_max] = NULL; | |
290 | break; | |
291 | } | |
292 | num++; | |
293 | ||
a22305cc JB |
294 | bh = NULL; |
295 | err = ocfs2_read_dir_block(dir, b++, &bh, | |
296 | OCFS2_BH_READAHEAD); | |
316f4b9f MF |
297 | bh_use[ra_max] = bh; |
298 | } | |
299 | } | |
300 | if ((bh = bh_use[ra_ptr++]) == NULL) | |
301 | goto next; | |
a22305cc | 302 | if (ocfs2_read_dir_block(dir, block, &bh, 0)) { |
5e0b3dec | 303 | /* read error, skip block & hope for the best. |
a22305cc | 304 | * ocfs2_read_dir_block() has released the bh. */ |
316f4b9f MF |
305 | ocfs2_error(dir->i_sb, "reading directory %llu, " |
306 | "offset %lu\n", | |
307 | (unsigned long long)OCFS2_I(dir)->ip_blkno, | |
308 | block); | |
316f4b9f MF |
309 | goto next; |
310 | } | |
311 | i = ocfs2_search_dirblock(bh, dir, name, namelen, | |
312 | block << sb->s_blocksize_bits, | |
23193e51 | 313 | bh->b_data, sb->s_blocksize, |
316f4b9f MF |
314 | res_dir); |
315 | if (i == 1) { | |
316 | OCFS2_I(dir)->ip_dir_start_lookup = block; | |
317 | ret = bh; | |
318 | goto cleanup_and_exit; | |
319 | } else { | |
320 | brelse(bh); | |
321 | if (i < 0) | |
322 | goto cleanup_and_exit; | |
323 | } | |
324 | next: | |
325 | if (++block >= nblocks) | |
326 | block = 0; | |
327 | } while (block != start); | |
328 | ||
329 | /* | |
330 | * If the directory has grown while we were searching, then | |
331 | * search the last part of the directory before giving up. | |
332 | */ | |
333 | block = nblocks; | |
334 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; | |
335 | if (block < nblocks) { | |
336 | start = 0; | |
337 | goto restart; | |
338 | } | |
339 | ||
340 | cleanup_and_exit: | |
341 | /* Clean up the read-ahead blocks */ | |
342 | for (; ra_ptr < ra_max; ra_ptr++) | |
343 | brelse(bh_use[ra_ptr]); | |
344 | ||
345 | mlog_exit_ptr(ret); | |
346 | return ret; | |
347 | } | |
348 | ||
23193e51 MF |
349 | /* |
350 | * Try to find an entry of the provided name within 'dir'. | |
351 | * | |
352 | * If nothing was found, NULL is returned. Otherwise, a buffer_head | |
353 | * and pointer to the dir entry are passed back. | |
354 | * | |
355 | * Caller can NOT assume anything about the contents of the | |
356 | * buffer_head - it is passed back only so that it can be passed into | |
357 | * any one of the manipulation functions (add entry, delete entry, | |
358 | * etc). As an example, bh in the extent directory case is a data | |
359 | * block, in the inline-data case it actually points to an inode. | |
360 | */ | |
361 | struct buffer_head *ocfs2_find_entry(const char *name, int namelen, | |
362 | struct inode *dir, | |
363 | struct ocfs2_dir_entry **res_dir) | |
364 | { | |
365 | *res_dir = NULL; | |
366 | ||
367 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
368 | return ocfs2_find_entry_id(name, namelen, dir, res_dir); | |
369 | ||
370 | return ocfs2_find_entry_el(name, namelen, dir, res_dir); | |
371 | } | |
372 | ||
5b6a3a2b MF |
373 | /* |
374 | * Update inode number and type of a previously found directory entry. | |
375 | */ | |
38760e24 MF |
376 | int ocfs2_update_entry(struct inode *dir, handle_t *handle, |
377 | struct buffer_head *de_bh, struct ocfs2_dir_entry *de, | |
378 | struct inode *new_entry_inode) | |
379 | { | |
380 | int ret; | |
381 | ||
5b6a3a2b MF |
382 | /* |
383 | * The same code works fine for both inline-data and extent | |
384 | * based directories, so no need to split this up. | |
385 | */ | |
386 | ||
38760e24 MF |
387 | ret = ocfs2_journal_access(handle, dir, de_bh, |
388 | OCFS2_JOURNAL_ACCESS_WRITE); | |
389 | if (ret) { | |
390 | mlog_errno(ret); | |
391 | goto out; | |
392 | } | |
393 | ||
394 | de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno); | |
395 | ocfs2_set_de_type(de, new_entry_inode->i_mode); | |
396 | ||
397 | ocfs2_journal_dirty(handle, de_bh); | |
398 | ||
399 | out: | |
400 | return ret; | |
401 | } | |
402 | ||
5b6a3a2b MF |
403 | static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir, |
404 | struct ocfs2_dir_entry *de_del, | |
405 | struct buffer_head *bh, char *first_de, | |
406 | unsigned int bytes) | |
316f4b9f MF |
407 | { |
408 | struct ocfs2_dir_entry *de, *pde; | |
409 | int i, status = -ENOENT; | |
410 | ||
411 | mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh); | |
412 | ||
413 | i = 0; | |
414 | pde = NULL; | |
5b6a3a2b MF |
415 | de = (struct ocfs2_dir_entry *) first_de; |
416 | while (i < bytes) { | |
316f4b9f MF |
417 | if (!ocfs2_check_dir_entry(dir, de, bh, i)) { |
418 | status = -EIO; | |
419 | mlog_errno(status); | |
420 | goto bail; | |
421 | } | |
422 | if (de == de_del) { | |
423 | status = ocfs2_journal_access(handle, dir, bh, | |
424 | OCFS2_JOURNAL_ACCESS_WRITE); | |
425 | if (status < 0) { | |
426 | status = -EIO; | |
427 | mlog_errno(status); | |
428 | goto bail; | |
429 | } | |
430 | if (pde) | |
0dd3256e MS |
431 | le16_add_cpu(&pde->rec_len, |
432 | le16_to_cpu(de->rec_len)); | |
316f4b9f MF |
433 | else |
434 | de->inode = 0; | |
435 | dir->i_version++; | |
436 | status = ocfs2_journal_dirty(handle, bh); | |
437 | goto bail; | |
438 | } | |
439 | i += le16_to_cpu(de->rec_len); | |
440 | pde = de; | |
441 | de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len)); | |
442 | } | |
443 | bail: | |
444 | mlog_exit(status); | |
445 | return status; | |
446 | } | |
447 | ||
5b6a3a2b MF |
448 | static inline int ocfs2_delete_entry_id(handle_t *handle, |
449 | struct inode *dir, | |
450 | struct ocfs2_dir_entry *de_del, | |
451 | struct buffer_head *bh) | |
452 | { | |
453 | int ret; | |
454 | struct buffer_head *di_bh = NULL; | |
455 | struct ocfs2_dinode *di; | |
456 | struct ocfs2_inline_data *data; | |
457 | ||
b657c95c | 458 | ret = ocfs2_read_inode_block(dir, &di_bh); |
5b6a3a2b MF |
459 | if (ret) { |
460 | mlog_errno(ret); | |
461 | goto out; | |
462 | } | |
463 | ||
464 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
465 | data = &di->id2.i_data; | |
466 | ||
467 | ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data, | |
468 | i_size_read(dir)); | |
469 | ||
470 | brelse(di_bh); | |
471 | out: | |
472 | return ret; | |
473 | } | |
474 | ||
475 | static inline int ocfs2_delete_entry_el(handle_t *handle, | |
476 | struct inode *dir, | |
477 | struct ocfs2_dir_entry *de_del, | |
478 | struct buffer_head *bh) | |
479 | { | |
480 | return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data, | |
481 | bh->b_size); | |
482 | } | |
483 | ||
484 | /* | |
485 | * ocfs2_delete_entry deletes a directory entry by merging it with the | |
486 | * previous entry | |
487 | */ | |
488 | int ocfs2_delete_entry(handle_t *handle, | |
489 | struct inode *dir, | |
490 | struct ocfs2_dir_entry *de_del, | |
491 | struct buffer_head *bh) | |
492 | { | |
493 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
494 | return ocfs2_delete_entry_id(handle, dir, de_del, bh); | |
495 | ||
496 | return ocfs2_delete_entry_el(handle, dir, de_del, bh); | |
497 | } | |
498 | ||
8553cf4f MF |
499 | /* |
500 | * Check whether 'de' has enough room to hold an entry of | |
501 | * 'new_rec_len' bytes. | |
502 | */ | |
503 | static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de, | |
504 | unsigned int new_rec_len) | |
505 | { | |
506 | unsigned int de_really_used; | |
507 | ||
508 | /* Check whether this is an empty record with enough space */ | |
509 | if (le64_to_cpu(de->inode) == 0 && | |
510 | le16_to_cpu(de->rec_len) >= new_rec_len) | |
511 | return 1; | |
512 | ||
513 | /* | |
514 | * Record might have free space at the end which we can | |
515 | * use. | |
516 | */ | |
517 | de_really_used = OCFS2_DIR_REC_LEN(de->name_len); | |
518 | if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len)) | |
519 | return 1; | |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
316f4b9f MF |
524 | /* we don't always have a dentry for what we want to add, so people |
525 | * like orphan dir can call this instead. | |
526 | * | |
527 | * If you pass me insert_bh, I'll skip the search of the other dir | |
528 | * blocks and put the record in there. | |
529 | */ | |
530 | int __ocfs2_add_entry(handle_t *handle, | |
531 | struct inode *dir, | |
532 | const char *name, int namelen, | |
533 | struct inode *inode, u64 blkno, | |
534 | struct buffer_head *parent_fe_bh, | |
535 | struct buffer_head *insert_bh) | |
536 | { | |
537 | unsigned long offset; | |
538 | unsigned short rec_len; | |
539 | struct ocfs2_dir_entry *de, *de1; | |
5b6a3a2b MF |
540 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data; |
541 | struct super_block *sb = dir->i_sb; | |
316f4b9f | 542 | int retval, status; |
5b6a3a2b MF |
543 | unsigned int size = sb->s_blocksize; |
544 | char *data_start = insert_bh->b_data; | |
316f4b9f MF |
545 | |
546 | mlog_entry_void(); | |
547 | ||
316f4b9f MF |
548 | if (!namelen) |
549 | return -EINVAL; | |
550 | ||
5b6a3a2b MF |
551 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
552 | data_start = di->id2.i_data.id_data; | |
553 | size = i_size_read(dir); | |
554 | ||
555 | BUG_ON(insert_bh != parent_fe_bh); | |
556 | } | |
557 | ||
316f4b9f MF |
558 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
559 | offset = 0; | |
5b6a3a2b | 560 | de = (struct ocfs2_dir_entry *) data_start; |
316f4b9f | 561 | while (1) { |
5b6a3a2b MF |
562 | BUG_ON((char *)de >= (size + data_start)); |
563 | ||
316f4b9f MF |
564 | /* These checks should've already been passed by the |
565 | * prepare function, but I guess we can leave them | |
566 | * here anyway. */ | |
567 | if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) { | |
568 | retval = -ENOENT; | |
569 | goto bail; | |
570 | } | |
571 | if (ocfs2_match(namelen, name, de)) { | |
572 | retval = -EEXIST; | |
573 | goto bail; | |
574 | } | |
8553cf4f MF |
575 | |
576 | if (ocfs2_dirent_would_fit(de, rec_len)) { | |
316f4b9f MF |
577 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
578 | retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); | |
579 | if (retval < 0) { | |
580 | mlog_errno(retval); | |
581 | goto bail; | |
582 | } | |
583 | ||
584 | status = ocfs2_journal_access(handle, dir, insert_bh, | |
585 | OCFS2_JOURNAL_ACCESS_WRITE); | |
586 | /* By now the buffer is marked for journaling */ | |
587 | offset += le16_to_cpu(de->rec_len); | |
588 | if (le64_to_cpu(de->inode)) { | |
589 | de1 = (struct ocfs2_dir_entry *)((char *) de + | |
590 | OCFS2_DIR_REC_LEN(de->name_len)); | |
591 | de1->rec_len = | |
592 | cpu_to_le16(le16_to_cpu(de->rec_len) - | |
593 | OCFS2_DIR_REC_LEN(de->name_len)); | |
594 | de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); | |
595 | de = de1; | |
596 | } | |
597 | de->file_type = OCFS2_FT_UNKNOWN; | |
598 | if (blkno) { | |
599 | de->inode = cpu_to_le64(blkno); | |
600 | ocfs2_set_de_type(de, inode->i_mode); | |
601 | } else | |
602 | de->inode = 0; | |
603 | de->name_len = namelen; | |
604 | memcpy(de->name, name, namelen); | |
605 | ||
606 | dir->i_version++; | |
607 | status = ocfs2_journal_dirty(handle, insert_bh); | |
608 | retval = 0; | |
609 | goto bail; | |
610 | } | |
611 | offset += le16_to_cpu(de->rec_len); | |
612 | de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len)); | |
613 | } | |
614 | ||
615 | /* when you think about it, the assert above should prevent us | |
616 | * from ever getting here. */ | |
617 | retval = -ENOSPC; | |
618 | bail: | |
619 | ||
620 | mlog_exit(retval); | |
621 | return retval; | |
622 | } | |
623 | ||
23193e51 | 624 | static int ocfs2_dir_foreach_blk_id(struct inode *inode, |
2b47c361 | 625 | u64 *f_version, |
23193e51 | 626 | loff_t *f_pos, void *priv, |
e7b34019 | 627 | filldir_t filldir, int *filldir_err) |
23193e51 MF |
628 | { |
629 | int ret, i, filldir_ret; | |
630 | unsigned long offset = *f_pos; | |
631 | struct buffer_head *di_bh = NULL; | |
632 | struct ocfs2_dinode *di; | |
633 | struct ocfs2_inline_data *data; | |
634 | struct ocfs2_dir_entry *de; | |
635 | ||
b657c95c | 636 | ret = ocfs2_read_inode_block(inode, &di_bh); |
23193e51 MF |
637 | if (ret) { |
638 | mlog(ML_ERROR, "Unable to read inode block for dir %llu\n", | |
639 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
640 | goto out; | |
641 | } | |
642 | ||
643 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
644 | data = &di->id2.i_data; | |
645 | ||
646 | while (*f_pos < i_size_read(inode)) { | |
647 | revalidate: | |
648 | /* If the dir block has changed since the last call to | |
649 | * readdir(2), then we might be pointing to an invalid | |
650 | * dirent right now. Scan from the start of the block | |
651 | * to make sure. */ | |
652 | if (*f_version != inode->i_version) { | |
653 | for (i = 0; i < i_size_read(inode) && i < offset; ) { | |
654 | de = (struct ocfs2_dir_entry *) | |
655 | (data->id_data + i); | |
656 | /* It's too expensive to do a full | |
657 | * dirent test each time round this | |
658 | * loop, but we do have to test at | |
659 | * least that it is non-zero. A | |
660 | * failure will be detected in the | |
661 | * dirent test below. */ | |
662 | if (le16_to_cpu(de->rec_len) < | |
663 | OCFS2_DIR_REC_LEN(1)) | |
664 | break; | |
665 | i += le16_to_cpu(de->rec_len); | |
666 | } | |
667 | *f_pos = offset = i; | |
668 | *f_version = inode->i_version; | |
669 | } | |
670 | ||
671 | de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos); | |
672 | if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) { | |
673 | /* On error, skip the f_pos to the end. */ | |
674 | *f_pos = i_size_read(inode); | |
675 | goto out; | |
676 | } | |
677 | offset += le16_to_cpu(de->rec_len); | |
678 | if (le64_to_cpu(de->inode)) { | |
679 | /* We might block in the next section | |
680 | * if the data destination is | |
681 | * currently swapped out. So, use a | |
682 | * version stamp to detect whether or | |
683 | * not the directory has been modified | |
684 | * during the copy operation. | |
685 | */ | |
2b47c361 | 686 | u64 version = *f_version; |
23193e51 MF |
687 | unsigned char d_type = DT_UNKNOWN; |
688 | ||
689 | if (de->file_type < OCFS2_FT_MAX) | |
690 | d_type = ocfs2_filetype_table[de->file_type]; | |
691 | ||
692 | filldir_ret = filldir(priv, de->name, | |
693 | de->name_len, | |
694 | *f_pos, | |
695 | le64_to_cpu(de->inode), | |
696 | d_type); | |
e7b34019 MF |
697 | if (filldir_ret) { |
698 | if (filldir_err) | |
699 | *filldir_err = filldir_ret; | |
23193e51 | 700 | break; |
e7b34019 | 701 | } |
23193e51 MF |
702 | if (version != *f_version) |
703 | goto revalidate; | |
704 | } | |
705 | *f_pos += le16_to_cpu(de->rec_len); | |
706 | } | |
707 | ||
708 | out: | |
709 | brelse(di_bh); | |
710 | ||
711 | return 0; | |
712 | } | |
713 | ||
714 | static int ocfs2_dir_foreach_blk_el(struct inode *inode, | |
2b47c361 | 715 | u64 *f_version, |
23193e51 | 716 | loff_t *f_pos, void *priv, |
e7b34019 | 717 | filldir_t filldir, int *filldir_err) |
ccd979bd MF |
718 | { |
719 | int error = 0; | |
aa958874 MF |
720 | unsigned long offset, blk, last_ra_blk = 0; |
721 | int i, stored; | |
ccd979bd MF |
722 | struct buffer_head * bh, * tmp; |
723 | struct ocfs2_dir_entry * de; | |
ccd979bd | 724 | struct super_block * sb = inode->i_sb; |
aa958874 | 725 | unsigned int ra_sectors = 16; |
ccd979bd MF |
726 | |
727 | stored = 0; | |
728 | bh = NULL; | |
729 | ||
b8bc5f4f | 730 | offset = (*f_pos) & (sb->s_blocksize - 1); |
ccd979bd | 731 | |
b8bc5f4f MF |
732 | while (!error && !stored && *f_pos < i_size_read(inode)) { |
733 | blk = (*f_pos) >> sb->s_blocksize_bits; | |
a22305cc JB |
734 | if (ocfs2_read_dir_block(inode, blk, &bh, 0)) { |
735 | /* Skip the corrupt dirblock and keep trying */ | |
b8bc5f4f | 736 | *f_pos += sb->s_blocksize - offset; |
ccd979bd MF |
737 | continue; |
738 | } | |
739 | ||
aa958874 MF |
740 | /* The idea here is to begin with 8k read-ahead and to stay |
741 | * 4k ahead of our current position. | |
742 | * | |
743 | * TODO: Use the pagecache for this. We just need to | |
744 | * make sure it's cluster-safe... */ | |
745 | if (!last_ra_blk | |
746 | || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { | |
747 | for (i = ra_sectors >> (sb->s_blocksize_bits - 9); | |
ccd979bd | 748 | i > 0; i--) { |
a22305cc JB |
749 | tmp = NULL; |
750 | if (!ocfs2_read_dir_block(inode, ++blk, &tmp, | |
751 | OCFS2_BH_READAHEAD)) | |
752 | brelse(tmp); | |
ccd979bd | 753 | } |
aa958874 MF |
754 | last_ra_blk = blk; |
755 | ra_sectors = 8; | |
ccd979bd MF |
756 | } |
757 | ||
758 | revalidate: | |
759 | /* If the dir block has changed since the last call to | |
760 | * readdir(2), then we might be pointing to an invalid | |
761 | * dirent right now. Scan from the start of the block | |
762 | * to make sure. */ | |
b8bc5f4f | 763 | if (*f_version != inode->i_version) { |
ccd979bd MF |
764 | for (i = 0; i < sb->s_blocksize && i < offset; ) { |
765 | de = (struct ocfs2_dir_entry *) (bh->b_data + i); | |
766 | /* It's too expensive to do a full | |
767 | * dirent test each time round this | |
768 | * loop, but we do have to test at | |
769 | * least that it is non-zero. A | |
770 | * failure will be detected in the | |
771 | * dirent test below. */ | |
772 | if (le16_to_cpu(de->rec_len) < | |
773 | OCFS2_DIR_REC_LEN(1)) | |
774 | break; | |
775 | i += le16_to_cpu(de->rec_len); | |
776 | } | |
777 | offset = i; | |
b8bc5f4f | 778 | *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1)) |
ccd979bd | 779 | | offset; |
b8bc5f4f | 780 | *f_version = inode->i_version; |
ccd979bd MF |
781 | } |
782 | ||
b8bc5f4f | 783 | while (!error && *f_pos < i_size_read(inode) |
ccd979bd MF |
784 | && offset < sb->s_blocksize) { |
785 | de = (struct ocfs2_dir_entry *) (bh->b_data + offset); | |
786 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { | |
787 | /* On error, skip the f_pos to the | |
788 | next block. */ | |
b8bc5f4f | 789 | *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1; |
ccd979bd | 790 | brelse(bh); |
b8bc5f4f | 791 | goto out; |
ccd979bd MF |
792 | } |
793 | offset += le16_to_cpu(de->rec_len); | |
794 | if (le64_to_cpu(de->inode)) { | |
795 | /* We might block in the next section | |
796 | * if the data destination is | |
797 | * currently swapped out. So, use a | |
798 | * version stamp to detect whether or | |
799 | * not the directory has been modified | |
800 | * during the copy operation. | |
801 | */ | |
b8bc5f4f | 802 | unsigned long version = *f_version; |
ccd979bd MF |
803 | unsigned char d_type = DT_UNKNOWN; |
804 | ||
805 | if (de->file_type < OCFS2_FT_MAX) | |
806 | d_type = ocfs2_filetype_table[de->file_type]; | |
b8bc5f4f | 807 | error = filldir(priv, de->name, |
ccd979bd | 808 | de->name_len, |
b8bc5f4f | 809 | *f_pos, |
7e853679 | 810 | le64_to_cpu(de->inode), |
ccd979bd | 811 | d_type); |
e7b34019 MF |
812 | if (error) { |
813 | if (filldir_err) | |
814 | *filldir_err = error; | |
ccd979bd | 815 | break; |
e7b34019 | 816 | } |
b8bc5f4f | 817 | if (version != *f_version) |
ccd979bd MF |
818 | goto revalidate; |
819 | stored ++; | |
820 | } | |
b8bc5f4f | 821 | *f_pos += le16_to_cpu(de->rec_len); |
ccd979bd MF |
822 | } |
823 | offset = 0; | |
824 | brelse(bh); | |
a22305cc | 825 | bh = NULL; |
ccd979bd MF |
826 | } |
827 | ||
828 | stored = 0; | |
b8bc5f4f MF |
829 | out: |
830 | return stored; | |
831 | } | |
832 | ||
2b47c361 | 833 | static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version, |
e7b34019 MF |
834 | loff_t *f_pos, void *priv, filldir_t filldir, |
835 | int *filldir_err) | |
23193e51 MF |
836 | { |
837 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
838 | return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv, | |
e7b34019 | 839 | filldir, filldir_err); |
23193e51 | 840 | |
e7b34019 MF |
841 | return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir, |
842 | filldir_err); | |
23193e51 MF |
843 | } |
844 | ||
5eae5b96 MF |
845 | /* |
846 | * This is intended to be called from inside other kernel functions, | |
847 | * so we fake some arguments. | |
848 | */ | |
849 | int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv, | |
850 | filldir_t filldir) | |
851 | { | |
e7b34019 | 852 | int ret = 0, filldir_err = 0; |
2b47c361 | 853 | u64 version = inode->i_version; |
5eae5b96 MF |
854 | |
855 | while (*f_pos < i_size_read(inode)) { | |
856 | ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv, | |
e7b34019 MF |
857 | filldir, &filldir_err); |
858 | if (ret || filldir_err) | |
5eae5b96 MF |
859 | break; |
860 | } | |
861 | ||
e7b34019 MF |
862 | if (ret > 0) |
863 | ret = -EIO; | |
864 | ||
5eae5b96 MF |
865 | return 0; |
866 | } | |
867 | ||
b8bc5f4f MF |
868 | /* |
869 | * ocfs2_readdir() | |
870 | * | |
871 | */ | |
872 | int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir) | |
873 | { | |
874 | int error = 0; | |
875 | struct inode *inode = filp->f_path.dentry->d_inode; | |
876 | int lock_level = 0; | |
877 | ||
878 | mlog_entry("dirino=%llu\n", | |
879 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
880 | ||
e63aecb6 | 881 | error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level); |
b8bc5f4f MF |
882 | if (lock_level && error >= 0) { |
883 | /* We release EX lock which used to update atime | |
884 | * and get PR lock again to reduce contention | |
885 | * on commonly accessed directories. */ | |
e63aecb6 | 886 | ocfs2_inode_unlock(inode, 1); |
b8bc5f4f | 887 | lock_level = 0; |
e63aecb6 | 888 | error = ocfs2_inode_lock(inode, NULL, 0); |
b8bc5f4f MF |
889 | } |
890 | if (error < 0) { | |
891 | if (error != -ENOENT) | |
892 | mlog_errno(error); | |
893 | /* we haven't got any yet, so propagate the error. */ | |
894 | goto bail_nolock; | |
895 | } | |
896 | ||
897 | error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos, | |
e7b34019 | 898 | dirent, filldir, NULL); |
b8bc5f4f | 899 | |
e63aecb6 | 900 | ocfs2_inode_unlock(inode, lock_level); |
ccd979bd | 901 | |
aa958874 | 902 | bail_nolock: |
b8bc5f4f | 903 | mlog_exit(error); |
ccd979bd | 904 | |
b8bc5f4f | 905 | return error; |
ccd979bd MF |
906 | } |
907 | ||
908 | /* | |
1b1dcc1b | 909 | * NOTE: this should always be called with parent dir i_mutex taken. |
ccd979bd MF |
910 | */ |
911 | int ocfs2_find_files_on_disk(const char *name, | |
912 | int namelen, | |
913 | u64 *blkno, | |
914 | struct inode *inode, | |
915 | struct buffer_head **dirent_bh, | |
916 | struct ocfs2_dir_entry **dirent) | |
917 | { | |
918 | int status = -ENOENT; | |
ccd979bd | 919 | |
2b388c67 JB |
920 | mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n", |
921 | namelen, name, blkno, inode, dirent_bh, dirent); | |
ccd979bd MF |
922 | |
923 | *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent); | |
924 | if (!*dirent_bh || !*dirent) { | |
925 | status = -ENOENT; | |
926 | goto leave; | |
927 | } | |
928 | ||
929 | *blkno = le64_to_cpu((*dirent)->inode); | |
930 | ||
931 | status = 0; | |
932 | leave: | |
933 | if (status < 0) { | |
934 | *dirent = NULL; | |
a81cb88b MF |
935 | brelse(*dirent_bh); |
936 | *dirent_bh = NULL; | |
ccd979bd MF |
937 | } |
938 | ||
939 | mlog_exit(status); | |
940 | return status; | |
941 | } | |
942 | ||
be94d117 MF |
943 | /* |
944 | * Convenience function for callers which just want the block number | |
945 | * mapped to a name and don't require the full dirent info, etc. | |
946 | */ | |
947 | int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name, | |
948 | int namelen, u64 *blkno) | |
949 | { | |
950 | int ret; | |
951 | struct buffer_head *bh = NULL; | |
952 | struct ocfs2_dir_entry *dirent = NULL; | |
953 | ||
954 | ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent); | |
955 | brelse(bh); | |
956 | ||
957 | return ret; | |
958 | } | |
959 | ||
ccd979bd MF |
960 | /* Check for a name within a directory. |
961 | * | |
962 | * Return 0 if the name does not exist | |
963 | * Return -EEXIST if the directory contains the name | |
964 | * | |
1b1dcc1b | 965 | * Callers should have i_mutex + a cluster lock on dir |
ccd979bd MF |
966 | */ |
967 | int ocfs2_check_dir_for_entry(struct inode *dir, | |
968 | const char *name, | |
969 | int namelen) | |
970 | { | |
971 | int ret; | |
972 | struct buffer_head *dirent_bh = NULL; | |
973 | struct ocfs2_dir_entry *dirent = NULL; | |
974 | ||
b0697053 MF |
975 | mlog_entry("dir %llu, name '%.*s'\n", |
976 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); | |
ccd979bd MF |
977 | |
978 | ret = -EEXIST; | |
979 | dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent); | |
980 | if (dirent_bh) | |
981 | goto bail; | |
982 | ||
983 | ret = 0; | |
984 | bail: | |
a81cb88b | 985 | brelse(dirent_bh); |
ccd979bd MF |
986 | |
987 | mlog_exit(ret); | |
988 | return ret; | |
989 | } | |
990 | ||
0bfbbf62 MF |
991 | struct ocfs2_empty_dir_priv { |
992 | unsigned seen_dot; | |
993 | unsigned seen_dot_dot; | |
994 | unsigned seen_other; | |
995 | }; | |
996 | static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len, | |
997 | loff_t pos, u64 ino, unsigned type) | |
998 | { | |
999 | struct ocfs2_empty_dir_priv *p = priv; | |
1000 | ||
1001 | /* | |
1002 | * Check the positions of "." and ".." records to be sure | |
1003 | * they're in the correct place. | |
1004 | */ | |
1005 | if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) { | |
1006 | p->seen_dot = 1; | |
1007 | return 0; | |
1008 | } | |
1009 | ||
1010 | if (name_len == 2 && !strncmp("..", name, 2) && | |
1011 | pos == OCFS2_DIR_REC_LEN(1)) { | |
1012 | p->seen_dot_dot = 1; | |
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | p->seen_other = 1; | |
1017 | return 1; | |
1018 | } | |
ccd979bd MF |
1019 | /* |
1020 | * routine to check that the specified directory is empty (for rmdir) | |
0bfbbf62 MF |
1021 | * |
1022 | * Returns 1 if dir is empty, zero otherwise. | |
ccd979bd MF |
1023 | */ |
1024 | int ocfs2_empty_dir(struct inode *inode) | |
1025 | { | |
0bfbbf62 MF |
1026 | int ret; |
1027 | loff_t start = 0; | |
1028 | struct ocfs2_empty_dir_priv priv; | |
ccd979bd | 1029 | |
0bfbbf62 | 1030 | memset(&priv, 0, sizeof(priv)); |
ccd979bd | 1031 | |
0bfbbf62 MF |
1032 | ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir); |
1033 | if (ret) | |
1034 | mlog_errno(ret); | |
1035 | ||
1036 | if (!priv.seen_dot || !priv.seen_dot_dot) { | |
1037 | mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", | |
b0697053 | 1038 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
0bfbbf62 MF |
1039 | /* |
1040 | * XXX: Is it really safe to allow an unlink to continue? | |
1041 | */ | |
ccd979bd MF |
1042 | return 1; |
1043 | } | |
0bfbbf62 MF |
1044 | |
1045 | return !priv.seen_other; | |
ccd979bd MF |
1046 | } |
1047 | ||
5b6a3a2b MF |
1048 | static void ocfs2_fill_initial_dirents(struct inode *inode, |
1049 | struct inode *parent, | |
1050 | char *start, unsigned int size) | |
1051 | { | |
1052 | struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start; | |
1053 | ||
1054 | de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); | |
1055 | de->name_len = 1; | |
1056 | de->rec_len = | |
1057 | cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); | |
1058 | strcpy(de->name, "."); | |
1059 | ocfs2_set_de_type(de, S_IFDIR); | |
1060 | ||
1061 | de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len)); | |
1062 | de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno); | |
1063 | de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1)); | |
1064 | de->name_len = 2; | |
1065 | strcpy(de->name, ".."); | |
1066 | ocfs2_set_de_type(de, S_IFDIR); | |
1067 | } | |
1068 | ||
1069 | /* | |
1070 | * This works together with code in ocfs2_mknod_locked() which sets | |
1071 | * the inline-data flag and initializes the inline-data section. | |
1072 | */ | |
1073 | static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb, | |
1074 | handle_t *handle, | |
1075 | struct inode *parent, | |
1076 | struct inode *inode, | |
1077 | struct buffer_head *di_bh) | |
1078 | { | |
1079 | int ret; | |
1080 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
1081 | struct ocfs2_inline_data *data = &di->id2.i_data; | |
1082 | unsigned int size = le16_to_cpu(data->id_count); | |
1083 | ||
1084 | ret = ocfs2_journal_access(handle, inode, di_bh, | |
1085 | OCFS2_JOURNAL_ACCESS_WRITE); | |
1086 | if (ret) { | |
1087 | mlog_errno(ret); | |
1088 | goto out; | |
1089 | } | |
1090 | ||
1091 | ocfs2_fill_initial_dirents(inode, parent, data->id_data, size); | |
1092 | ||
1093 | ocfs2_journal_dirty(handle, di_bh); | |
1094 | if (ret) { | |
1095 | mlog_errno(ret); | |
1096 | goto out; | |
1097 | } | |
1098 | ||
1099 | i_size_write(inode, size); | |
1100 | inode->i_nlink = 2; | |
1101 | inode->i_blocks = ocfs2_inode_sector_count(inode); | |
1102 | ||
1103 | ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); | |
1104 | if (ret < 0) | |
1105 | mlog_errno(ret); | |
1106 | ||
1107 | out: | |
1108 | return ret; | |
1109 | } | |
1110 | ||
1111 | static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb, | |
1112 | handle_t *handle, | |
1113 | struct inode *parent, | |
1114 | struct inode *inode, | |
1115 | struct buffer_head *fe_bh, | |
1116 | struct ocfs2_alloc_context *data_ac) | |
316f4b9f MF |
1117 | { |
1118 | int status; | |
1119 | struct buffer_head *new_bh = NULL; | |
316f4b9f MF |
1120 | |
1121 | mlog_entry_void(); | |
1122 | ||
1123 | status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh, | |
1124 | data_ac, NULL, &new_bh); | |
1125 | if (status < 0) { | |
1126 | mlog_errno(status); | |
1127 | goto bail; | |
1128 | } | |
1129 | ||
1130 | ocfs2_set_new_buffer_uptodate(inode, new_bh); | |
1131 | ||
1132 | status = ocfs2_journal_access(handle, inode, new_bh, | |
1133 | OCFS2_JOURNAL_ACCESS_CREATE); | |
1134 | if (status < 0) { | |
1135 | mlog_errno(status); | |
1136 | goto bail; | |
1137 | } | |
1138 | memset(new_bh->b_data, 0, osb->sb->s_blocksize); | |
1139 | ||
5b6a3a2b MF |
1140 | ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, |
1141 | osb->sb->s_blocksize); | |
316f4b9f MF |
1142 | |
1143 | status = ocfs2_journal_dirty(handle, new_bh); | |
1144 | if (status < 0) { | |
1145 | mlog_errno(status); | |
1146 | goto bail; | |
1147 | } | |
1148 | ||
1149 | i_size_write(inode, inode->i_sb->s_blocksize); | |
1150 | inode->i_nlink = 2; | |
1151 | inode->i_blocks = ocfs2_inode_sector_count(inode); | |
1152 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | |
1153 | if (status < 0) { | |
1154 | mlog_errno(status); | |
1155 | goto bail; | |
1156 | } | |
1157 | ||
1158 | status = 0; | |
1159 | bail: | |
a81cb88b | 1160 | brelse(new_bh); |
316f4b9f MF |
1161 | |
1162 | mlog_exit(status); | |
1163 | return status; | |
1164 | } | |
1165 | ||
5b6a3a2b MF |
1166 | int ocfs2_fill_new_dir(struct ocfs2_super *osb, |
1167 | handle_t *handle, | |
1168 | struct inode *parent, | |
1169 | struct inode *inode, | |
1170 | struct buffer_head *fe_bh, | |
1171 | struct ocfs2_alloc_context *data_ac) | |
1172 | { | |
1173 | BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL); | |
1174 | ||
1175 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) | |
1176 | return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh); | |
1177 | ||
1178 | return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh, | |
1179 | data_ac); | |
1180 | } | |
1181 | ||
1182 | static void ocfs2_expand_last_dirent(char *start, unsigned int old_size, | |
1183 | unsigned int new_size) | |
1184 | { | |
1185 | struct ocfs2_dir_entry *de; | |
1186 | struct ocfs2_dir_entry *prev_de; | |
1187 | char *de_buf, *limit; | |
1188 | unsigned int bytes = new_size - old_size; | |
1189 | ||
1190 | limit = start + old_size; | |
1191 | de_buf = start; | |
1192 | de = (struct ocfs2_dir_entry *)de_buf; | |
1193 | do { | |
1194 | prev_de = de; | |
1195 | de_buf += le16_to_cpu(de->rec_len); | |
1196 | de = (struct ocfs2_dir_entry *)de_buf; | |
1197 | } while (de_buf < limit); | |
1198 | ||
1199 | le16_add_cpu(&prev_de->rec_len, bytes); | |
1200 | } | |
1201 | ||
1202 | /* | |
1203 | * We allocate enough clusters to fulfill "blocks_wanted", but set | |
1204 | * i_size to exactly one block. Ocfs2_extend_dir() will handle the | |
1205 | * rest automatically for us. | |
1206 | * | |
1207 | * *first_block_bh is a pointer to the 1st data block allocated to the | |
1208 | * directory. | |
1209 | */ | |
1210 | static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh, | |
1211 | unsigned int blocks_wanted, | |
1212 | struct buffer_head **first_block_bh) | |
1213 | { | |
5b6a3a2b MF |
1214 | u32 alloc, bit_off, len; |
1215 | struct super_block *sb = dir->i_sb; | |
a90714c1 | 1216 | int ret, credits = ocfs2_inline_to_extents_credits(sb); |
5b6a3a2b MF |
1217 | u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits; |
1218 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); | |
1219 | struct ocfs2_inode_info *oi = OCFS2_I(dir); | |
1220 | struct ocfs2_alloc_context *data_ac; | |
1221 | struct buffer_head *dirdata_bh = NULL; | |
1222 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
1223 | handle_t *handle; | |
f99b9b7c | 1224 | struct ocfs2_extent_tree et; |
a90714c1 | 1225 | int did_quota = 0; |
f99b9b7c | 1226 | |
8d6220d6 | 1227 | ocfs2_init_dinode_extent_tree(&et, dir, di_bh); |
5b6a3a2b MF |
1228 | |
1229 | alloc = ocfs2_clusters_for_bytes(sb, bytes); | |
1230 | ||
1231 | /* | |
1232 | * We should never need more than 2 clusters for this - | |
1233 | * maximum dirent size is far less than one block. In fact, | |
1234 | * the only time we'd need more than one cluster is if | |
1235 | * blocksize == clustersize and the dirent won't fit in the | |
1236 | * extra space that the expansion to a single block gives. As | |
1237 | * of today, that only happens on 4k/4k file systems. | |
1238 | */ | |
1239 | BUG_ON(alloc > 2); | |
1240 | ||
1241 | ret = ocfs2_reserve_clusters(osb, alloc, &data_ac); | |
1242 | if (ret) { | |
1243 | mlog_errno(ret); | |
1244 | goto out; | |
1245 | } | |
1246 | ||
1247 | down_write(&oi->ip_alloc_sem); | |
1248 | ||
1249 | /* | |
c78bad11 | 1250 | * Prepare for worst case allocation scenario of two separate |
5b6a3a2b MF |
1251 | * extents. |
1252 | */ | |
1253 | if (alloc == 2) | |
1254 | credits += OCFS2_SUBALLOC_ALLOC; | |
1255 | ||
1256 | handle = ocfs2_start_trans(osb, credits); | |
1257 | if (IS_ERR(handle)) { | |
1258 | ret = PTR_ERR(handle); | |
1259 | mlog_errno(ret); | |
1260 | goto out_sem; | |
1261 | } | |
1262 | ||
a90714c1 JK |
1263 | if (vfs_dq_alloc_space_nodirty(dir, |
1264 | ocfs2_clusters_to_bytes(osb->sb, alloc))) { | |
1265 | ret = -EDQUOT; | |
1266 | goto out_commit; | |
1267 | } | |
1268 | did_quota = 1; | |
5b6a3a2b MF |
1269 | /* |
1270 | * Try to claim as many clusters as the bitmap can give though | |
1271 | * if we only get one now, that's enough to continue. The rest | |
1272 | * will be claimed after the conversion to extents. | |
1273 | */ | |
1274 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len); | |
1275 | if (ret) { | |
1276 | mlog_errno(ret); | |
1277 | goto out_commit; | |
1278 | } | |
1279 | ||
1280 | /* | |
1281 | * Operations are carefully ordered so that we set up the new | |
1282 | * data block first. The conversion from inline data to | |
1283 | * extents follows. | |
1284 | */ | |
1285 | blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); | |
1286 | dirdata_bh = sb_getblk(sb, blkno); | |
1287 | if (!dirdata_bh) { | |
1288 | ret = -EIO; | |
1289 | mlog_errno(ret); | |
1290 | goto out_commit; | |
1291 | } | |
1292 | ||
1293 | ocfs2_set_new_buffer_uptodate(dir, dirdata_bh); | |
1294 | ||
1295 | ret = ocfs2_journal_access(handle, dir, dirdata_bh, | |
1296 | OCFS2_JOURNAL_ACCESS_CREATE); | |
1297 | if (ret) { | |
1298 | mlog_errno(ret); | |
1299 | goto out_commit; | |
1300 | } | |
1301 | ||
1302 | memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir)); | |
1303 | memset(dirdata_bh->b_data + i_size_read(dir), 0, | |
1304 | sb->s_blocksize - i_size_read(dir)); | |
1305 | ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), | |
1306 | sb->s_blocksize); | |
1307 | ||
1308 | ret = ocfs2_journal_dirty(handle, dirdata_bh); | |
1309 | if (ret) { | |
1310 | mlog_errno(ret); | |
1311 | goto out_commit; | |
1312 | } | |
1313 | ||
1314 | /* | |
1315 | * Set extent, i_size, etc on the directory. After this, the | |
1316 | * inode should contain the same exact dirents as before and | |
1317 | * be fully accessible from system calls. | |
1318 | * | |
1319 | * We let the later dirent insert modify c/mtime - to the user | |
1320 | * the data hasn't changed. | |
1321 | */ | |
1322 | ret = ocfs2_journal_access(handle, dir, di_bh, | |
1323 | OCFS2_JOURNAL_ACCESS_CREATE); | |
1324 | if (ret) { | |
1325 | mlog_errno(ret); | |
1326 | goto out_commit; | |
1327 | } | |
1328 | ||
1329 | spin_lock(&oi->ip_lock); | |
1330 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; | |
1331 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); | |
1332 | spin_unlock(&oi->ip_lock); | |
1333 | ||
1334 | ocfs2_dinode_new_extent_list(dir, di); | |
1335 | ||
1336 | i_size_write(dir, sb->s_blocksize); | |
1337 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; | |
1338 | ||
1339 | di->i_size = cpu_to_le64(sb->s_blocksize); | |
1340 | di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec); | |
1341 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec); | |
5b6a3a2b MF |
1342 | |
1343 | /* | |
1344 | * This should never fail as our extent list is empty and all | |
1345 | * related blocks have been journaled already. | |
1346 | */ | |
f99b9b7c JB |
1347 | ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len, |
1348 | 0, NULL); | |
5b6a3a2b MF |
1349 | if (ret) { |
1350 | mlog_errno(ret); | |
83cab533 | 1351 | goto out_commit; |
5b6a3a2b MF |
1352 | } |
1353 | ||
9780eb6c MF |
1354 | /* |
1355 | * Set i_blocks after the extent insert for the most up to | |
1356 | * date ip_clusters value. | |
1357 | */ | |
1358 | dir->i_blocks = ocfs2_inode_sector_count(dir); | |
1359 | ||
5b6a3a2b MF |
1360 | ret = ocfs2_journal_dirty(handle, di_bh); |
1361 | if (ret) { | |
1362 | mlog_errno(ret); | |
1363 | goto out_commit; | |
1364 | } | |
1365 | ||
1366 | /* | |
1367 | * We asked for two clusters, but only got one in the 1st | |
1368 | * pass. Claim the 2nd cluster as a separate extent. | |
1369 | */ | |
1370 | if (alloc > len) { | |
1371 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, | |
1372 | &len); | |
1373 | if (ret) { | |
1374 | mlog_errno(ret); | |
1375 | goto out_commit; | |
1376 | } | |
1377 | blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); | |
1378 | ||
f99b9b7c JB |
1379 | ret = ocfs2_insert_extent(osb, handle, dir, &et, 1, |
1380 | blkno, len, 0, NULL); | |
5b6a3a2b MF |
1381 | if (ret) { |
1382 | mlog_errno(ret); | |
83cab533 | 1383 | goto out_commit; |
5b6a3a2b MF |
1384 | } |
1385 | } | |
1386 | ||
1387 | *first_block_bh = dirdata_bh; | |
1388 | dirdata_bh = NULL; | |
1389 | ||
1390 | out_commit: | |
a90714c1 JK |
1391 | if (ret < 0 && did_quota) |
1392 | vfs_dq_free_space_nodirty(dir, | |
1393 | ocfs2_clusters_to_bytes(osb->sb, 2)); | |
5b6a3a2b MF |
1394 | ocfs2_commit_trans(osb, handle); |
1395 | ||
1396 | out_sem: | |
1397 | up_write(&oi->ip_alloc_sem); | |
1398 | ||
1399 | out: | |
1400 | if (data_ac) | |
1401 | ocfs2_free_alloc_context(data_ac); | |
1402 | ||
1403 | brelse(dirdata_bh); | |
1404 | ||
1405 | return ret; | |
1406 | } | |
1407 | ||
ccd979bd | 1408 | /* returns a bh of the 1st new block in the allocation. */ |
316f4b9f MF |
1409 | static int ocfs2_do_extend_dir(struct super_block *sb, |
1410 | handle_t *handle, | |
1411 | struct inode *dir, | |
1412 | struct buffer_head *parent_fe_bh, | |
1413 | struct ocfs2_alloc_context *data_ac, | |
1414 | struct ocfs2_alloc_context *meta_ac, | |
1415 | struct buffer_head **new_bh) | |
ccd979bd MF |
1416 | { |
1417 | int status; | |
a90714c1 | 1418 | int extend, did_quota = 0; |
8110b073 | 1419 | u64 p_blkno, v_blkno; |
ccd979bd MF |
1420 | |
1421 | spin_lock(&OCFS2_I(dir)->ip_lock); | |
1422 | extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); | |
1423 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
1424 | ||
1425 | if (extend) { | |
dcd0538f MF |
1426 | u32 offset = OCFS2_I(dir)->ip_clusters; |
1427 | ||
a90714c1 JK |
1428 | if (vfs_dq_alloc_space_nodirty(dir, |
1429 | ocfs2_clusters_to_bytes(sb, 1))) { | |
1430 | status = -EDQUOT; | |
1431 | goto bail; | |
1432 | } | |
1433 | did_quota = 1; | |
1434 | ||
0eb8d47e TM |
1435 | status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset, |
1436 | 1, 0, parent_fe_bh, handle, | |
1437 | data_ac, meta_ac, NULL); | |
ccd979bd MF |
1438 | BUG_ON(status == -EAGAIN); |
1439 | if (status < 0) { | |
1440 | mlog_errno(status); | |
1441 | goto bail; | |
1442 | } | |
1443 | } | |
1444 | ||
8110b073 MF |
1445 | v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir)); |
1446 | status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL); | |
ccd979bd MF |
1447 | if (status < 0) { |
1448 | mlog_errno(status); | |
1449 | goto bail; | |
1450 | } | |
1451 | ||
1452 | *new_bh = sb_getblk(sb, p_blkno); | |
1453 | if (!*new_bh) { | |
1454 | status = -EIO; | |
1455 | mlog_errno(status); | |
1456 | goto bail; | |
1457 | } | |
1458 | status = 0; | |
1459 | bail: | |
a90714c1 JK |
1460 | if (did_quota && status < 0) |
1461 | vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1)); | |
ccd979bd MF |
1462 | mlog_exit(status); |
1463 | return status; | |
1464 | } | |
1465 | ||
5b6a3a2b MF |
1466 | /* |
1467 | * Assumes you already have a cluster lock on the directory. | |
1468 | * | |
1469 | * 'blocks_wanted' is only used if we have an inline directory which | |
1470 | * is to be turned into an extent based one. The size of the dirent to | |
1471 | * insert might be larger than the space gained by growing to just one | |
1472 | * block, so we may have to grow the inode by two blocks in that case. | |
1473 | */ | |
ccd979bd MF |
1474 | static int ocfs2_extend_dir(struct ocfs2_super *osb, |
1475 | struct inode *dir, | |
1476 | struct buffer_head *parent_fe_bh, | |
5b6a3a2b | 1477 | unsigned int blocks_wanted, |
ccd979bd MF |
1478 | struct buffer_head **new_de_bh) |
1479 | { | |
1480 | int status = 0; | |
ee19a779 | 1481 | int credits, num_free_extents, drop_alloc_sem = 0; |
ccd979bd MF |
1482 | loff_t dir_i_size; |
1483 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; | |
811f933d | 1484 | struct ocfs2_extent_list *el = &fe->id2.i_list; |
ccd979bd MF |
1485 | struct ocfs2_alloc_context *data_ac = NULL; |
1486 | struct ocfs2_alloc_context *meta_ac = NULL; | |
1fabe148 | 1487 | handle_t *handle = NULL; |
ccd979bd MF |
1488 | struct buffer_head *new_bh = NULL; |
1489 | struct ocfs2_dir_entry * de; | |
1490 | struct super_block *sb = osb->sb; | |
f99b9b7c | 1491 | struct ocfs2_extent_tree et; |
ccd979bd MF |
1492 | |
1493 | mlog_entry_void(); | |
1494 | ||
5b6a3a2b MF |
1495 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
1496 | status = ocfs2_expand_inline_dir(dir, parent_fe_bh, | |
1497 | blocks_wanted, &new_bh); | |
1498 | if (status) { | |
1499 | mlog_errno(status); | |
1500 | goto bail; | |
1501 | } | |
1502 | ||
1503 | if (blocks_wanted == 1) { | |
1504 | /* | |
1505 | * If the new dirent will fit inside the space | |
1506 | * created by pushing out to one block, then | |
1507 | * we can complete the operation | |
1508 | * here. Otherwise we have to expand i_size | |
1509 | * and format the 2nd block below. | |
1510 | */ | |
1511 | BUG_ON(new_bh == NULL); | |
1512 | goto bail_bh; | |
1513 | } | |
1514 | ||
1515 | /* | |
1516 | * Get rid of 'new_bh' - we want to format the 2nd | |
1517 | * data block and return that instead. | |
1518 | */ | |
1519 | brelse(new_bh); | |
1520 | new_bh = NULL; | |
1521 | ||
1522 | dir_i_size = i_size_read(dir); | |
1523 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; | |
1524 | goto do_extend; | |
1525 | } | |
1526 | ||
ccd979bd | 1527 | dir_i_size = i_size_read(dir); |
b0697053 MF |
1528 | mlog(0, "extending dir %llu (i_size = %lld)\n", |
1529 | (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size); | |
ccd979bd | 1530 | |
ccd979bd MF |
1531 | /* dir->i_size is always block aligned. */ |
1532 | spin_lock(&OCFS2_I(dir)->ip_lock); | |
1533 | if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { | |
1534 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
8d6220d6 | 1535 | ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh); |
f99b9b7c | 1536 | num_free_extents = ocfs2_num_free_extents(osb, dir, &et); |
ccd979bd MF |
1537 | if (num_free_extents < 0) { |
1538 | status = num_free_extents; | |
1539 | mlog_errno(status); | |
1540 | goto bail; | |
1541 | } | |
1542 | ||
1543 | if (!num_free_extents) { | |
811f933d | 1544 | status = ocfs2_reserve_new_metadata(osb, el, &meta_ac); |
ccd979bd MF |
1545 | if (status < 0) { |
1546 | if (status != -ENOSPC) | |
1547 | mlog_errno(status); | |
1548 | goto bail; | |
1549 | } | |
1550 | } | |
1551 | ||
da5cbf2f | 1552 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
ccd979bd MF |
1553 | if (status < 0) { |
1554 | if (status != -ENOSPC) | |
1555 | mlog_errno(status); | |
1556 | goto bail; | |
1557 | } | |
1558 | ||
811f933d | 1559 | credits = ocfs2_calc_extend_credits(sb, el, 1); |
ccd979bd MF |
1560 | } else { |
1561 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
1562 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; | |
1563 | } | |
1564 | ||
5b6a3a2b | 1565 | do_extend: |
ee19a779 JB |
1566 | down_write(&OCFS2_I(dir)->ip_alloc_sem); |
1567 | drop_alloc_sem = 1; | |
1568 | ||
65eff9cc | 1569 | handle = ocfs2_start_trans(osb, credits); |
ccd979bd MF |
1570 | if (IS_ERR(handle)) { |
1571 | status = PTR_ERR(handle); | |
1572 | handle = NULL; | |
1573 | mlog_errno(status); | |
1574 | goto bail; | |
1575 | } | |
1576 | ||
1577 | status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, | |
1578 | data_ac, meta_ac, &new_bh); | |
1579 | if (status < 0) { | |
1580 | mlog_errno(status); | |
1581 | goto bail; | |
1582 | } | |
1583 | ||
1584 | ocfs2_set_new_buffer_uptodate(dir, new_bh); | |
1585 | ||
1586 | status = ocfs2_journal_access(handle, dir, new_bh, | |
1587 | OCFS2_JOURNAL_ACCESS_CREATE); | |
1588 | if (status < 0) { | |
1589 | mlog_errno(status); | |
1590 | goto bail; | |
1591 | } | |
1592 | memset(new_bh->b_data, 0, sb->s_blocksize); | |
1593 | de = (struct ocfs2_dir_entry *) new_bh->b_data; | |
1594 | de->inode = 0; | |
1595 | de->rec_len = cpu_to_le16(sb->s_blocksize); | |
1596 | status = ocfs2_journal_dirty(handle, new_bh); | |
1597 | if (status < 0) { | |
1598 | mlog_errno(status); | |
1599 | goto bail; | |
1600 | } | |
1601 | ||
1602 | dir_i_size += dir->i_sb->s_blocksize; | |
1603 | i_size_write(dir, dir_i_size); | |
8110b073 | 1604 | dir->i_blocks = ocfs2_inode_sector_count(dir); |
ccd979bd MF |
1605 | status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
1606 | if (status < 0) { | |
1607 | mlog_errno(status); | |
1608 | goto bail; | |
1609 | } | |
1610 | ||
5b6a3a2b | 1611 | bail_bh: |
ccd979bd MF |
1612 | *new_de_bh = new_bh; |
1613 | get_bh(*new_de_bh); | |
1614 | bail: | |
ee19a779 JB |
1615 | if (drop_alloc_sem) |
1616 | up_write(&OCFS2_I(dir)->ip_alloc_sem); | |
ccd979bd | 1617 | if (handle) |
02dc1af4 | 1618 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
1619 | |
1620 | if (data_ac) | |
1621 | ocfs2_free_alloc_context(data_ac); | |
1622 | if (meta_ac) | |
1623 | ocfs2_free_alloc_context(meta_ac); | |
1624 | ||
a81cb88b | 1625 | brelse(new_bh); |
ccd979bd MF |
1626 | |
1627 | mlog_exit(status); | |
1628 | return status; | |
1629 | } | |
1630 | ||
5b6a3a2b MF |
1631 | static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh, |
1632 | const char *name, int namelen, | |
1633 | struct buffer_head **ret_de_bh, | |
1634 | unsigned int *blocks_wanted) | |
ccd979bd | 1635 | { |
5b6a3a2b MF |
1636 | int ret; |
1637 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
1638 | struct ocfs2_dir_entry *de, *last_de = NULL; | |
1639 | char *de_buf, *limit; | |
1640 | unsigned long offset = 0; | |
1641 | unsigned int rec_len, new_rec_len; | |
1642 | ||
1643 | de_buf = di->id2.i_data.id_data; | |
1644 | limit = de_buf + i_size_read(dir); | |
1645 | rec_len = OCFS2_DIR_REC_LEN(namelen); | |
ccd979bd | 1646 | |
5b6a3a2b MF |
1647 | while (de_buf < limit) { |
1648 | de = (struct ocfs2_dir_entry *)de_buf; | |
ccd979bd | 1649 | |
5b6a3a2b MF |
1650 | if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) { |
1651 | ret = -ENOENT; | |
1652 | goto out; | |
1653 | } | |
1654 | if (ocfs2_match(namelen, name, de)) { | |
1655 | ret = -EEXIST; | |
1656 | goto out; | |
1657 | } | |
1658 | if (ocfs2_dirent_would_fit(de, rec_len)) { | |
1659 | /* Ok, we found a spot. Return this bh and let | |
1660 | * the caller actually fill it in. */ | |
1661 | *ret_de_bh = di_bh; | |
1662 | get_bh(*ret_de_bh); | |
1663 | ret = 0; | |
1664 | goto out; | |
1665 | } | |
ccd979bd | 1666 | |
5b6a3a2b MF |
1667 | last_de = de; |
1668 | de_buf += le16_to_cpu(de->rec_len); | |
1669 | offset += le16_to_cpu(de->rec_len); | |
1670 | } | |
ccd979bd | 1671 | |
5b6a3a2b MF |
1672 | /* |
1673 | * We're going to require expansion of the directory - figure | |
1674 | * out how many blocks we'll need so that a place for the | |
1675 | * dirent can be found. | |
1676 | */ | |
1677 | *blocks_wanted = 1; | |
1678 | new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir)); | |
1679 | if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len))) | |
1680 | *blocks_wanted = 2; | |
ccd979bd | 1681 | |
5b6a3a2b MF |
1682 | ret = -ENOSPC; |
1683 | out: | |
1684 | return ret; | |
1685 | } | |
1686 | ||
1687 | static int ocfs2_find_dir_space_el(struct inode *dir, const char *name, | |
1688 | int namelen, struct buffer_head **ret_de_bh) | |
1689 | { | |
1690 | unsigned long offset; | |
1691 | struct buffer_head *bh = NULL; | |
1692 | unsigned short rec_len; | |
1693 | struct ocfs2_dir_entry *de; | |
1694 | struct super_block *sb = dir->i_sb; | |
1695 | int status; | |
ccd979bd | 1696 | |
a22305cc JB |
1697 | status = ocfs2_read_dir_block(dir, 0, &bh, 0); |
1698 | if (status) { | |
ccd979bd MF |
1699 | mlog_errno(status); |
1700 | goto bail; | |
1701 | } | |
1702 | ||
1703 | rec_len = OCFS2_DIR_REC_LEN(namelen); | |
1704 | offset = 0; | |
1705 | de = (struct ocfs2_dir_entry *) bh->b_data; | |
1706 | while (1) { | |
1707 | if ((char *)de >= sb->s_blocksize + bh->b_data) { | |
1708 | brelse(bh); | |
1709 | bh = NULL; | |
1710 | ||
1711 | if (i_size_read(dir) <= offset) { | |
5b6a3a2b MF |
1712 | /* |
1713 | * Caller will have to expand this | |
1714 | * directory. | |
1715 | */ | |
1716 | status = -ENOSPC; | |
ccd979bd MF |
1717 | goto bail; |
1718 | } | |
a22305cc JB |
1719 | status = ocfs2_read_dir_block(dir, |
1720 | offset >> sb->s_blocksize_bits, | |
1721 | &bh, 0); | |
1722 | if (status) { | |
ccd979bd MF |
1723 | mlog_errno(status); |
1724 | goto bail; | |
1725 | } | |
1726 | /* move to next block */ | |
1727 | de = (struct ocfs2_dir_entry *) bh->b_data; | |
1728 | } | |
1729 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { | |
1730 | status = -ENOENT; | |
1731 | goto bail; | |
1732 | } | |
1733 | if (ocfs2_match(namelen, name, de)) { | |
1734 | status = -EEXIST; | |
1735 | goto bail; | |
1736 | } | |
8553cf4f | 1737 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
ccd979bd MF |
1738 | /* Ok, we found a spot. Return this bh and let |
1739 | * the caller actually fill it in. */ | |
1740 | *ret_de_bh = bh; | |
1741 | get_bh(*ret_de_bh); | |
1742 | status = 0; | |
1743 | goto bail; | |
1744 | } | |
1745 | offset += le16_to_cpu(de->rec_len); | |
1746 | de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); | |
1747 | } | |
1748 | ||
1749 | status = 0; | |
1750 | bail: | |
a81cb88b | 1751 | brelse(bh); |
ccd979bd MF |
1752 | |
1753 | mlog_exit(status); | |
1754 | return status; | |
1755 | } | |
5b6a3a2b MF |
1756 | |
1757 | int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, | |
1758 | struct inode *dir, | |
1759 | struct buffer_head *parent_fe_bh, | |
1760 | const char *name, | |
1761 | int namelen, | |
1762 | struct buffer_head **ret_de_bh) | |
1763 | { | |
1764 | int ret; | |
1765 | unsigned int blocks_wanted = 1; | |
1766 | struct buffer_head *bh = NULL; | |
1767 | ||
1768 | mlog(0, "getting ready to insert namelen %d into dir %llu\n", | |
1769 | namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno); | |
1770 | ||
1771 | *ret_de_bh = NULL; | |
1772 | ||
1773 | if (!namelen) { | |
1774 | ret = -EINVAL; | |
1775 | mlog_errno(ret); | |
1776 | goto out; | |
1777 | } | |
1778 | ||
1779 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { | |
1780 | ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name, | |
1781 | namelen, &bh, &blocks_wanted); | |
1782 | } else | |
1783 | ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh); | |
1784 | ||
1785 | if (ret && ret != -ENOSPC) { | |
1786 | mlog_errno(ret); | |
1787 | goto out; | |
1788 | } | |
1789 | ||
1790 | if (ret == -ENOSPC) { | |
1791 | /* | |
1792 | * We have to expand the directory to add this name. | |
1793 | */ | |
1794 | BUG_ON(bh); | |
1795 | ||
1796 | ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted, | |
1797 | &bh); | |
1798 | if (ret) { | |
1799 | if (ret != -ENOSPC) | |
1800 | mlog_errno(ret); | |
1801 | goto out; | |
1802 | } | |
1803 | ||
1804 | BUG_ON(!bh); | |
1805 | } | |
1806 | ||
1807 | *ret_de_bh = bh; | |
1808 | bh = NULL; | |
1809 | out: | |
a81cb88b | 1810 | brelse(bh); |
5b6a3a2b MF |
1811 | return ret; |
1812 | } |