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> | |
43 | ||
44 | #define MLOG_MASK_PREFIX ML_NAMEI | |
45 | #include <cluster/masklog.h> | |
46 | ||
47 | #include "ocfs2.h" | |
48 | ||
49 | #include "alloc.h" | |
50 | #include "dir.h" | |
51 | #include "dlmglue.h" | |
52 | #include "extent_map.h" | |
53 | #include "file.h" | |
54 | #include "inode.h" | |
55 | #include "journal.h" | |
56 | #include "namei.h" | |
57 | #include "suballoc.h" | |
58 | #include "uptodate.h" | |
59 | ||
60 | #include "buffer_head_io.h" | |
61 | ||
62 | static unsigned char ocfs2_filetype_table[] = { | |
63 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | |
64 | }; | |
65 | ||
66 | static int ocfs2_extend_dir(struct ocfs2_super *osb, | |
67 | struct inode *dir, | |
68 | struct buffer_head *parent_fe_bh, | |
69 | struct buffer_head **new_de_bh); | |
70 | /* | |
71 | * ocfs2_readdir() | |
72 | * | |
73 | */ | |
74 | int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir) | |
75 | { | |
76 | int error = 0; | |
aa958874 MF |
77 | unsigned long offset, blk, last_ra_blk = 0; |
78 | int i, stored; | |
ccd979bd MF |
79 | struct buffer_head * bh, * tmp; |
80 | struct ocfs2_dir_entry * de; | |
81 | int err; | |
d28c9174 | 82 | struct inode *inode = filp->f_path.dentry->d_inode; |
ccd979bd | 83 | struct super_block * sb = inode->i_sb; |
aa958874 | 84 | unsigned int ra_sectors = 16; |
25899dee | 85 | int lock_level = 0; |
ccd979bd | 86 | |
b0697053 MF |
87 | mlog_entry("dirino=%llu\n", |
88 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd MF |
89 | |
90 | stored = 0; | |
91 | bh = NULL; | |
92 | ||
25899dee TY |
93 | error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level); |
94 | if (lock_level && error >= 0) { | |
95 | /* We release EX lock which used to update atime | |
96 | * and get PR lock again to reduce contention | |
97 | * on commonly accessed directories. */ | |
98 | ocfs2_meta_unlock(inode, 1); | |
99 | lock_level = 0; | |
100 | error = ocfs2_meta_lock(inode, NULL, 0); | |
101 | } | |
ccd979bd MF |
102 | if (error < 0) { |
103 | if (error != -ENOENT) | |
104 | mlog_errno(error); | |
105 | /* we haven't got any yet, so propagate the error. */ | |
106 | stored = error; | |
aa958874 | 107 | goto bail_nolock; |
ccd979bd | 108 | } |
ccd979bd MF |
109 | |
110 | offset = filp->f_pos & (sb->s_blocksize - 1); | |
111 | ||
112 | while (!error && !stored && filp->f_pos < i_size_read(inode)) { | |
113 | blk = (filp->f_pos) >> sb->s_blocksize_bits; | |
114 | bh = ocfs2_bread(inode, blk, &err, 0); | |
115 | if (!bh) { | |
b0697053 MF |
116 | mlog(ML_ERROR, |
117 | "directory #%llu contains a hole at offset %lld\n", | |
118 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
ccd979bd MF |
119 | filp->f_pos); |
120 | filp->f_pos += sb->s_blocksize - offset; | |
121 | continue; | |
122 | } | |
123 | ||
aa958874 MF |
124 | /* The idea here is to begin with 8k read-ahead and to stay |
125 | * 4k ahead of our current position. | |
126 | * | |
127 | * TODO: Use the pagecache for this. We just need to | |
128 | * make sure it's cluster-safe... */ | |
129 | if (!last_ra_blk | |
130 | || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { | |
131 | for (i = ra_sectors >> (sb->s_blocksize_bits - 9); | |
ccd979bd MF |
132 | i > 0; i--) { |
133 | tmp = ocfs2_bread(inode, ++blk, &err, 1); | |
134 | if (tmp) | |
135 | brelse(tmp); | |
136 | } | |
aa958874 MF |
137 | last_ra_blk = blk; |
138 | ra_sectors = 8; | |
ccd979bd MF |
139 | } |
140 | ||
141 | revalidate: | |
142 | /* If the dir block has changed since the last call to | |
143 | * readdir(2), then we might be pointing to an invalid | |
144 | * dirent right now. Scan from the start of the block | |
145 | * to make sure. */ | |
146 | if (filp->f_version != inode->i_version) { | |
147 | for (i = 0; i < sb->s_blocksize && i < offset; ) { | |
148 | de = (struct ocfs2_dir_entry *) (bh->b_data + i); | |
149 | /* It's too expensive to do a full | |
150 | * dirent test each time round this | |
151 | * loop, but we do have to test at | |
152 | * least that it is non-zero. A | |
153 | * failure will be detected in the | |
154 | * dirent test below. */ | |
155 | if (le16_to_cpu(de->rec_len) < | |
156 | OCFS2_DIR_REC_LEN(1)) | |
157 | break; | |
158 | i += le16_to_cpu(de->rec_len); | |
159 | } | |
160 | offset = i; | |
161 | filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1)) | |
162 | | offset; | |
163 | filp->f_version = inode->i_version; | |
164 | } | |
165 | ||
166 | while (!error && filp->f_pos < i_size_read(inode) | |
167 | && offset < sb->s_blocksize) { | |
168 | de = (struct ocfs2_dir_entry *) (bh->b_data + offset); | |
169 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { | |
170 | /* On error, skip the f_pos to the | |
171 | next block. */ | |
172 | filp->f_pos = (filp->f_pos | | |
173 | (sb->s_blocksize - 1)) + 1; | |
174 | brelse(bh); | |
175 | goto bail; | |
176 | } | |
177 | offset += le16_to_cpu(de->rec_len); | |
178 | if (le64_to_cpu(de->inode)) { | |
179 | /* We might block in the next section | |
180 | * if the data destination is | |
181 | * currently swapped out. So, use a | |
182 | * version stamp to detect whether or | |
183 | * not the directory has been modified | |
184 | * during the copy operation. | |
185 | */ | |
186 | unsigned long version = filp->f_version; | |
187 | unsigned char d_type = DT_UNKNOWN; | |
188 | ||
189 | if (de->file_type < OCFS2_FT_MAX) | |
190 | d_type = ocfs2_filetype_table[de->file_type]; | |
191 | error = filldir(dirent, de->name, | |
192 | de->name_len, | |
193 | filp->f_pos, | |
194 | ino_from_blkno(sb, le64_to_cpu(de->inode)), | |
195 | d_type); | |
196 | if (error) | |
197 | break; | |
198 | if (version != filp->f_version) | |
199 | goto revalidate; | |
200 | stored ++; | |
201 | } | |
202 | filp->f_pos += le16_to_cpu(de->rec_len); | |
203 | } | |
204 | offset = 0; | |
205 | brelse(bh); | |
206 | } | |
207 | ||
208 | stored = 0; | |
209 | bail: | |
25899dee | 210 | ocfs2_meta_unlock(inode, lock_level); |
ccd979bd | 211 | |
aa958874 | 212 | bail_nolock: |
ccd979bd MF |
213 | mlog_exit(stored); |
214 | ||
215 | return stored; | |
216 | } | |
217 | ||
218 | /* | |
1b1dcc1b | 219 | * NOTE: this should always be called with parent dir i_mutex taken. |
ccd979bd MF |
220 | */ |
221 | int ocfs2_find_files_on_disk(const char *name, | |
222 | int namelen, | |
223 | u64 *blkno, | |
224 | struct inode *inode, | |
225 | struct buffer_head **dirent_bh, | |
226 | struct ocfs2_dir_entry **dirent) | |
227 | { | |
228 | int status = -ENOENT; | |
ccd979bd | 229 | |
2b388c67 JB |
230 | mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n", |
231 | namelen, name, blkno, inode, dirent_bh, dirent); | |
ccd979bd MF |
232 | |
233 | *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent); | |
234 | if (!*dirent_bh || !*dirent) { | |
235 | status = -ENOENT; | |
236 | goto leave; | |
237 | } | |
238 | ||
239 | *blkno = le64_to_cpu((*dirent)->inode); | |
240 | ||
241 | status = 0; | |
242 | leave: | |
243 | if (status < 0) { | |
244 | *dirent = NULL; | |
245 | if (*dirent_bh) { | |
246 | brelse(*dirent_bh); | |
247 | *dirent_bh = NULL; | |
248 | } | |
249 | } | |
250 | ||
251 | mlog_exit(status); | |
252 | return status; | |
253 | } | |
254 | ||
255 | /* Check for a name within a directory. | |
256 | * | |
257 | * Return 0 if the name does not exist | |
258 | * Return -EEXIST if the directory contains the name | |
259 | * | |
1b1dcc1b | 260 | * Callers should have i_mutex + a cluster lock on dir |
ccd979bd MF |
261 | */ |
262 | int ocfs2_check_dir_for_entry(struct inode *dir, | |
263 | const char *name, | |
264 | int namelen) | |
265 | { | |
266 | int ret; | |
267 | struct buffer_head *dirent_bh = NULL; | |
268 | struct ocfs2_dir_entry *dirent = NULL; | |
269 | ||
b0697053 MF |
270 | mlog_entry("dir %llu, name '%.*s'\n", |
271 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); | |
ccd979bd MF |
272 | |
273 | ret = -EEXIST; | |
274 | dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent); | |
275 | if (dirent_bh) | |
276 | goto bail; | |
277 | ||
278 | ret = 0; | |
279 | bail: | |
280 | if (dirent_bh) | |
281 | brelse(dirent_bh); | |
282 | ||
283 | mlog_exit(ret); | |
284 | return ret; | |
285 | } | |
286 | ||
287 | /* | |
288 | * routine to check that the specified directory is empty (for rmdir) | |
289 | */ | |
290 | int ocfs2_empty_dir(struct inode *inode) | |
291 | { | |
292 | unsigned long offset; | |
293 | struct buffer_head * bh; | |
294 | struct ocfs2_dir_entry * de, * de1; | |
295 | struct super_block * sb; | |
296 | int err; | |
297 | ||
298 | sb = inode->i_sb; | |
299 | if ((i_size_read(inode) < | |
300 | (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) || | |
301 | !(bh = ocfs2_bread(inode, 0, &err, 0))) { | |
b0697053 MF |
302 | mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n", |
303 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd MF |
304 | return 1; |
305 | } | |
306 | ||
307 | de = (struct ocfs2_dir_entry *) bh->b_data; | |
308 | de1 = (struct ocfs2_dir_entry *) | |
309 | ((char *)de + le16_to_cpu(de->rec_len)); | |
310 | if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) || | |
311 | !le64_to_cpu(de1->inode) || | |
312 | strcmp(".", de->name) || | |
313 | strcmp("..", de1->name)) { | |
b0697053 MF |
314 | mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", |
315 | (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
ccd979bd MF |
316 | brelse(bh); |
317 | return 1; | |
318 | } | |
319 | offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len); | |
320 | de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len)); | |
321 | while (offset < i_size_read(inode) ) { | |
322 | if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) { | |
323 | brelse(bh); | |
324 | bh = ocfs2_bread(inode, | |
325 | offset >> sb->s_blocksize_bits, &err, 0); | |
326 | if (!bh) { | |
b0697053 MF |
327 | mlog(ML_ERROR, "dir %llu has a hole at %lu\n", |
328 | (unsigned long long)OCFS2_I(inode)->ip_blkno, offset); | |
ccd979bd MF |
329 | offset += sb->s_blocksize; |
330 | continue; | |
331 | } | |
332 | de = (struct ocfs2_dir_entry *) bh->b_data; | |
333 | } | |
334 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { | |
335 | brelse(bh); | |
336 | return 1; | |
337 | } | |
338 | if (le64_to_cpu(de->inode)) { | |
339 | brelse(bh); | |
340 | return 0; | |
341 | } | |
342 | offset += le16_to_cpu(de->rec_len); | |
343 | de = (struct ocfs2_dir_entry *) | |
344 | ((char *)de + le16_to_cpu(de->rec_len)); | |
345 | } | |
346 | brelse(bh); | |
347 | return 1; | |
348 | } | |
349 | ||
350 | /* returns a bh of the 1st new block in the allocation. */ | |
351 | int ocfs2_do_extend_dir(struct super_block *sb, | |
1fabe148 | 352 | handle_t *handle, |
ccd979bd MF |
353 | struct inode *dir, |
354 | struct buffer_head *parent_fe_bh, | |
355 | struct ocfs2_alloc_context *data_ac, | |
356 | struct ocfs2_alloc_context *meta_ac, | |
357 | struct buffer_head **new_bh) | |
358 | { | |
359 | int status; | |
360 | int extend; | |
361 | u64 p_blkno; | |
362 | ||
363 | spin_lock(&OCFS2_I(dir)->ip_lock); | |
364 | extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); | |
365 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
366 | ||
367 | if (extend) { | |
dcd0538f MF |
368 | u32 offset = OCFS2_I(dir)->ip_clusters; |
369 | ||
370 | status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset, | |
371 | 1, parent_fe_bh, handle, | |
ccd979bd MF |
372 | data_ac, meta_ac, NULL); |
373 | BUG_ON(status == -EAGAIN); | |
374 | if (status < 0) { | |
375 | mlog_errno(status); | |
376 | goto bail; | |
377 | } | |
378 | } | |
379 | ||
380 | status = ocfs2_extent_map_get_blocks(dir, (dir->i_blocks >> | |
381 | (sb->s_blocksize_bits - 9)), | |
49cb8d2d | 382 | &p_blkno, NULL, NULL); |
ccd979bd MF |
383 | if (status < 0) { |
384 | mlog_errno(status); | |
385 | goto bail; | |
386 | } | |
387 | ||
388 | *new_bh = sb_getblk(sb, p_blkno); | |
389 | if (!*new_bh) { | |
390 | status = -EIO; | |
391 | mlog_errno(status); | |
392 | goto bail; | |
393 | } | |
394 | status = 0; | |
395 | bail: | |
396 | mlog_exit(status); | |
397 | return status; | |
398 | } | |
399 | ||
400 | /* assumes you already have a cluster lock on the directory. */ | |
401 | static int ocfs2_extend_dir(struct ocfs2_super *osb, | |
402 | struct inode *dir, | |
403 | struct buffer_head *parent_fe_bh, | |
404 | struct buffer_head **new_de_bh) | |
405 | { | |
406 | int status = 0; | |
407 | int credits, num_free_extents; | |
408 | loff_t dir_i_size; | |
409 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; | |
410 | struct ocfs2_alloc_context *data_ac = NULL; | |
411 | struct ocfs2_alloc_context *meta_ac = NULL; | |
1fabe148 | 412 | handle_t *handle = NULL; |
ccd979bd MF |
413 | struct buffer_head *new_bh = NULL; |
414 | struct ocfs2_dir_entry * de; | |
415 | struct super_block *sb = osb->sb; | |
416 | ||
417 | mlog_entry_void(); | |
418 | ||
419 | dir_i_size = i_size_read(dir); | |
b0697053 MF |
420 | mlog(0, "extending dir %llu (i_size = %lld)\n", |
421 | (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size); | |
ccd979bd | 422 | |
ccd979bd MF |
423 | /* dir->i_size is always block aligned. */ |
424 | spin_lock(&OCFS2_I(dir)->ip_lock); | |
425 | if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { | |
426 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
427 | num_free_extents = ocfs2_num_free_extents(osb, dir, fe); | |
428 | if (num_free_extents < 0) { | |
429 | status = num_free_extents; | |
430 | mlog_errno(status); | |
431 | goto bail; | |
432 | } | |
433 | ||
434 | if (!num_free_extents) { | |
da5cbf2f | 435 | status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac); |
ccd979bd MF |
436 | if (status < 0) { |
437 | if (status != -ENOSPC) | |
438 | mlog_errno(status); | |
439 | goto bail; | |
440 | } | |
441 | } | |
442 | ||
da5cbf2f | 443 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
ccd979bd MF |
444 | if (status < 0) { |
445 | if (status != -ENOSPC) | |
446 | mlog_errno(status); | |
447 | goto bail; | |
448 | } | |
449 | ||
450 | credits = ocfs2_calc_extend_credits(sb, fe, 1); | |
451 | } else { | |
452 | spin_unlock(&OCFS2_I(dir)->ip_lock); | |
453 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; | |
454 | } | |
455 | ||
65eff9cc | 456 | handle = ocfs2_start_trans(osb, credits); |
ccd979bd MF |
457 | if (IS_ERR(handle)) { |
458 | status = PTR_ERR(handle); | |
459 | handle = NULL; | |
460 | mlog_errno(status); | |
461 | goto bail; | |
462 | } | |
463 | ||
464 | status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, | |
465 | data_ac, meta_ac, &new_bh); | |
466 | if (status < 0) { | |
467 | mlog_errno(status); | |
468 | goto bail; | |
469 | } | |
470 | ||
471 | ocfs2_set_new_buffer_uptodate(dir, new_bh); | |
472 | ||
473 | status = ocfs2_journal_access(handle, dir, new_bh, | |
474 | OCFS2_JOURNAL_ACCESS_CREATE); | |
475 | if (status < 0) { | |
476 | mlog_errno(status); | |
477 | goto bail; | |
478 | } | |
479 | memset(new_bh->b_data, 0, sb->s_blocksize); | |
480 | de = (struct ocfs2_dir_entry *) new_bh->b_data; | |
481 | de->inode = 0; | |
482 | de->rec_len = cpu_to_le16(sb->s_blocksize); | |
483 | status = ocfs2_journal_dirty(handle, new_bh); | |
484 | if (status < 0) { | |
485 | mlog_errno(status); | |
486 | goto bail; | |
487 | } | |
488 | ||
489 | dir_i_size += dir->i_sb->s_blocksize; | |
490 | i_size_write(dir, dir_i_size); | |
491 | dir->i_blocks = ocfs2_align_bytes_to_sectors(dir_i_size); | |
492 | status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); | |
493 | if (status < 0) { | |
494 | mlog_errno(status); | |
495 | goto bail; | |
496 | } | |
497 | ||
498 | *new_de_bh = new_bh; | |
499 | get_bh(*new_de_bh); | |
500 | bail: | |
501 | if (handle) | |
02dc1af4 | 502 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
503 | |
504 | if (data_ac) | |
505 | ocfs2_free_alloc_context(data_ac); | |
506 | if (meta_ac) | |
507 | ocfs2_free_alloc_context(meta_ac); | |
508 | ||
509 | if (new_bh) | |
510 | brelse(new_bh); | |
511 | ||
512 | mlog_exit(status); | |
513 | return status; | |
514 | } | |
515 | ||
516 | /* | |
517 | * Search the dir for a good spot, extending it if necessary. The | |
518 | * block containing an appropriate record is returned in ret_de_bh. | |
519 | */ | |
520 | int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, | |
521 | struct inode *dir, | |
522 | struct buffer_head *parent_fe_bh, | |
523 | const char *name, | |
524 | int namelen, | |
525 | struct buffer_head **ret_de_bh) | |
526 | { | |
527 | unsigned long offset; | |
528 | struct buffer_head * bh = NULL; | |
529 | unsigned short rec_len; | |
530 | struct ocfs2_dinode *fe; | |
531 | struct ocfs2_dir_entry *de; | |
532 | struct super_block *sb; | |
533 | int status; | |
534 | ||
535 | mlog_entry_void(); | |
536 | ||
b0697053 MF |
537 | mlog(0, "getting ready to insert namelen %d into dir %llu\n", |
538 | namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno); | |
ccd979bd MF |
539 | |
540 | BUG_ON(!S_ISDIR(dir->i_mode)); | |
541 | fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; | |
542 | BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir)); | |
543 | ||
544 | sb = dir->i_sb; | |
545 | ||
546 | if (!namelen) { | |
547 | status = -EINVAL; | |
548 | mlog_errno(status); | |
549 | goto bail; | |
550 | } | |
551 | ||
552 | bh = ocfs2_bread(dir, 0, &status, 0); | |
553 | if (!bh) { | |
554 | mlog_errno(status); | |
555 | goto bail; | |
556 | } | |
557 | ||
558 | rec_len = OCFS2_DIR_REC_LEN(namelen); | |
559 | offset = 0; | |
560 | de = (struct ocfs2_dir_entry *) bh->b_data; | |
561 | while (1) { | |
562 | if ((char *)de >= sb->s_blocksize + bh->b_data) { | |
563 | brelse(bh); | |
564 | bh = NULL; | |
565 | ||
566 | if (i_size_read(dir) <= offset) { | |
567 | status = ocfs2_extend_dir(osb, | |
568 | dir, | |
569 | parent_fe_bh, | |
570 | &bh); | |
571 | if (status < 0) { | |
572 | mlog_errno(status); | |
573 | goto bail; | |
574 | } | |
575 | BUG_ON(!bh); | |
576 | *ret_de_bh = bh; | |
577 | get_bh(*ret_de_bh); | |
578 | goto bail; | |
579 | } | |
580 | bh = ocfs2_bread(dir, | |
581 | offset >> sb->s_blocksize_bits, | |
582 | &status, | |
583 | 0); | |
584 | if (!bh) { | |
585 | mlog_errno(status); | |
586 | goto bail; | |
587 | } | |
588 | /* move to next block */ | |
589 | de = (struct ocfs2_dir_entry *) bh->b_data; | |
590 | } | |
591 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { | |
592 | status = -ENOENT; | |
593 | goto bail; | |
594 | } | |
595 | if (ocfs2_match(namelen, name, de)) { | |
596 | status = -EEXIST; | |
597 | goto bail; | |
598 | } | |
599 | if (((le64_to_cpu(de->inode) == 0) && | |
600 | (le16_to_cpu(de->rec_len) >= rec_len)) || | |
601 | (le16_to_cpu(de->rec_len) >= | |
602 | (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) { | |
603 | /* Ok, we found a spot. Return this bh and let | |
604 | * the caller actually fill it in. */ | |
605 | *ret_de_bh = bh; | |
606 | get_bh(*ret_de_bh); | |
607 | status = 0; | |
608 | goto bail; | |
609 | } | |
610 | offset += le16_to_cpu(de->rec_len); | |
611 | de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); | |
612 | } | |
613 | ||
614 | status = 0; | |
615 | bail: | |
616 | if (bh) | |
617 | brelse(bh); | |
618 | ||
619 | mlog_exit(status); | |
620 | return status; | |
621 | } |