dir.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. /*
  375. * ocfs2_readdir()
  376. *
  377. */
  378. int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
  379. {
  380. int error = 0;
  381. unsigned long offset, blk, last_ra_blk = 0;
  382. int i, stored;
  383. struct buffer_head * bh, * tmp;
  384. struct ocfs2_dir_entry * de;
  385. int err;
  386. struct inode *inode = filp->f_path.dentry->d_inode;
  387. struct super_block * sb = inode->i_sb;
  388. unsigned int ra_sectors = 16;
  389. int lock_level = 0;
  390. mlog_entry("dirino=%llu\n",
  391. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  392. stored = 0;
  393. bh = NULL;
  394. error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  395. if (lock_level && error >= 0) {
  396. /* We release EX lock which used to update atime
  397. * and get PR lock again to reduce contention
  398. * on commonly accessed directories. */
  399. ocfs2_meta_unlock(inode, 1);
  400. lock_level = 0;
  401. error = ocfs2_meta_lock(inode, NULL, 0);
  402. }
  403. if (error < 0) {
  404. if (error != -ENOENT)
  405. mlog_errno(error);
  406. /* we haven't got any yet, so propagate the error. */
  407. stored = error;
  408. goto bail_nolock;
  409. }
  410. offset = filp->f_pos & (sb->s_blocksize - 1);
  411. while (!error && !stored && filp->f_pos < i_size_read(inode)) {
  412. blk = (filp->f_pos) >> sb->s_blocksize_bits;
  413. bh = ocfs2_bread(inode, blk, &err, 0);
  414. if (!bh) {
  415. mlog(ML_ERROR,
  416. "directory #%llu contains a hole at offset %lld\n",
  417. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  418. filp->f_pos);
  419. filp->f_pos += sb->s_blocksize - offset;
  420. continue;
  421. }
  422. /* The idea here is to begin with 8k read-ahead and to stay
  423. * 4k ahead of our current position.
  424. *
  425. * TODO: Use the pagecache for this. We just need to
  426. * make sure it's cluster-safe... */
  427. if (!last_ra_blk
  428. || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
  429. for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
  430. i > 0; i--) {
  431. tmp = ocfs2_bread(inode, ++blk, &err, 1);
  432. if (tmp)
  433. brelse(tmp);
  434. }
  435. last_ra_blk = blk;
  436. ra_sectors = 8;
  437. }
  438. revalidate:
  439. /* If the dir block has changed since the last call to
  440. * readdir(2), then we might be pointing to an invalid
  441. * dirent right now. Scan from the start of the block
  442. * to make sure. */
  443. if (filp->f_version != inode->i_version) {
  444. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  445. de = (struct ocfs2_dir_entry *) (bh->b_data + i);
  446. /* It's too expensive to do a full
  447. * dirent test each time round this
  448. * loop, but we do have to test at
  449. * least that it is non-zero. A
  450. * failure will be detected in the
  451. * dirent test below. */
  452. if (le16_to_cpu(de->rec_len) <
  453. OCFS2_DIR_REC_LEN(1))
  454. break;
  455. i += le16_to_cpu(de->rec_len);
  456. }
  457. offset = i;
  458. filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
  459. | offset;
  460. filp->f_version = inode->i_version;
  461. }
  462. while (!error && filp->f_pos < i_size_read(inode)
  463. && offset < sb->s_blocksize) {
  464. de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
  465. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  466. /* On error, skip the f_pos to the
  467. next block. */
  468. filp->f_pos = (filp->f_pos |
  469. (sb->s_blocksize - 1)) + 1;
  470. brelse(bh);
  471. goto bail;
  472. }
  473. offset += le16_to_cpu(de->rec_len);
  474. if (le64_to_cpu(de->inode)) {
  475. /* We might block in the next section
  476. * if the data destination is
  477. * currently swapped out. So, use a
  478. * version stamp to detect whether or
  479. * not the directory has been modified
  480. * during the copy operation.
  481. */
  482. unsigned long version = filp->f_version;
  483. unsigned char d_type = DT_UNKNOWN;
  484. if (de->file_type < OCFS2_FT_MAX)
  485. d_type = ocfs2_filetype_table[de->file_type];
  486. error = filldir(dirent, de->name,
  487. de->name_len,
  488. filp->f_pos,
  489. ino_from_blkno(sb, le64_to_cpu(de->inode)),
  490. d_type);
  491. if (error)
  492. break;
  493. if (version != filp->f_version)
  494. goto revalidate;
  495. stored ++;
  496. }
  497. filp->f_pos += le16_to_cpu(de->rec_len);
  498. }
  499. offset = 0;
  500. brelse(bh);
  501. }
  502. stored = 0;
  503. bail:
  504. ocfs2_meta_unlock(inode, lock_level);
  505. bail_nolock:
  506. mlog_exit(stored);
  507. return stored;
  508. }
  509. /*
  510. * NOTE: this should always be called with parent dir i_mutex taken.
  511. */
  512. int ocfs2_find_files_on_disk(const char *name,
  513. int namelen,
  514. u64 *blkno,
  515. struct inode *inode,
  516. struct buffer_head **dirent_bh,
  517. struct ocfs2_dir_entry **dirent)
  518. {
  519. int status = -ENOENT;
  520. mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
  521. namelen, name, blkno, inode, dirent_bh, dirent);
  522. *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
  523. if (!*dirent_bh || !*dirent) {
  524. status = -ENOENT;
  525. goto leave;
  526. }
  527. *blkno = le64_to_cpu((*dirent)->inode);
  528. status = 0;
  529. leave:
  530. if (status < 0) {
  531. *dirent = NULL;
  532. if (*dirent_bh) {
  533. brelse(*dirent_bh);
  534. *dirent_bh = NULL;
  535. }
  536. }
  537. mlog_exit(status);
  538. return status;
  539. }
  540. /* Check for a name within a directory.
  541. *
  542. * Return 0 if the name does not exist
  543. * Return -EEXIST if the directory contains the name
  544. *
  545. * Callers should have i_mutex + a cluster lock on dir
  546. */
  547. int ocfs2_check_dir_for_entry(struct inode *dir,
  548. const char *name,
  549. int namelen)
  550. {
  551. int ret;
  552. struct buffer_head *dirent_bh = NULL;
  553. struct ocfs2_dir_entry *dirent = NULL;
  554. mlog_entry("dir %llu, name '%.*s'\n",
  555. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  556. ret = -EEXIST;
  557. dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
  558. if (dirent_bh)
  559. goto bail;
  560. ret = 0;
  561. bail:
  562. if (dirent_bh)
  563. brelse(dirent_bh);
  564. mlog_exit(ret);
  565. return ret;
  566. }
  567. /*
  568. * routine to check that the specified directory is empty (for rmdir)
  569. */
  570. int ocfs2_empty_dir(struct inode *inode)
  571. {
  572. unsigned long offset;
  573. struct buffer_head * bh;
  574. struct ocfs2_dir_entry * de, * de1;
  575. struct super_block * sb;
  576. int err;
  577. sb = inode->i_sb;
  578. if ((i_size_read(inode) <
  579. (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
  580. !(bh = ocfs2_bread(inode, 0, &err, 0))) {
  581. mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n",
  582. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  583. return 1;
  584. }
  585. de = (struct ocfs2_dir_entry *) bh->b_data;
  586. de1 = (struct ocfs2_dir_entry *)
  587. ((char *)de + le16_to_cpu(de->rec_len));
  588. if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
  589. !le64_to_cpu(de1->inode) ||
  590. strcmp(".", de->name) ||
  591. strcmp("..", de1->name)) {
  592. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  593. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  594. brelse(bh);
  595. return 1;
  596. }
  597. offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
  598. de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
  599. while (offset < i_size_read(inode) ) {
  600. if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
  601. brelse(bh);
  602. bh = ocfs2_bread(inode,
  603. offset >> sb->s_blocksize_bits, &err, 0);
  604. if (!bh) {
  605. mlog(ML_ERROR, "dir %llu has a hole at %lu\n",
  606. (unsigned long long)OCFS2_I(inode)->ip_blkno, offset);
  607. offset += sb->s_blocksize;
  608. continue;
  609. }
  610. de = (struct ocfs2_dir_entry *) bh->b_data;
  611. }
  612. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  613. brelse(bh);
  614. return 1;
  615. }
  616. if (le64_to_cpu(de->inode)) {
  617. brelse(bh);
  618. return 0;
  619. }
  620. offset += le16_to_cpu(de->rec_len);
  621. de = (struct ocfs2_dir_entry *)
  622. ((char *)de + le16_to_cpu(de->rec_len));
  623. }
  624. brelse(bh);
  625. return 1;
  626. }
  627. int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  628. handle_t *handle,
  629. struct inode *parent,
  630. struct inode *inode,
  631. struct buffer_head *fe_bh,
  632. struct ocfs2_alloc_context *data_ac)
  633. {
  634. int status;
  635. struct buffer_head *new_bh = NULL;
  636. struct ocfs2_dir_entry *de = NULL;
  637. mlog_entry_void();
  638. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  639. data_ac, NULL, &new_bh);
  640. if (status < 0) {
  641. mlog_errno(status);
  642. goto bail;
  643. }
  644. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  645. status = ocfs2_journal_access(handle, inode, new_bh,
  646. OCFS2_JOURNAL_ACCESS_CREATE);
  647. if (status < 0) {
  648. mlog_errno(status);
  649. goto bail;
  650. }
  651. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  652. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  653. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  654. de->name_len = 1;
  655. de->rec_len =
  656. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  657. strcpy(de->name, ".");
  658. ocfs2_set_de_type(de, S_IFDIR);
  659. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  660. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  661. de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
  662. OCFS2_DIR_REC_LEN(1));
  663. de->name_len = 2;
  664. strcpy(de->name, "..");
  665. ocfs2_set_de_type(de, S_IFDIR);
  666. status = ocfs2_journal_dirty(handle, new_bh);
  667. if (status < 0) {
  668. mlog_errno(status);
  669. goto bail;
  670. }
  671. i_size_write(inode, inode->i_sb->s_blocksize);
  672. inode->i_nlink = 2;
  673. inode->i_blocks = ocfs2_inode_sector_count(inode);
  674. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  675. if (status < 0) {
  676. mlog_errno(status);
  677. goto bail;
  678. }
  679. status = 0;
  680. bail:
  681. if (new_bh)
  682. brelse(new_bh);
  683. mlog_exit(status);
  684. return status;
  685. }
  686. /* returns a bh of the 1st new block in the allocation. */
  687. static int ocfs2_do_extend_dir(struct super_block *sb,
  688. handle_t *handle,
  689. struct inode *dir,
  690. struct buffer_head *parent_fe_bh,
  691. struct ocfs2_alloc_context *data_ac,
  692. struct ocfs2_alloc_context *meta_ac,
  693. struct buffer_head **new_bh)
  694. {
  695. int status;
  696. int extend;
  697. u64 p_blkno, v_blkno;
  698. spin_lock(&OCFS2_I(dir)->ip_lock);
  699. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  700. spin_unlock(&OCFS2_I(dir)->ip_lock);
  701. if (extend) {
  702. u32 offset = OCFS2_I(dir)->ip_clusters;
  703. status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
  704. 1, 0, parent_fe_bh, handle,
  705. data_ac, meta_ac, NULL);
  706. BUG_ON(status == -EAGAIN);
  707. if (status < 0) {
  708. mlog_errno(status);
  709. goto bail;
  710. }
  711. }
  712. v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  713. status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
  714. if (status < 0) {
  715. mlog_errno(status);
  716. goto bail;
  717. }
  718. *new_bh = sb_getblk(sb, p_blkno);
  719. if (!*new_bh) {
  720. status = -EIO;
  721. mlog_errno(status);
  722. goto bail;
  723. }
  724. status = 0;
  725. bail:
  726. mlog_exit(status);
  727. return status;
  728. }
  729. /* assumes you already have a cluster lock on the directory. */
  730. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  731. struct inode *dir,
  732. struct buffer_head *parent_fe_bh,
  733. struct buffer_head **new_de_bh)
  734. {
  735. int status = 0;
  736. int credits, num_free_extents, drop_alloc_sem = 0;
  737. loff_t dir_i_size;
  738. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  739. struct ocfs2_alloc_context *data_ac = NULL;
  740. struct ocfs2_alloc_context *meta_ac = NULL;
  741. handle_t *handle = NULL;
  742. struct buffer_head *new_bh = NULL;
  743. struct ocfs2_dir_entry * de;
  744. struct super_block *sb = osb->sb;
  745. mlog_entry_void();
  746. dir_i_size = i_size_read(dir);
  747. mlog(0, "extending dir %llu (i_size = %lld)\n",
  748. (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
  749. /* dir->i_size is always block aligned. */
  750. spin_lock(&OCFS2_I(dir)->ip_lock);
  751. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  752. spin_unlock(&OCFS2_I(dir)->ip_lock);
  753. num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
  754. if (num_free_extents < 0) {
  755. status = num_free_extents;
  756. mlog_errno(status);
  757. goto bail;
  758. }
  759. if (!num_free_extents) {
  760. status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
  761. if (status < 0) {
  762. if (status != -ENOSPC)
  763. mlog_errno(status);
  764. goto bail;
  765. }
  766. }
  767. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  768. if (status < 0) {
  769. if (status != -ENOSPC)
  770. mlog_errno(status);
  771. goto bail;
  772. }
  773. credits = ocfs2_calc_extend_credits(sb, fe, 1);
  774. } else {
  775. spin_unlock(&OCFS2_I(dir)->ip_lock);
  776. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  777. }
  778. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  779. drop_alloc_sem = 1;
  780. handle = ocfs2_start_trans(osb, credits);
  781. if (IS_ERR(handle)) {
  782. status = PTR_ERR(handle);
  783. handle = NULL;
  784. mlog_errno(status);
  785. goto bail;
  786. }
  787. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  788. data_ac, meta_ac, &new_bh);
  789. if (status < 0) {
  790. mlog_errno(status);
  791. goto bail;
  792. }
  793. ocfs2_set_new_buffer_uptodate(dir, new_bh);
  794. status = ocfs2_journal_access(handle, dir, new_bh,
  795. OCFS2_JOURNAL_ACCESS_CREATE);
  796. if (status < 0) {
  797. mlog_errno(status);
  798. goto bail;
  799. }
  800. memset(new_bh->b_data, 0, sb->s_blocksize);
  801. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  802. de->inode = 0;
  803. de->rec_len = cpu_to_le16(sb->s_blocksize);
  804. status = ocfs2_journal_dirty(handle, new_bh);
  805. if (status < 0) {
  806. mlog_errno(status);
  807. goto bail;
  808. }
  809. dir_i_size += dir->i_sb->s_blocksize;
  810. i_size_write(dir, dir_i_size);
  811. dir->i_blocks = ocfs2_inode_sector_count(dir);
  812. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  813. if (status < 0) {
  814. mlog_errno(status);
  815. goto bail;
  816. }
  817. *new_de_bh = new_bh;
  818. get_bh(*new_de_bh);
  819. bail:
  820. if (drop_alloc_sem)
  821. up_write(&OCFS2_I(dir)->ip_alloc_sem);
  822. if (handle)
  823. ocfs2_commit_trans(osb, handle);
  824. if (data_ac)
  825. ocfs2_free_alloc_context(data_ac);
  826. if (meta_ac)
  827. ocfs2_free_alloc_context(meta_ac);
  828. if (new_bh)
  829. brelse(new_bh);
  830. mlog_exit(status);
  831. return status;
  832. }
  833. /*
  834. * Search the dir for a good spot, extending it if necessary. The
  835. * block containing an appropriate record is returned in ret_de_bh.
  836. */
  837. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  838. struct inode *dir,
  839. struct buffer_head *parent_fe_bh,
  840. const char *name,
  841. int namelen,
  842. struct buffer_head **ret_de_bh)
  843. {
  844. unsigned long offset;
  845. struct buffer_head * bh = NULL;
  846. unsigned short rec_len;
  847. struct ocfs2_dinode *fe;
  848. struct ocfs2_dir_entry *de;
  849. struct super_block *sb;
  850. int status;
  851. mlog_entry_void();
  852. mlog(0, "getting ready to insert namelen %d into dir %llu\n",
  853. namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  854. BUG_ON(!S_ISDIR(dir->i_mode));
  855. fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  856. BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
  857. sb = dir->i_sb;
  858. if (!namelen) {
  859. status = -EINVAL;
  860. mlog_errno(status);
  861. goto bail;
  862. }
  863. bh = ocfs2_bread(dir, 0, &status, 0);
  864. if (!bh) {
  865. mlog_errno(status);
  866. goto bail;
  867. }
  868. rec_len = OCFS2_DIR_REC_LEN(namelen);
  869. offset = 0;
  870. de = (struct ocfs2_dir_entry *) bh->b_data;
  871. while (1) {
  872. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  873. brelse(bh);
  874. bh = NULL;
  875. if (i_size_read(dir) <= offset) {
  876. status = ocfs2_extend_dir(osb,
  877. dir,
  878. parent_fe_bh,
  879. &bh);
  880. if (status < 0) {
  881. mlog_errno(status);
  882. goto bail;
  883. }
  884. BUG_ON(!bh);
  885. *ret_de_bh = bh;
  886. get_bh(*ret_de_bh);
  887. goto bail;
  888. }
  889. bh = ocfs2_bread(dir,
  890. offset >> sb->s_blocksize_bits,
  891. &status,
  892. 0);
  893. if (!bh) {
  894. mlog_errno(status);
  895. goto bail;
  896. }
  897. /* move to next block */
  898. de = (struct ocfs2_dir_entry *) bh->b_data;
  899. }
  900. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  901. status = -ENOENT;
  902. goto bail;
  903. }
  904. if (ocfs2_match(namelen, name, de)) {
  905. status = -EEXIST;
  906. goto bail;
  907. }
  908. if (((le64_to_cpu(de->inode) == 0) &&
  909. (le16_to_cpu(de->rec_len) >= rec_len)) ||
  910. (le16_to_cpu(de->rec_len) >=
  911. (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
  912. /* Ok, we found a spot. Return this bh and let
  913. * the caller actually fill it in. */
  914. *ret_de_bh = bh;
  915. get_bh(*ret_de_bh);
  916. status = 0;
  917. goto bail;
  918. }
  919. offset += le16_to_cpu(de->rec_len);
  920. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  921. }
  922. status = 0;
  923. bail:
  924. if (bh)
  925. brelse(bh);
  926. mlog_exit(status);
  927. return status;
  928. }