dir.c 15 KB

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