dir.c 15 KB

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