dir.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
  197. namelen, name, blkno, inode, dirent_bh, dirent);
  198. *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
  199. if (!*dirent_bh || !*dirent) {
  200. status = -ENOENT;
  201. goto leave;
  202. }
  203. *blkno = le64_to_cpu((*dirent)->inode);
  204. status = 0;
  205. leave:
  206. if (status < 0) {
  207. *dirent = NULL;
  208. if (*dirent_bh) {
  209. brelse(*dirent_bh);
  210. *dirent_bh = NULL;
  211. }
  212. }
  213. mlog_exit(status);
  214. return status;
  215. }
  216. /* Check for a name within a directory.
  217. *
  218. * Return 0 if the name does not exist
  219. * Return -EEXIST if the directory contains the name
  220. *
  221. * Callers should have i_mutex + a cluster lock on dir
  222. */
  223. int ocfs2_check_dir_for_entry(struct inode *dir,
  224. const char *name,
  225. int namelen)
  226. {
  227. int ret;
  228. struct buffer_head *dirent_bh = NULL;
  229. struct ocfs2_dir_entry *dirent = NULL;
  230. mlog_entry("dir %llu, name '%.*s'\n",
  231. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  232. ret = -EEXIST;
  233. dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
  234. if (dirent_bh)
  235. goto bail;
  236. ret = 0;
  237. bail:
  238. if (dirent_bh)
  239. brelse(dirent_bh);
  240. mlog_exit(ret);
  241. return ret;
  242. }
  243. /*
  244. * routine to check that the specified directory is empty (for rmdir)
  245. */
  246. int ocfs2_empty_dir(struct inode *inode)
  247. {
  248. unsigned long offset;
  249. struct buffer_head * bh;
  250. struct ocfs2_dir_entry * de, * de1;
  251. struct super_block * sb;
  252. int err;
  253. sb = inode->i_sb;
  254. if ((i_size_read(inode) <
  255. (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) ||
  256. !(bh = ocfs2_bread(inode, 0, &err, 0))) {
  257. mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n",
  258. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  259. return 1;
  260. }
  261. de = (struct ocfs2_dir_entry *) bh->b_data;
  262. de1 = (struct ocfs2_dir_entry *)
  263. ((char *)de + le16_to_cpu(de->rec_len));
  264. if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) ||
  265. !le64_to_cpu(de1->inode) ||
  266. strcmp(".", de->name) ||
  267. strcmp("..", de1->name)) {
  268. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  269. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  270. brelse(bh);
  271. return 1;
  272. }
  273. offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len);
  274. de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len));
  275. while (offset < i_size_read(inode) ) {
  276. if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) {
  277. brelse(bh);
  278. bh = ocfs2_bread(inode,
  279. offset >> sb->s_blocksize_bits, &err, 0);
  280. if (!bh) {
  281. mlog(ML_ERROR, "dir %llu has a hole at %lu\n",
  282. (unsigned long long)OCFS2_I(inode)->ip_blkno, offset);
  283. offset += sb->s_blocksize;
  284. continue;
  285. }
  286. de = (struct ocfs2_dir_entry *) bh->b_data;
  287. }
  288. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  289. brelse(bh);
  290. return 1;
  291. }
  292. if (le64_to_cpu(de->inode)) {
  293. brelse(bh);
  294. return 0;
  295. }
  296. offset += le16_to_cpu(de->rec_len);
  297. de = (struct ocfs2_dir_entry *)
  298. ((char *)de + le16_to_cpu(de->rec_len));
  299. }
  300. brelse(bh);
  301. return 1;
  302. }
  303. /* returns a bh of the 1st new block in the allocation. */
  304. int ocfs2_do_extend_dir(struct super_block *sb,
  305. struct ocfs2_journal_handle *handle,
  306. struct inode *dir,
  307. struct buffer_head *parent_fe_bh,
  308. struct ocfs2_alloc_context *data_ac,
  309. struct ocfs2_alloc_context *meta_ac,
  310. struct buffer_head **new_bh)
  311. {
  312. int status;
  313. int extend;
  314. u64 p_blkno;
  315. spin_lock(&OCFS2_I(dir)->ip_lock);
  316. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  317. spin_unlock(&OCFS2_I(dir)->ip_lock);
  318. if (extend) {
  319. status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, 1,
  320. parent_fe_bh, handle,
  321. data_ac, meta_ac, NULL);
  322. BUG_ON(status == -EAGAIN);
  323. if (status < 0) {
  324. mlog_errno(status);
  325. goto bail;
  326. }
  327. }
  328. status = ocfs2_extent_map_get_blocks(dir, (dir->i_blocks >>
  329. (sb->s_blocksize_bits - 9)),
  330. 1, &p_blkno, NULL);
  331. if (status < 0) {
  332. mlog_errno(status);
  333. goto bail;
  334. }
  335. *new_bh = sb_getblk(sb, p_blkno);
  336. if (!*new_bh) {
  337. status = -EIO;
  338. mlog_errno(status);
  339. goto bail;
  340. }
  341. status = 0;
  342. bail:
  343. mlog_exit(status);
  344. return status;
  345. }
  346. /* assumes you already have a cluster lock on the directory. */
  347. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  348. struct inode *dir,
  349. struct buffer_head *parent_fe_bh,
  350. struct buffer_head **new_de_bh)
  351. {
  352. int status = 0;
  353. int credits, num_free_extents;
  354. loff_t dir_i_size;
  355. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  356. struct ocfs2_alloc_context *data_ac = NULL;
  357. struct ocfs2_alloc_context *meta_ac = NULL;
  358. struct ocfs2_journal_handle *handle = NULL;
  359. struct buffer_head *new_bh = NULL;
  360. struct ocfs2_dir_entry * de;
  361. struct super_block *sb = osb->sb;
  362. mlog_entry_void();
  363. dir_i_size = i_size_read(dir);
  364. mlog(0, "extending dir %llu (i_size = %lld)\n",
  365. (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
  366. handle = ocfs2_alloc_handle(osb);
  367. if (handle == NULL) {
  368. status = -ENOMEM;
  369. mlog_errno(status);
  370. goto bail;
  371. }
  372. /* dir->i_size is always block aligned. */
  373. spin_lock(&OCFS2_I(dir)->ip_lock);
  374. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  375. spin_unlock(&OCFS2_I(dir)->ip_lock);
  376. num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
  377. if (num_free_extents < 0) {
  378. status = num_free_extents;
  379. mlog_errno(status);
  380. goto bail;
  381. }
  382. if (!num_free_extents) {
  383. status = ocfs2_reserve_new_metadata(osb, handle,
  384. fe, &meta_ac);
  385. if (status < 0) {
  386. if (status != -ENOSPC)
  387. mlog_errno(status);
  388. goto bail;
  389. }
  390. }
  391. status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
  392. if (status < 0) {
  393. if (status != -ENOSPC)
  394. mlog_errno(status);
  395. goto bail;
  396. }
  397. credits = ocfs2_calc_extend_credits(sb, fe, 1);
  398. } else {
  399. spin_unlock(&OCFS2_I(dir)->ip_lock);
  400. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  401. }
  402. handle = ocfs2_start_trans(osb, handle, credits);
  403. if (IS_ERR(handle)) {
  404. status = PTR_ERR(handle);
  405. handle = NULL;
  406. mlog_errno(status);
  407. goto bail;
  408. }
  409. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  410. data_ac, meta_ac, &new_bh);
  411. if (status < 0) {
  412. mlog_errno(status);
  413. goto bail;
  414. }
  415. ocfs2_set_new_buffer_uptodate(dir, new_bh);
  416. status = ocfs2_journal_access(handle, dir, new_bh,
  417. OCFS2_JOURNAL_ACCESS_CREATE);
  418. if (status < 0) {
  419. mlog_errno(status);
  420. goto bail;
  421. }
  422. memset(new_bh->b_data, 0, sb->s_blocksize);
  423. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  424. de->inode = 0;
  425. de->rec_len = cpu_to_le16(sb->s_blocksize);
  426. status = ocfs2_journal_dirty(handle, new_bh);
  427. if (status < 0) {
  428. mlog_errno(status);
  429. goto bail;
  430. }
  431. dir_i_size += dir->i_sb->s_blocksize;
  432. i_size_write(dir, dir_i_size);
  433. dir->i_blocks = ocfs2_align_bytes_to_sectors(dir_i_size);
  434. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  435. if (status < 0) {
  436. mlog_errno(status);
  437. goto bail;
  438. }
  439. *new_de_bh = new_bh;
  440. get_bh(*new_de_bh);
  441. bail:
  442. if (handle)
  443. ocfs2_commit_trans(handle);
  444. if (data_ac)
  445. ocfs2_free_alloc_context(data_ac);
  446. if (meta_ac)
  447. ocfs2_free_alloc_context(meta_ac);
  448. if (new_bh)
  449. brelse(new_bh);
  450. mlog_exit(status);
  451. return status;
  452. }
  453. /*
  454. * Search the dir for a good spot, extending it if necessary. The
  455. * block containing an appropriate record is returned in ret_de_bh.
  456. */
  457. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  458. struct inode *dir,
  459. struct buffer_head *parent_fe_bh,
  460. const char *name,
  461. int namelen,
  462. struct buffer_head **ret_de_bh)
  463. {
  464. unsigned long offset;
  465. struct buffer_head * bh = NULL;
  466. unsigned short rec_len;
  467. struct ocfs2_dinode *fe;
  468. struct ocfs2_dir_entry *de;
  469. struct super_block *sb;
  470. int status;
  471. mlog_entry_void();
  472. mlog(0, "getting ready to insert namelen %d into dir %llu\n",
  473. namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  474. BUG_ON(!S_ISDIR(dir->i_mode));
  475. fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  476. BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
  477. sb = dir->i_sb;
  478. if (!namelen) {
  479. status = -EINVAL;
  480. mlog_errno(status);
  481. goto bail;
  482. }
  483. bh = ocfs2_bread(dir, 0, &status, 0);
  484. if (!bh) {
  485. mlog_errno(status);
  486. goto bail;
  487. }
  488. rec_len = OCFS2_DIR_REC_LEN(namelen);
  489. offset = 0;
  490. de = (struct ocfs2_dir_entry *) bh->b_data;
  491. while (1) {
  492. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  493. brelse(bh);
  494. bh = NULL;
  495. if (i_size_read(dir) <= offset) {
  496. status = ocfs2_extend_dir(osb,
  497. dir,
  498. parent_fe_bh,
  499. &bh);
  500. if (status < 0) {
  501. mlog_errno(status);
  502. goto bail;
  503. }
  504. BUG_ON(!bh);
  505. *ret_de_bh = bh;
  506. get_bh(*ret_de_bh);
  507. goto bail;
  508. }
  509. bh = ocfs2_bread(dir,
  510. offset >> sb->s_blocksize_bits,
  511. &status,
  512. 0);
  513. if (!bh) {
  514. mlog_errno(status);
  515. goto bail;
  516. }
  517. /* move to next block */
  518. de = (struct ocfs2_dir_entry *) bh->b_data;
  519. }
  520. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  521. status = -ENOENT;
  522. goto bail;
  523. }
  524. if (ocfs2_match(namelen, name, de)) {
  525. status = -EEXIST;
  526. goto bail;
  527. }
  528. if (((le64_to_cpu(de->inode) == 0) &&
  529. (le16_to_cpu(de->rec_len) >= rec_len)) ||
  530. (le16_to_cpu(de->rec_len) >=
  531. (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
  532. /* Ok, we found a spot. Return this bh and let
  533. * the caller actually fill it in. */
  534. *ret_de_bh = bh;
  535. get_bh(*ret_de_bh);
  536. status = 0;
  537. goto bail;
  538. }
  539. offset += le16_to_cpu(de->rec_len);
  540. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  541. }
  542. status = 0;
  543. bail:
  544. if (bh)
  545. brelse(bh);
  546. mlog_exit(status);
  547. return status;
  548. }