dir.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. #include <linux/fs.h>
  39. #include <linux/types.h>
  40. #include <linux/slab.h>
  41. #include <linux/highmem.h>
  42. #define MLOG_MASK_PREFIX ML_NAMEI
  43. #include <cluster/masklog.h>
  44. #include "ocfs2.h"
  45. #include "alloc.h"
  46. #include "dir.h"
  47. #include "dlmglue.h"
  48. #include "extent_map.h"
  49. #include "file.h"
  50. #include "inode.h"
  51. #include "journal.h"
  52. #include "namei.h"
  53. #include "suballoc.h"
  54. #include "super.h"
  55. #include "uptodate.h"
  56. #include "buffer_head_io.h"
  57. #define NAMEI_RA_CHUNKS 2
  58. #define NAMEI_RA_BLOCKS 4
  59. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  60. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  61. static unsigned char ocfs2_filetype_table[] = {
  62. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  63. };
  64. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  65. struct inode *dir,
  66. struct buffer_head *parent_fe_bh,
  67. struct buffer_head **new_de_bh);
  68. static int ocfs2_do_extend_dir(struct super_block *sb,
  69. handle_t *handle,
  70. struct inode *dir,
  71. struct buffer_head *parent_fe_bh,
  72. struct ocfs2_alloc_context *data_ac,
  73. struct ocfs2_alloc_context *meta_ac,
  74. struct buffer_head **new_bh);
  75. int ocfs2_check_dir_entry(struct inode * dir,
  76. struct ocfs2_dir_entry * de,
  77. struct buffer_head * bh,
  78. unsigned long offset)
  79. {
  80. const char *error_msg = NULL;
  81. const int rlen = le16_to_cpu(de->rec_len);
  82. if (rlen < OCFS2_DIR_REC_LEN(1))
  83. error_msg = "rec_len is smaller than minimal";
  84. else if (rlen % 4 != 0)
  85. error_msg = "rec_len % 4 != 0";
  86. else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
  87. error_msg = "rec_len is too small for name_len";
  88. else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
  89. error_msg = "directory entry across blocks";
  90. if (error_msg != NULL)
  91. mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
  92. "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
  93. (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
  94. offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
  95. de->name_len);
  96. return error_msg == NULL ? 1 : 0;
  97. }
  98. static inline int ocfs2_match(int len,
  99. const char * const name,
  100. struct ocfs2_dir_entry *de)
  101. {
  102. if (len != de->name_len)
  103. return 0;
  104. if (!de->inode)
  105. return 0;
  106. return !memcmp(name, de->name, len);
  107. }
  108. /*
  109. * Returns 0 if not found, -1 on failure, and 1 on success
  110. */
  111. static int inline ocfs2_search_dirblock(struct buffer_head *bh,
  112. struct inode *dir,
  113. const char *name, int namelen,
  114. unsigned long offset,
  115. struct ocfs2_dir_entry **res_dir)
  116. {
  117. struct ocfs2_dir_entry *de;
  118. char *dlimit, *de_buf;
  119. int de_len;
  120. int ret = 0;
  121. mlog_entry_void();
  122. de_buf = bh->b_data;
  123. dlimit = de_buf + dir->i_sb->s_blocksize;
  124. while (de_buf < dlimit) {
  125. /* this code is executed quadratically often */
  126. /* do minimal checking `by hand' */
  127. de = (struct ocfs2_dir_entry *) de_buf;
  128. if (de_buf + namelen <= dlimit &&
  129. ocfs2_match(namelen, name, de)) {
  130. /* found a match - just to be sure, do a full check */
  131. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  132. ret = -1;
  133. goto bail;
  134. }
  135. *res_dir = de;
  136. ret = 1;
  137. goto bail;
  138. }
  139. /* prevent looping on a bad block */
  140. de_len = le16_to_cpu(de->rec_len);
  141. if (de_len <= 0) {
  142. ret = -1;
  143. goto bail;
  144. }
  145. de_buf += de_len;
  146. offset += de_len;
  147. }
  148. bail:
  149. mlog_exit(ret);
  150. return ret;
  151. }
  152. struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
  153. struct inode *dir,
  154. struct ocfs2_dir_entry **res_dir)
  155. {
  156. struct super_block *sb;
  157. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  158. struct buffer_head *bh, *ret = NULL;
  159. unsigned long start, block, b;
  160. int ra_max = 0; /* Number of bh's in the readahead
  161. buffer, bh_use[] */
  162. int ra_ptr = 0; /* Current index into readahead
  163. buffer */
  164. int num = 0;
  165. int nblocks, i, err;
  166. mlog_entry_void();
  167. *res_dir = NULL;
  168. sb = dir->i_sb;
  169. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  170. start = OCFS2_I(dir)->ip_dir_start_lookup;
  171. if (start >= nblocks)
  172. start = 0;
  173. block = start;
  174. restart:
  175. do {
  176. /*
  177. * We deal with the read-ahead logic here.
  178. */
  179. if (ra_ptr >= ra_max) {
  180. /* Refill the readahead buffer */
  181. ra_ptr = 0;
  182. b = block;
  183. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  184. /*
  185. * Terminate if we reach the end of the
  186. * directory and must wrap, or if our
  187. * search has finished at this block.
  188. */
  189. if (b >= nblocks || (num && block == start)) {
  190. bh_use[ra_max] = NULL;
  191. break;
  192. }
  193. num++;
  194. bh = ocfs2_bread(dir, b++, &err, 1);
  195. bh_use[ra_max] = bh;
  196. }
  197. }
  198. if ((bh = bh_use[ra_ptr++]) == NULL)
  199. goto next;
  200. wait_on_buffer(bh);
  201. if (!buffer_uptodate(bh)) {
  202. /* read error, skip block & hope for the best */
  203. ocfs2_error(dir->i_sb, "reading directory %llu, "
  204. "offset %lu\n",
  205. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  206. block);
  207. brelse(bh);
  208. goto next;
  209. }
  210. i = ocfs2_search_dirblock(bh, dir, name, namelen,
  211. block << sb->s_blocksize_bits,
  212. res_dir);
  213. if (i == 1) {
  214. OCFS2_I(dir)->ip_dir_start_lookup = block;
  215. ret = bh;
  216. goto cleanup_and_exit;
  217. } else {
  218. brelse(bh);
  219. if (i < 0)
  220. goto cleanup_and_exit;
  221. }
  222. next:
  223. if (++block >= nblocks)
  224. block = 0;
  225. } while (block != start);
  226. /*
  227. * If the directory has grown while we were searching, then
  228. * search the last part of the directory before giving up.
  229. */
  230. block = nblocks;
  231. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  232. if (block < nblocks) {
  233. start = 0;
  234. goto restart;
  235. }
  236. cleanup_and_exit:
  237. /* Clean up the read-ahead blocks */
  238. for (; ra_ptr < ra_max; ra_ptr++)
  239. brelse(bh_use[ra_ptr]);
  240. mlog_exit_ptr(ret);
  241. return ret;
  242. }
  243. /*
  244. * ocfs2_delete_entry deletes a directory entry by merging it with the
  245. * previous entry
  246. */
  247. int ocfs2_delete_entry(handle_t *handle,
  248. struct inode *dir,
  249. struct ocfs2_dir_entry *de_del,
  250. struct buffer_head *bh)
  251. {
  252. struct ocfs2_dir_entry *de, *pde;
  253. int i, status = -ENOENT;
  254. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
  255. i = 0;
  256. pde = NULL;
  257. de = (struct ocfs2_dir_entry *) bh->b_data;
  258. while (i < bh->b_size) {
  259. if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
  260. status = -EIO;
  261. mlog_errno(status);
  262. goto bail;
  263. }
  264. if (de == de_del) {
  265. status = ocfs2_journal_access(handle, dir, bh,
  266. OCFS2_JOURNAL_ACCESS_WRITE);
  267. if (status < 0) {
  268. status = -EIO;
  269. mlog_errno(status);
  270. goto bail;
  271. }
  272. if (pde)
  273. pde->rec_len =
  274. cpu_to_le16(le16_to_cpu(pde->rec_len) +
  275. le16_to_cpu(de->rec_len));
  276. else
  277. de->inode = 0;
  278. dir->i_version++;
  279. status = ocfs2_journal_dirty(handle, bh);
  280. goto bail;
  281. }
  282. i += le16_to_cpu(de->rec_len);
  283. pde = de;
  284. de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
  285. }
  286. bail:
  287. mlog_exit(status);
  288. return status;
  289. }
  290. /* we don't always have a dentry for what we want to add, so people
  291. * like orphan dir can call this instead.
  292. *
  293. * If you pass me insert_bh, I'll skip the search of the other dir
  294. * blocks and put the record in there.
  295. */
  296. int __ocfs2_add_entry(handle_t *handle,
  297. struct inode *dir,
  298. const char *name, int namelen,
  299. struct inode *inode, u64 blkno,
  300. struct buffer_head *parent_fe_bh,
  301. struct buffer_head *insert_bh)
  302. {
  303. unsigned long offset;
  304. unsigned short rec_len;
  305. struct ocfs2_dir_entry *de, *de1;
  306. struct super_block *sb;
  307. int retval, status;
  308. mlog_entry_void();
  309. sb = dir->i_sb;
  310. if (!namelen)
  311. return -EINVAL;
  312. rec_len = OCFS2_DIR_REC_LEN(namelen);
  313. offset = 0;
  314. de = (struct ocfs2_dir_entry *) insert_bh->b_data;
  315. while (1) {
  316. BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
  317. /* These checks should've already been passed by the
  318. * prepare function, but I guess we can leave them
  319. * here anyway. */
  320. if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
  321. retval = -ENOENT;
  322. goto bail;
  323. }
  324. if (ocfs2_match(namelen, name, de)) {
  325. retval = -EEXIST;
  326. goto bail;
  327. }
  328. if (((le64_to_cpu(de->inode) == 0) &&
  329. (le16_to_cpu(de->rec_len) >= rec_len)) ||
  330. (le16_to_cpu(de->rec_len) >=
  331. (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
  332. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  333. retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  334. if (retval < 0) {
  335. mlog_errno(retval);
  336. goto bail;
  337. }
  338. status = ocfs2_journal_access(handle, dir, insert_bh,
  339. OCFS2_JOURNAL_ACCESS_WRITE);
  340. /* By now the buffer is marked for journaling */
  341. offset += le16_to_cpu(de->rec_len);
  342. if (le64_to_cpu(de->inode)) {
  343. de1 = (struct ocfs2_dir_entry *)((char *) de +
  344. OCFS2_DIR_REC_LEN(de->name_len));
  345. de1->rec_len =
  346. cpu_to_le16(le16_to_cpu(de->rec_len) -
  347. OCFS2_DIR_REC_LEN(de->name_len));
  348. de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  349. de = de1;
  350. }
  351. de->file_type = OCFS2_FT_UNKNOWN;
  352. if (blkno) {
  353. de->inode = cpu_to_le64(blkno);
  354. ocfs2_set_de_type(de, inode->i_mode);
  355. } else
  356. de->inode = 0;
  357. de->name_len = namelen;
  358. memcpy(de->name, name, namelen);
  359. dir->i_version++;
  360. status = ocfs2_journal_dirty(handle, insert_bh);
  361. retval = 0;
  362. goto bail;
  363. }
  364. offset += le16_to_cpu(de->rec_len);
  365. de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
  366. }
  367. /* when you think about it, the assert above should prevent us
  368. * from ever getting here. */
  369. retval = -ENOSPC;
  370. bail:
  371. mlog_exit(retval);
  372. return retval;
  373. }
  374. static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
  375. loff_t *f_pos, void *priv, filldir_t filldir)
  376. {
  377. int error = 0;
  378. unsigned long offset, blk, last_ra_blk = 0;
  379. int i, stored;
  380. struct buffer_head * bh, * tmp;
  381. struct ocfs2_dir_entry * de;
  382. int err;
  383. struct super_block * sb = inode->i_sb;
  384. unsigned int ra_sectors = 16;
  385. stored = 0;
  386. bh = NULL;
  387. offset = (*f_pos) & (sb->s_blocksize - 1);
  388. while (!error && !stored && *f_pos < i_size_read(inode)) {
  389. blk = (*f_pos) >> sb->s_blocksize_bits;
  390. bh = ocfs2_bread(inode, blk, &err, 0);
  391. if (!bh) {
  392. mlog(ML_ERROR,
  393. "directory #%llu contains a hole at offset %lld\n",
  394. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  395. *f_pos);
  396. *f_pos += sb->s_blocksize - offset;
  397. continue;
  398. }
  399. /* The idea here is to begin with 8k read-ahead and to stay
  400. * 4k ahead of our current position.
  401. *
  402. * TODO: Use the pagecache for this. We just need to
  403. * make sure it's cluster-safe... */
  404. if (!last_ra_blk
  405. || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
  406. for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
  407. i > 0; i--) {
  408. tmp = ocfs2_bread(inode, ++blk, &err, 1);
  409. if (tmp)
  410. brelse(tmp);
  411. }
  412. last_ra_blk = blk;
  413. ra_sectors = 8;
  414. }
  415. revalidate:
  416. /* If the dir block has changed since the last call to
  417. * readdir(2), then we might be pointing to an invalid
  418. * dirent right now. Scan from the start of the block
  419. * to make sure. */
  420. if (*f_version != inode->i_version) {
  421. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  422. de = (struct ocfs2_dir_entry *) (bh->b_data + i);
  423. /* It's too expensive to do a full
  424. * dirent test each time round this
  425. * loop, but we do have to test at
  426. * least that it is non-zero. A
  427. * failure will be detected in the
  428. * dirent test below. */
  429. if (le16_to_cpu(de->rec_len) <
  430. OCFS2_DIR_REC_LEN(1))
  431. break;
  432. i += le16_to_cpu(de->rec_len);
  433. }
  434. offset = i;
  435. *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
  436. | offset;
  437. *f_version = inode->i_version;
  438. }
  439. while (!error && *f_pos < i_size_read(inode)
  440. && offset < sb->s_blocksize) {
  441. de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
  442. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  443. /* On error, skip the f_pos to the
  444. next block. */
  445. *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
  446. brelse(bh);
  447. goto out;
  448. }
  449. offset += le16_to_cpu(de->rec_len);
  450. if (le64_to_cpu(de->inode)) {
  451. /* We might block in the next section
  452. * if the data destination is
  453. * currently swapped out. So, use a
  454. * version stamp to detect whether or
  455. * not the directory has been modified
  456. * during the copy operation.
  457. */
  458. unsigned long version = *f_version;
  459. unsigned char d_type = DT_UNKNOWN;
  460. if (de->file_type < OCFS2_FT_MAX)
  461. d_type = ocfs2_filetype_table[de->file_type];
  462. error = filldir(priv, de->name,
  463. de->name_len,
  464. *f_pos,
  465. le64_to_cpu(de->inode),
  466. d_type);
  467. if (error)
  468. break;
  469. if (version != *f_version)
  470. goto revalidate;
  471. stored ++;
  472. }
  473. *f_pos += le16_to_cpu(de->rec_len);
  474. }
  475. offset = 0;
  476. brelse(bh);
  477. }
  478. stored = 0;
  479. out:
  480. return stored;
  481. }
  482. /*
  483. * ocfs2_readdir()
  484. *
  485. */
  486. int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
  487. {
  488. int error = 0;
  489. struct inode *inode = filp->f_path.dentry->d_inode;
  490. int lock_level = 0;
  491. mlog_entry("dirino=%llu\n",
  492. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  493. error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  494. if (lock_level && error >= 0) {
  495. /* We release EX lock which used to update atime
  496. * and get PR lock again to reduce contention
  497. * on commonly accessed directories. */
  498. ocfs2_meta_unlock(inode, 1);
  499. lock_level = 0;
  500. error = ocfs2_meta_lock(inode, NULL, 0);
  501. }
  502. if (error < 0) {
  503. if (error != -ENOENT)
  504. mlog_errno(error);
  505. /* we haven't got any yet, so propagate the error. */
  506. goto bail_nolock;
  507. }
  508. error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
  509. dirent, filldir);
  510. ocfs2_meta_unlock(inode, lock_level);
  511. bail_nolock:
  512. mlog_exit(error);
  513. return error;
  514. }
  515. /*
  516. * NOTE: this should always be called with parent dir i_mutex taken.
  517. */
  518. int ocfs2_find_files_on_disk(const char *name,
  519. int namelen,
  520. u64 *blkno,
  521. struct inode *inode,
  522. struct buffer_head **dirent_bh,
  523. struct ocfs2_dir_entry **dirent)
  524. {
  525. int status = -ENOENT;
  526. mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
  527. namelen, name, blkno, inode, dirent_bh, dirent);
  528. *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
  529. if (!*dirent_bh || !*dirent) {
  530. status = -ENOENT;
  531. goto leave;
  532. }
  533. *blkno = le64_to_cpu((*dirent)->inode);
  534. status = 0;
  535. leave:
  536. if (status < 0) {
  537. *dirent = NULL;
  538. if (*dirent_bh) {
  539. brelse(*dirent_bh);
  540. *dirent_bh = NULL;
  541. }
  542. }
  543. mlog_exit(status);
  544. return status;
  545. }
  546. /* Check for a name within a directory.
  547. *
  548. * Return 0 if the name does not exist
  549. * Return -EEXIST if the directory contains the name
  550. *
  551. * Callers should have i_mutex + a cluster lock on dir
  552. */
  553. int ocfs2_check_dir_for_entry(struct inode *dir,
  554. const char *name,
  555. int namelen)
  556. {
  557. int ret;
  558. struct buffer_head *dirent_bh = NULL;
  559. struct ocfs2_dir_entry *dirent = NULL;
  560. mlog_entry("dir %llu, name '%.*s'\n",
  561. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  562. ret = -EEXIST;
  563. dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
  564. if (dirent_bh)
  565. goto bail;
  566. ret = 0;
  567. bail:
  568. if (dirent_bh)
  569. brelse(dirent_bh);
  570. mlog_exit(ret);
  571. return ret;
  572. }
  573. /*
  574. * routine to check that the specified directory is empty (for rmdir)
  575. */
  576. int ocfs2_empty_dir(struct inode *inode)
  577. {
  578. unsigned long offset;
  579. struct buffer_head * bh;
  580. struct ocfs2_dir_entry * de, * de1;
  581. struct super_block * sb;
  582. int err;
  583. sb = inode->i_sb;
  584. if ((i_size_read(inode) <
  585. (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
  586. !(bh = ocfs2_bread(inode, 0, &err, 0))) {
  587. mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n",
  588. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  589. return 1;
  590. }
  591. de = (struct ocfs2_dir_entry *) bh->b_data;
  592. de1 = (struct ocfs2_dir_entry *)
  593. ((char *)de + le16_to_cpu(de->rec_len));
  594. if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
  595. !le64_to_cpu(de1->inode) ||
  596. strcmp(".", de->name) ||
  597. strcmp("..", de1->name)) {
  598. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  599. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  600. brelse(bh);
  601. return 1;
  602. }
  603. offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
  604. de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
  605. while (offset < i_size_read(inode) ) {
  606. if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
  607. brelse(bh);
  608. bh = ocfs2_bread(inode,
  609. offset >> sb->s_blocksize_bits, &err, 0);
  610. if (!bh) {
  611. mlog(ML_ERROR, "dir %llu has a hole at %lu\n",
  612. (unsigned long long)OCFS2_I(inode)->ip_blkno, offset);
  613. offset += sb->s_blocksize;
  614. continue;
  615. }
  616. de = (struct ocfs2_dir_entry *) bh->b_data;
  617. }
  618. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  619. brelse(bh);
  620. return 1;
  621. }
  622. if (le64_to_cpu(de->inode)) {
  623. brelse(bh);
  624. return 0;
  625. }
  626. offset += le16_to_cpu(de->rec_len);
  627. de = (struct ocfs2_dir_entry *)
  628. ((char *)de + le16_to_cpu(de->rec_len));
  629. }
  630. brelse(bh);
  631. return 1;
  632. }
  633. int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  634. handle_t *handle,
  635. struct inode *parent,
  636. struct inode *inode,
  637. struct buffer_head *fe_bh,
  638. struct ocfs2_alloc_context *data_ac)
  639. {
  640. int status;
  641. struct buffer_head *new_bh = NULL;
  642. struct ocfs2_dir_entry *de = NULL;
  643. mlog_entry_void();
  644. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  645. data_ac, NULL, &new_bh);
  646. if (status < 0) {
  647. mlog_errno(status);
  648. goto bail;
  649. }
  650. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  651. status = ocfs2_journal_access(handle, inode, new_bh,
  652. OCFS2_JOURNAL_ACCESS_CREATE);
  653. if (status < 0) {
  654. mlog_errno(status);
  655. goto bail;
  656. }
  657. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  658. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  659. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  660. de->name_len = 1;
  661. de->rec_len =
  662. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  663. strcpy(de->name, ".");
  664. ocfs2_set_de_type(de, S_IFDIR);
  665. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  666. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  667. de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
  668. OCFS2_DIR_REC_LEN(1));
  669. de->name_len = 2;
  670. strcpy(de->name, "..");
  671. ocfs2_set_de_type(de, S_IFDIR);
  672. status = ocfs2_journal_dirty(handle, new_bh);
  673. if (status < 0) {
  674. mlog_errno(status);
  675. goto bail;
  676. }
  677. i_size_write(inode, inode->i_sb->s_blocksize);
  678. inode->i_nlink = 2;
  679. inode->i_blocks = ocfs2_inode_sector_count(inode);
  680. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  681. if (status < 0) {
  682. mlog_errno(status);
  683. goto bail;
  684. }
  685. status = 0;
  686. bail:
  687. if (new_bh)
  688. brelse(new_bh);
  689. mlog_exit(status);
  690. return status;
  691. }
  692. /* returns a bh of the 1st new block in the allocation. */
  693. static int ocfs2_do_extend_dir(struct super_block *sb,
  694. handle_t *handle,
  695. struct inode *dir,
  696. struct buffer_head *parent_fe_bh,
  697. struct ocfs2_alloc_context *data_ac,
  698. struct ocfs2_alloc_context *meta_ac,
  699. struct buffer_head **new_bh)
  700. {
  701. int status;
  702. int extend;
  703. u64 p_blkno, v_blkno;
  704. spin_lock(&OCFS2_I(dir)->ip_lock);
  705. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  706. spin_unlock(&OCFS2_I(dir)->ip_lock);
  707. if (extend) {
  708. u32 offset = OCFS2_I(dir)->ip_clusters;
  709. status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
  710. 1, 0, parent_fe_bh, handle,
  711. data_ac, meta_ac, NULL);
  712. BUG_ON(status == -EAGAIN);
  713. if (status < 0) {
  714. mlog_errno(status);
  715. goto bail;
  716. }
  717. }
  718. v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  719. status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
  720. if (status < 0) {
  721. mlog_errno(status);
  722. goto bail;
  723. }
  724. *new_bh = sb_getblk(sb, p_blkno);
  725. if (!*new_bh) {
  726. status = -EIO;
  727. mlog_errno(status);
  728. goto bail;
  729. }
  730. status = 0;
  731. bail:
  732. mlog_exit(status);
  733. return status;
  734. }
  735. /* assumes you already have a cluster lock on the directory. */
  736. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  737. struct inode *dir,
  738. struct buffer_head *parent_fe_bh,
  739. struct buffer_head **new_de_bh)
  740. {
  741. int status = 0;
  742. int credits, num_free_extents, drop_alloc_sem = 0;
  743. loff_t dir_i_size;
  744. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  745. struct ocfs2_alloc_context *data_ac = NULL;
  746. struct ocfs2_alloc_context *meta_ac = NULL;
  747. handle_t *handle = NULL;
  748. struct buffer_head *new_bh = NULL;
  749. struct ocfs2_dir_entry * de;
  750. struct super_block *sb = osb->sb;
  751. mlog_entry_void();
  752. dir_i_size = i_size_read(dir);
  753. mlog(0, "extending dir %llu (i_size = %lld)\n",
  754. (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
  755. /* dir->i_size is always block aligned. */
  756. spin_lock(&OCFS2_I(dir)->ip_lock);
  757. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  758. spin_unlock(&OCFS2_I(dir)->ip_lock);
  759. num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
  760. if (num_free_extents < 0) {
  761. status = num_free_extents;
  762. mlog_errno(status);
  763. goto bail;
  764. }
  765. if (!num_free_extents) {
  766. status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
  767. if (status < 0) {
  768. if (status != -ENOSPC)
  769. mlog_errno(status);
  770. goto bail;
  771. }
  772. }
  773. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  774. if (status < 0) {
  775. if (status != -ENOSPC)
  776. mlog_errno(status);
  777. goto bail;
  778. }
  779. credits = ocfs2_calc_extend_credits(sb, fe, 1);
  780. } else {
  781. spin_unlock(&OCFS2_I(dir)->ip_lock);
  782. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  783. }
  784. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  785. drop_alloc_sem = 1;
  786. handle = ocfs2_start_trans(osb, credits);
  787. if (IS_ERR(handle)) {
  788. status = PTR_ERR(handle);
  789. handle = NULL;
  790. mlog_errno(status);
  791. goto bail;
  792. }
  793. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  794. data_ac, meta_ac, &new_bh);
  795. if (status < 0) {
  796. mlog_errno(status);
  797. goto bail;
  798. }
  799. ocfs2_set_new_buffer_uptodate(dir, new_bh);
  800. status = ocfs2_journal_access(handle, dir, new_bh,
  801. OCFS2_JOURNAL_ACCESS_CREATE);
  802. if (status < 0) {
  803. mlog_errno(status);
  804. goto bail;
  805. }
  806. memset(new_bh->b_data, 0, sb->s_blocksize);
  807. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  808. de->inode = 0;
  809. de->rec_len = cpu_to_le16(sb->s_blocksize);
  810. status = ocfs2_journal_dirty(handle, new_bh);
  811. if (status < 0) {
  812. mlog_errno(status);
  813. goto bail;
  814. }
  815. dir_i_size += dir->i_sb->s_blocksize;
  816. i_size_write(dir, dir_i_size);
  817. dir->i_blocks = ocfs2_inode_sector_count(dir);
  818. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  819. if (status < 0) {
  820. mlog_errno(status);
  821. goto bail;
  822. }
  823. *new_de_bh = new_bh;
  824. get_bh(*new_de_bh);
  825. bail:
  826. if (drop_alloc_sem)
  827. up_write(&OCFS2_I(dir)->ip_alloc_sem);
  828. if (handle)
  829. ocfs2_commit_trans(osb, handle);
  830. if (data_ac)
  831. ocfs2_free_alloc_context(data_ac);
  832. if (meta_ac)
  833. ocfs2_free_alloc_context(meta_ac);
  834. if (new_bh)
  835. brelse(new_bh);
  836. mlog_exit(status);
  837. return status;
  838. }
  839. /*
  840. * Search the dir for a good spot, extending it if necessary. The
  841. * block containing an appropriate record is returned in ret_de_bh.
  842. */
  843. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  844. struct inode *dir,
  845. struct buffer_head *parent_fe_bh,
  846. const char *name,
  847. int namelen,
  848. struct buffer_head **ret_de_bh)
  849. {
  850. unsigned long offset;
  851. struct buffer_head * bh = NULL;
  852. unsigned short rec_len;
  853. struct ocfs2_dinode *fe;
  854. struct ocfs2_dir_entry *de;
  855. struct super_block *sb;
  856. int status;
  857. mlog_entry_void();
  858. mlog(0, "getting ready to insert namelen %d into dir %llu\n",
  859. namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  860. BUG_ON(!S_ISDIR(dir->i_mode));
  861. fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  862. BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
  863. sb = dir->i_sb;
  864. if (!namelen) {
  865. status = -EINVAL;
  866. mlog_errno(status);
  867. goto bail;
  868. }
  869. bh = ocfs2_bread(dir, 0, &status, 0);
  870. if (!bh) {
  871. mlog_errno(status);
  872. goto bail;
  873. }
  874. rec_len = OCFS2_DIR_REC_LEN(namelen);
  875. offset = 0;
  876. de = (struct ocfs2_dir_entry *) bh->b_data;
  877. while (1) {
  878. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  879. brelse(bh);
  880. bh = NULL;
  881. if (i_size_read(dir) <= offset) {
  882. status = ocfs2_extend_dir(osb,
  883. dir,
  884. parent_fe_bh,
  885. &bh);
  886. if (status < 0) {
  887. mlog_errno(status);
  888. goto bail;
  889. }
  890. BUG_ON(!bh);
  891. *ret_de_bh = bh;
  892. get_bh(*ret_de_bh);
  893. goto bail;
  894. }
  895. bh = ocfs2_bread(dir,
  896. offset >> sb->s_blocksize_bits,
  897. &status,
  898. 0);
  899. if (!bh) {
  900. mlog_errno(status);
  901. goto bail;
  902. }
  903. /* move to next block */
  904. de = (struct ocfs2_dir_entry *) bh->b_data;
  905. }
  906. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  907. status = -ENOENT;
  908. goto bail;
  909. }
  910. if (ocfs2_match(namelen, name, de)) {
  911. status = -EEXIST;
  912. goto bail;
  913. }
  914. if (((le64_to_cpu(de->inode) == 0) &&
  915. (le16_to_cpu(de->rec_len) >= rec_len)) ||
  916. (le16_to_cpu(de->rec_len) >=
  917. (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
  918. /* Ok, we found a spot. Return this bh and let
  919. * the caller actually fill it in. */
  920. *ret_de_bh = bh;
  921. get_bh(*ret_de_bh);
  922. status = 0;
  923. goto bail;
  924. }
  925. offset += le16_to_cpu(de->rec_len);
  926. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  927. }
  928. status = 0;
  929. bail:
  930. if (bh)
  931. brelse(bh);
  932. mlog_exit(status);
  933. return status;
  934. }