dir.c 26 KB

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