dir.c 15 KB

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