dir.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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. /*
  76. * bh passed here can be an inode block or a dir data block, depending
  77. * on the inode inline data flag.
  78. */
  79. static int ocfs2_check_dir_entry(struct inode * dir,
  80. struct ocfs2_dir_entry * de,
  81. struct buffer_head * bh,
  82. unsigned long offset)
  83. {
  84. const char *error_msg = NULL;
  85. const int rlen = le16_to_cpu(de->rec_len);
  86. if (rlen < OCFS2_DIR_REC_LEN(1))
  87. error_msg = "rec_len is smaller than minimal";
  88. else if (rlen % 4 != 0)
  89. error_msg = "rec_len % 4 != 0";
  90. else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
  91. error_msg = "rec_len is too small for name_len";
  92. else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
  93. error_msg = "directory entry across blocks";
  94. if (error_msg != NULL)
  95. mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
  96. "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
  97. (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
  98. offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
  99. de->name_len);
  100. return error_msg == NULL ? 1 : 0;
  101. }
  102. static inline int ocfs2_match(int len,
  103. const char * const name,
  104. struct ocfs2_dir_entry *de)
  105. {
  106. if (len != de->name_len)
  107. return 0;
  108. if (!de->inode)
  109. return 0;
  110. return !memcmp(name, de->name, len);
  111. }
  112. /*
  113. * Returns 0 if not found, -1 on failure, and 1 on success
  114. */
  115. static int inline ocfs2_search_dirblock(struct buffer_head *bh,
  116. struct inode *dir,
  117. const char *name, int namelen,
  118. unsigned long offset,
  119. char *first_de,
  120. unsigned int bytes,
  121. struct ocfs2_dir_entry **res_dir)
  122. {
  123. struct ocfs2_dir_entry *de;
  124. char *dlimit, *de_buf;
  125. int de_len;
  126. int ret = 0;
  127. mlog_entry_void();
  128. de_buf = first_de;
  129. dlimit = de_buf + bytes;
  130. while (de_buf < dlimit) {
  131. /* this code is executed quadratically often */
  132. /* do minimal checking `by hand' */
  133. de = (struct ocfs2_dir_entry *) de_buf;
  134. if (de_buf + namelen <= dlimit &&
  135. ocfs2_match(namelen, name, de)) {
  136. /* found a match - just to be sure, do a full check */
  137. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  138. ret = -1;
  139. goto bail;
  140. }
  141. *res_dir = de;
  142. ret = 1;
  143. goto bail;
  144. }
  145. /* prevent looping on a bad block */
  146. de_len = le16_to_cpu(de->rec_len);
  147. if (de_len <= 0) {
  148. ret = -1;
  149. goto bail;
  150. }
  151. de_buf += de_len;
  152. offset += de_len;
  153. }
  154. bail:
  155. mlog_exit(ret);
  156. return ret;
  157. }
  158. static struct buffer_head *ocfs2_find_entry_id(const char *name,
  159. int namelen,
  160. struct inode *dir,
  161. struct ocfs2_dir_entry **res_dir)
  162. {
  163. int ret, found;
  164. struct buffer_head *di_bh = NULL;
  165. struct ocfs2_dinode *di;
  166. struct ocfs2_inline_data *data;
  167. ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
  168. &di_bh, OCFS2_BH_CACHED, dir);
  169. if (ret) {
  170. mlog_errno(ret);
  171. goto out;
  172. }
  173. di = (struct ocfs2_dinode *)di_bh->b_data;
  174. data = &di->id2.i_data;
  175. found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
  176. data->id_data, i_size_read(dir), res_dir);
  177. if (found == 1)
  178. return di_bh;
  179. brelse(di_bh);
  180. out:
  181. return NULL;
  182. }
  183. struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
  184. struct inode *dir,
  185. struct ocfs2_dir_entry **res_dir)
  186. {
  187. struct super_block *sb;
  188. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  189. struct buffer_head *bh, *ret = NULL;
  190. unsigned long start, block, b;
  191. int ra_max = 0; /* Number of bh's in the readahead
  192. buffer, bh_use[] */
  193. int ra_ptr = 0; /* Current index into readahead
  194. buffer */
  195. int num = 0;
  196. int nblocks, i, err;
  197. mlog_entry_void();
  198. sb = dir->i_sb;
  199. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  200. start = OCFS2_I(dir)->ip_dir_start_lookup;
  201. if (start >= nblocks)
  202. start = 0;
  203. block = start;
  204. restart:
  205. do {
  206. /*
  207. * We deal with the read-ahead logic here.
  208. */
  209. if (ra_ptr >= ra_max) {
  210. /* Refill the readahead buffer */
  211. ra_ptr = 0;
  212. b = block;
  213. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  214. /*
  215. * Terminate if we reach the end of the
  216. * directory and must wrap, or if our
  217. * search has finished at this block.
  218. */
  219. if (b >= nblocks || (num && block == start)) {
  220. bh_use[ra_max] = NULL;
  221. break;
  222. }
  223. num++;
  224. bh = ocfs2_bread(dir, b++, &err, 1);
  225. bh_use[ra_max] = bh;
  226. }
  227. }
  228. if ((bh = bh_use[ra_ptr++]) == NULL)
  229. goto next;
  230. wait_on_buffer(bh);
  231. if (!buffer_uptodate(bh)) {
  232. /* read error, skip block & hope for the best */
  233. ocfs2_error(dir->i_sb, "reading directory %llu, "
  234. "offset %lu\n",
  235. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  236. block);
  237. brelse(bh);
  238. goto next;
  239. }
  240. i = ocfs2_search_dirblock(bh, dir, name, namelen,
  241. block << sb->s_blocksize_bits,
  242. bh->b_data, sb->s_blocksize,
  243. res_dir);
  244. if (i == 1) {
  245. OCFS2_I(dir)->ip_dir_start_lookup = block;
  246. ret = bh;
  247. goto cleanup_and_exit;
  248. } else {
  249. brelse(bh);
  250. if (i < 0)
  251. goto cleanup_and_exit;
  252. }
  253. next:
  254. if (++block >= nblocks)
  255. block = 0;
  256. } while (block != start);
  257. /*
  258. * If the directory has grown while we were searching, then
  259. * search the last part of the directory before giving up.
  260. */
  261. block = nblocks;
  262. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  263. if (block < nblocks) {
  264. start = 0;
  265. goto restart;
  266. }
  267. cleanup_and_exit:
  268. /* Clean up the read-ahead blocks */
  269. for (; ra_ptr < ra_max; ra_ptr++)
  270. brelse(bh_use[ra_ptr]);
  271. mlog_exit_ptr(ret);
  272. return ret;
  273. }
  274. /*
  275. * Try to find an entry of the provided name within 'dir'.
  276. *
  277. * If nothing was found, NULL is returned. Otherwise, a buffer_head
  278. * and pointer to the dir entry are passed back.
  279. *
  280. * Caller can NOT assume anything about the contents of the
  281. * buffer_head - it is passed back only so that it can be passed into
  282. * any one of the manipulation functions (add entry, delete entry,
  283. * etc). As an example, bh in the extent directory case is a data
  284. * block, in the inline-data case it actually points to an inode.
  285. */
  286. struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
  287. struct inode *dir,
  288. struct ocfs2_dir_entry **res_dir)
  289. {
  290. *res_dir = NULL;
  291. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  292. return ocfs2_find_entry_id(name, namelen, dir, res_dir);
  293. return ocfs2_find_entry_el(name, namelen, dir, res_dir);
  294. }
  295. int ocfs2_update_entry(struct inode *dir, handle_t *handle,
  296. struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
  297. struct inode *new_entry_inode)
  298. {
  299. int ret;
  300. ret = ocfs2_journal_access(handle, dir, de_bh,
  301. OCFS2_JOURNAL_ACCESS_WRITE);
  302. if (ret) {
  303. mlog_errno(ret);
  304. goto out;
  305. }
  306. de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
  307. ocfs2_set_de_type(de, new_entry_inode->i_mode);
  308. ocfs2_journal_dirty(handle, de_bh);
  309. out:
  310. return ret;
  311. }
  312. /*
  313. * ocfs2_delete_entry deletes a directory entry by merging it with the
  314. * previous entry
  315. */
  316. int ocfs2_delete_entry(handle_t *handle,
  317. struct inode *dir,
  318. struct ocfs2_dir_entry *de_del,
  319. struct buffer_head *bh)
  320. {
  321. struct ocfs2_dir_entry *de, *pde;
  322. int i, status = -ENOENT;
  323. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
  324. i = 0;
  325. pde = NULL;
  326. de = (struct ocfs2_dir_entry *) bh->b_data;
  327. while (i < bh->b_size) {
  328. if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
  329. status = -EIO;
  330. mlog_errno(status);
  331. goto bail;
  332. }
  333. if (de == de_del) {
  334. status = ocfs2_journal_access(handle, dir, bh,
  335. OCFS2_JOURNAL_ACCESS_WRITE);
  336. if (status < 0) {
  337. status = -EIO;
  338. mlog_errno(status);
  339. goto bail;
  340. }
  341. if (pde)
  342. pde->rec_len =
  343. cpu_to_le16(le16_to_cpu(pde->rec_len) +
  344. le16_to_cpu(de->rec_len));
  345. else
  346. de->inode = 0;
  347. dir->i_version++;
  348. status = ocfs2_journal_dirty(handle, bh);
  349. goto bail;
  350. }
  351. i += le16_to_cpu(de->rec_len);
  352. pde = de;
  353. de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
  354. }
  355. bail:
  356. mlog_exit(status);
  357. return status;
  358. }
  359. /*
  360. * Check whether 'de' has enough room to hold an entry of
  361. * 'new_rec_len' bytes.
  362. */
  363. static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
  364. unsigned int new_rec_len)
  365. {
  366. unsigned int de_really_used;
  367. /* Check whether this is an empty record with enough space */
  368. if (le64_to_cpu(de->inode) == 0 &&
  369. le16_to_cpu(de->rec_len) >= new_rec_len)
  370. return 1;
  371. /*
  372. * Record might have free space at the end which we can
  373. * use.
  374. */
  375. de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
  376. if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
  377. return 1;
  378. return 0;
  379. }
  380. /* we don't always have a dentry for what we want to add, so people
  381. * like orphan dir can call this instead.
  382. *
  383. * If you pass me insert_bh, I'll skip the search of the other dir
  384. * blocks and put the record in there.
  385. */
  386. int __ocfs2_add_entry(handle_t *handle,
  387. struct inode *dir,
  388. const char *name, int namelen,
  389. struct inode *inode, u64 blkno,
  390. struct buffer_head *parent_fe_bh,
  391. struct buffer_head *insert_bh)
  392. {
  393. unsigned long offset;
  394. unsigned short rec_len;
  395. struct ocfs2_dir_entry *de, *de1;
  396. struct super_block *sb;
  397. int retval, status;
  398. mlog_entry_void();
  399. sb = dir->i_sb;
  400. if (!namelen)
  401. return -EINVAL;
  402. rec_len = OCFS2_DIR_REC_LEN(namelen);
  403. offset = 0;
  404. de = (struct ocfs2_dir_entry *) insert_bh->b_data;
  405. while (1) {
  406. BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
  407. /* These checks should've already been passed by the
  408. * prepare function, but I guess we can leave them
  409. * here anyway. */
  410. if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
  411. retval = -ENOENT;
  412. goto bail;
  413. }
  414. if (ocfs2_match(namelen, name, de)) {
  415. retval = -EEXIST;
  416. goto bail;
  417. }
  418. if (ocfs2_dirent_would_fit(de, rec_len)) {
  419. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  420. retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  421. if (retval < 0) {
  422. mlog_errno(retval);
  423. goto bail;
  424. }
  425. status = ocfs2_journal_access(handle, dir, insert_bh,
  426. OCFS2_JOURNAL_ACCESS_WRITE);
  427. /* By now the buffer is marked for journaling */
  428. offset += le16_to_cpu(de->rec_len);
  429. if (le64_to_cpu(de->inode)) {
  430. de1 = (struct ocfs2_dir_entry *)((char *) de +
  431. OCFS2_DIR_REC_LEN(de->name_len));
  432. de1->rec_len =
  433. cpu_to_le16(le16_to_cpu(de->rec_len) -
  434. OCFS2_DIR_REC_LEN(de->name_len));
  435. de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  436. de = de1;
  437. }
  438. de->file_type = OCFS2_FT_UNKNOWN;
  439. if (blkno) {
  440. de->inode = cpu_to_le64(blkno);
  441. ocfs2_set_de_type(de, inode->i_mode);
  442. } else
  443. de->inode = 0;
  444. de->name_len = namelen;
  445. memcpy(de->name, name, namelen);
  446. dir->i_version++;
  447. status = ocfs2_journal_dirty(handle, insert_bh);
  448. retval = 0;
  449. goto bail;
  450. }
  451. offset += le16_to_cpu(de->rec_len);
  452. de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
  453. }
  454. /* when you think about it, the assert above should prevent us
  455. * from ever getting here. */
  456. retval = -ENOSPC;
  457. bail:
  458. mlog_exit(retval);
  459. return retval;
  460. }
  461. static int ocfs2_dir_foreach_blk_id(struct inode *inode,
  462. unsigned long *f_version,
  463. loff_t *f_pos, void *priv,
  464. filldir_t filldir)
  465. {
  466. int ret, i, filldir_ret;
  467. unsigned long offset = *f_pos;
  468. struct buffer_head *di_bh = NULL;
  469. struct ocfs2_dinode *di;
  470. struct ocfs2_inline_data *data;
  471. struct ocfs2_dir_entry *de;
  472. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
  473. &di_bh, OCFS2_BH_CACHED, inode);
  474. if (ret) {
  475. mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
  476. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  477. goto out;
  478. }
  479. di = (struct ocfs2_dinode *)di_bh->b_data;
  480. data = &di->id2.i_data;
  481. while (*f_pos < i_size_read(inode)) {
  482. revalidate:
  483. /* If the dir block has changed since the last call to
  484. * readdir(2), then we might be pointing to an invalid
  485. * dirent right now. Scan from the start of the block
  486. * to make sure. */
  487. if (*f_version != inode->i_version) {
  488. for (i = 0; i < i_size_read(inode) && i < offset; ) {
  489. de = (struct ocfs2_dir_entry *)
  490. (data->id_data + i);
  491. /* It's too expensive to do a full
  492. * dirent test each time round this
  493. * loop, but we do have to test at
  494. * least that it is non-zero. A
  495. * failure will be detected in the
  496. * dirent test below. */
  497. if (le16_to_cpu(de->rec_len) <
  498. OCFS2_DIR_REC_LEN(1))
  499. break;
  500. i += le16_to_cpu(de->rec_len);
  501. }
  502. *f_pos = offset = i;
  503. *f_version = inode->i_version;
  504. }
  505. de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
  506. if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
  507. /* On error, skip the f_pos to the end. */
  508. *f_pos = i_size_read(inode);
  509. goto out;
  510. }
  511. offset += le16_to_cpu(de->rec_len);
  512. if (le64_to_cpu(de->inode)) {
  513. /* We might block in the next section
  514. * if the data destination is
  515. * currently swapped out. So, use a
  516. * version stamp to detect whether or
  517. * not the directory has been modified
  518. * during the copy operation.
  519. */
  520. unsigned long version = *f_version;
  521. unsigned char d_type = DT_UNKNOWN;
  522. if (de->file_type < OCFS2_FT_MAX)
  523. d_type = ocfs2_filetype_table[de->file_type];
  524. filldir_ret = filldir(priv, de->name,
  525. de->name_len,
  526. *f_pos,
  527. le64_to_cpu(de->inode),
  528. d_type);
  529. if (filldir_ret)
  530. break;
  531. if (version != *f_version)
  532. goto revalidate;
  533. }
  534. *f_pos += le16_to_cpu(de->rec_len);
  535. }
  536. out:
  537. brelse(di_bh);
  538. return 0;
  539. }
  540. static int ocfs2_dir_foreach_blk_el(struct inode *inode,
  541. unsigned long *f_version,
  542. loff_t *f_pos, void *priv,
  543. filldir_t filldir)
  544. {
  545. int error = 0;
  546. unsigned long offset, blk, last_ra_blk = 0;
  547. int i, stored;
  548. struct buffer_head * bh, * tmp;
  549. struct ocfs2_dir_entry * de;
  550. int err;
  551. struct super_block * sb = inode->i_sb;
  552. unsigned int ra_sectors = 16;
  553. stored = 0;
  554. bh = NULL;
  555. offset = (*f_pos) & (sb->s_blocksize - 1);
  556. while (!error && !stored && *f_pos < i_size_read(inode)) {
  557. blk = (*f_pos) >> sb->s_blocksize_bits;
  558. bh = ocfs2_bread(inode, blk, &err, 0);
  559. if (!bh) {
  560. mlog(ML_ERROR,
  561. "directory #%llu contains a hole at offset %lld\n",
  562. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  563. *f_pos);
  564. *f_pos += sb->s_blocksize - offset;
  565. continue;
  566. }
  567. /* The idea here is to begin with 8k read-ahead and to stay
  568. * 4k ahead of our current position.
  569. *
  570. * TODO: Use the pagecache for this. We just need to
  571. * make sure it's cluster-safe... */
  572. if (!last_ra_blk
  573. || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
  574. for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
  575. i > 0; i--) {
  576. tmp = ocfs2_bread(inode, ++blk, &err, 1);
  577. if (tmp)
  578. brelse(tmp);
  579. }
  580. last_ra_blk = blk;
  581. ra_sectors = 8;
  582. }
  583. revalidate:
  584. /* If the dir block has changed since the last call to
  585. * readdir(2), then we might be pointing to an invalid
  586. * dirent right now. Scan from the start of the block
  587. * to make sure. */
  588. if (*f_version != inode->i_version) {
  589. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  590. de = (struct ocfs2_dir_entry *) (bh->b_data + i);
  591. /* It's too expensive to do a full
  592. * dirent test each time round this
  593. * loop, but we do have to test at
  594. * least that it is non-zero. A
  595. * failure will be detected in the
  596. * dirent test below. */
  597. if (le16_to_cpu(de->rec_len) <
  598. OCFS2_DIR_REC_LEN(1))
  599. break;
  600. i += le16_to_cpu(de->rec_len);
  601. }
  602. offset = i;
  603. *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
  604. | offset;
  605. *f_version = inode->i_version;
  606. }
  607. while (!error && *f_pos < i_size_read(inode)
  608. && offset < sb->s_blocksize) {
  609. de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
  610. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  611. /* On error, skip the f_pos to the
  612. next block. */
  613. *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
  614. brelse(bh);
  615. goto out;
  616. }
  617. offset += le16_to_cpu(de->rec_len);
  618. if (le64_to_cpu(de->inode)) {
  619. /* We might block in the next section
  620. * if the data destination is
  621. * currently swapped out. So, use a
  622. * version stamp to detect whether or
  623. * not the directory has been modified
  624. * during the copy operation.
  625. */
  626. unsigned long version = *f_version;
  627. unsigned char d_type = DT_UNKNOWN;
  628. if (de->file_type < OCFS2_FT_MAX)
  629. d_type = ocfs2_filetype_table[de->file_type];
  630. error = filldir(priv, de->name,
  631. de->name_len,
  632. *f_pos,
  633. le64_to_cpu(de->inode),
  634. d_type);
  635. if (error)
  636. break;
  637. if (version != *f_version)
  638. goto revalidate;
  639. stored ++;
  640. }
  641. *f_pos += le16_to_cpu(de->rec_len);
  642. }
  643. offset = 0;
  644. brelse(bh);
  645. }
  646. stored = 0;
  647. out:
  648. return stored;
  649. }
  650. static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version,
  651. loff_t *f_pos, void *priv, filldir_t filldir)
  652. {
  653. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  654. return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
  655. filldir);
  656. return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir);
  657. }
  658. /*
  659. * This is intended to be called from inside other kernel functions,
  660. * so we fake some arguments.
  661. */
  662. int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
  663. filldir_t filldir)
  664. {
  665. int ret = 0;
  666. unsigned long version = inode->i_version;
  667. while (*f_pos < i_size_read(inode)) {
  668. ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
  669. filldir);
  670. if (ret)
  671. break;
  672. }
  673. return 0;
  674. }
  675. /*
  676. * ocfs2_readdir()
  677. *
  678. */
  679. int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
  680. {
  681. int error = 0;
  682. struct inode *inode = filp->f_path.dentry->d_inode;
  683. int lock_level = 0;
  684. mlog_entry("dirino=%llu\n",
  685. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  686. error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  687. if (lock_level && error >= 0) {
  688. /* We release EX lock which used to update atime
  689. * and get PR lock again to reduce contention
  690. * on commonly accessed directories. */
  691. ocfs2_meta_unlock(inode, 1);
  692. lock_level = 0;
  693. error = ocfs2_meta_lock(inode, NULL, 0);
  694. }
  695. if (error < 0) {
  696. if (error != -ENOENT)
  697. mlog_errno(error);
  698. /* we haven't got any yet, so propagate the error. */
  699. goto bail_nolock;
  700. }
  701. error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
  702. dirent, filldir);
  703. ocfs2_meta_unlock(inode, lock_level);
  704. bail_nolock:
  705. mlog_exit(error);
  706. return error;
  707. }
  708. /*
  709. * NOTE: this should always be called with parent dir i_mutex taken.
  710. */
  711. int ocfs2_find_files_on_disk(const char *name,
  712. int namelen,
  713. u64 *blkno,
  714. struct inode *inode,
  715. struct buffer_head **dirent_bh,
  716. struct ocfs2_dir_entry **dirent)
  717. {
  718. int status = -ENOENT;
  719. mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
  720. namelen, name, blkno, inode, dirent_bh, dirent);
  721. *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
  722. if (!*dirent_bh || !*dirent) {
  723. status = -ENOENT;
  724. goto leave;
  725. }
  726. *blkno = le64_to_cpu((*dirent)->inode);
  727. status = 0;
  728. leave:
  729. if (status < 0) {
  730. *dirent = NULL;
  731. if (*dirent_bh) {
  732. brelse(*dirent_bh);
  733. *dirent_bh = NULL;
  734. }
  735. }
  736. mlog_exit(status);
  737. return status;
  738. }
  739. /*
  740. * Convenience function for callers which just want the block number
  741. * mapped to a name and don't require the full dirent info, etc.
  742. */
  743. int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
  744. int namelen, u64 *blkno)
  745. {
  746. int ret;
  747. struct buffer_head *bh = NULL;
  748. struct ocfs2_dir_entry *dirent = NULL;
  749. ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
  750. brelse(bh);
  751. return ret;
  752. }
  753. /* Check for a name within a directory.
  754. *
  755. * Return 0 if the name does not exist
  756. * Return -EEXIST if the directory contains the name
  757. *
  758. * Callers should have i_mutex + a cluster lock on dir
  759. */
  760. int ocfs2_check_dir_for_entry(struct inode *dir,
  761. const char *name,
  762. int namelen)
  763. {
  764. int ret;
  765. struct buffer_head *dirent_bh = NULL;
  766. struct ocfs2_dir_entry *dirent = NULL;
  767. mlog_entry("dir %llu, name '%.*s'\n",
  768. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  769. ret = -EEXIST;
  770. dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
  771. if (dirent_bh)
  772. goto bail;
  773. ret = 0;
  774. bail:
  775. if (dirent_bh)
  776. brelse(dirent_bh);
  777. mlog_exit(ret);
  778. return ret;
  779. }
  780. struct ocfs2_empty_dir_priv {
  781. unsigned seen_dot;
  782. unsigned seen_dot_dot;
  783. unsigned seen_other;
  784. };
  785. static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
  786. loff_t pos, u64 ino, unsigned type)
  787. {
  788. struct ocfs2_empty_dir_priv *p = priv;
  789. /*
  790. * Check the positions of "." and ".." records to be sure
  791. * they're in the correct place.
  792. */
  793. if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
  794. p->seen_dot = 1;
  795. return 0;
  796. }
  797. if (name_len == 2 && !strncmp("..", name, 2) &&
  798. pos == OCFS2_DIR_REC_LEN(1)) {
  799. p->seen_dot_dot = 1;
  800. return 0;
  801. }
  802. p->seen_other = 1;
  803. return 1;
  804. }
  805. /*
  806. * routine to check that the specified directory is empty (for rmdir)
  807. *
  808. * Returns 1 if dir is empty, zero otherwise.
  809. */
  810. int ocfs2_empty_dir(struct inode *inode)
  811. {
  812. int ret;
  813. loff_t start = 0;
  814. struct ocfs2_empty_dir_priv priv;
  815. memset(&priv, 0, sizeof(priv));
  816. ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
  817. if (ret)
  818. mlog_errno(ret);
  819. if (!priv.seen_dot || !priv.seen_dot_dot) {
  820. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  821. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  822. /*
  823. * XXX: Is it really safe to allow an unlink to continue?
  824. */
  825. return 1;
  826. }
  827. return !priv.seen_other;
  828. }
  829. int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  830. handle_t *handle,
  831. struct inode *parent,
  832. struct inode *inode,
  833. struct buffer_head *fe_bh,
  834. struct ocfs2_alloc_context *data_ac)
  835. {
  836. int status;
  837. struct buffer_head *new_bh = NULL;
  838. struct ocfs2_dir_entry *de = NULL;
  839. mlog_entry_void();
  840. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  841. data_ac, NULL, &new_bh);
  842. if (status < 0) {
  843. mlog_errno(status);
  844. goto bail;
  845. }
  846. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  847. status = ocfs2_journal_access(handle, inode, new_bh,
  848. OCFS2_JOURNAL_ACCESS_CREATE);
  849. if (status < 0) {
  850. mlog_errno(status);
  851. goto bail;
  852. }
  853. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  854. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  855. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  856. de->name_len = 1;
  857. de->rec_len =
  858. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  859. strcpy(de->name, ".");
  860. ocfs2_set_de_type(de, S_IFDIR);
  861. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  862. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  863. de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize -
  864. OCFS2_DIR_REC_LEN(1));
  865. de->name_len = 2;
  866. strcpy(de->name, "..");
  867. ocfs2_set_de_type(de, S_IFDIR);
  868. status = ocfs2_journal_dirty(handle, new_bh);
  869. if (status < 0) {
  870. mlog_errno(status);
  871. goto bail;
  872. }
  873. i_size_write(inode, inode->i_sb->s_blocksize);
  874. inode->i_nlink = 2;
  875. inode->i_blocks = ocfs2_inode_sector_count(inode);
  876. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  877. if (status < 0) {
  878. mlog_errno(status);
  879. goto bail;
  880. }
  881. status = 0;
  882. bail:
  883. if (new_bh)
  884. brelse(new_bh);
  885. mlog_exit(status);
  886. return status;
  887. }
  888. /* returns a bh of the 1st new block in the allocation. */
  889. static int ocfs2_do_extend_dir(struct super_block *sb,
  890. handle_t *handle,
  891. struct inode *dir,
  892. struct buffer_head *parent_fe_bh,
  893. struct ocfs2_alloc_context *data_ac,
  894. struct ocfs2_alloc_context *meta_ac,
  895. struct buffer_head **new_bh)
  896. {
  897. int status;
  898. int extend;
  899. u64 p_blkno, v_blkno;
  900. spin_lock(&OCFS2_I(dir)->ip_lock);
  901. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  902. spin_unlock(&OCFS2_I(dir)->ip_lock);
  903. if (extend) {
  904. u32 offset = OCFS2_I(dir)->ip_clusters;
  905. status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
  906. 1, 0, parent_fe_bh, handle,
  907. data_ac, meta_ac, NULL);
  908. BUG_ON(status == -EAGAIN);
  909. if (status < 0) {
  910. mlog_errno(status);
  911. goto bail;
  912. }
  913. }
  914. v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  915. status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
  916. if (status < 0) {
  917. mlog_errno(status);
  918. goto bail;
  919. }
  920. *new_bh = sb_getblk(sb, p_blkno);
  921. if (!*new_bh) {
  922. status = -EIO;
  923. mlog_errno(status);
  924. goto bail;
  925. }
  926. status = 0;
  927. bail:
  928. mlog_exit(status);
  929. return status;
  930. }
  931. /* assumes you already have a cluster lock on the directory. */
  932. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  933. struct inode *dir,
  934. struct buffer_head *parent_fe_bh,
  935. struct buffer_head **new_de_bh)
  936. {
  937. int status = 0;
  938. int credits, num_free_extents, drop_alloc_sem = 0;
  939. loff_t dir_i_size;
  940. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  941. struct ocfs2_alloc_context *data_ac = NULL;
  942. struct ocfs2_alloc_context *meta_ac = NULL;
  943. handle_t *handle = NULL;
  944. struct buffer_head *new_bh = NULL;
  945. struct ocfs2_dir_entry * de;
  946. struct super_block *sb = osb->sb;
  947. mlog_entry_void();
  948. dir_i_size = i_size_read(dir);
  949. mlog(0, "extending dir %llu (i_size = %lld)\n",
  950. (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
  951. /* dir->i_size is always block aligned. */
  952. spin_lock(&OCFS2_I(dir)->ip_lock);
  953. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  954. spin_unlock(&OCFS2_I(dir)->ip_lock);
  955. num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
  956. if (num_free_extents < 0) {
  957. status = num_free_extents;
  958. mlog_errno(status);
  959. goto bail;
  960. }
  961. if (!num_free_extents) {
  962. status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
  963. if (status < 0) {
  964. if (status != -ENOSPC)
  965. mlog_errno(status);
  966. goto bail;
  967. }
  968. }
  969. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  970. if (status < 0) {
  971. if (status != -ENOSPC)
  972. mlog_errno(status);
  973. goto bail;
  974. }
  975. credits = ocfs2_calc_extend_credits(sb, fe, 1);
  976. } else {
  977. spin_unlock(&OCFS2_I(dir)->ip_lock);
  978. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  979. }
  980. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  981. drop_alloc_sem = 1;
  982. handle = ocfs2_start_trans(osb, credits);
  983. if (IS_ERR(handle)) {
  984. status = PTR_ERR(handle);
  985. handle = NULL;
  986. mlog_errno(status);
  987. goto bail;
  988. }
  989. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  990. data_ac, meta_ac, &new_bh);
  991. if (status < 0) {
  992. mlog_errno(status);
  993. goto bail;
  994. }
  995. ocfs2_set_new_buffer_uptodate(dir, new_bh);
  996. status = ocfs2_journal_access(handle, dir, new_bh,
  997. OCFS2_JOURNAL_ACCESS_CREATE);
  998. if (status < 0) {
  999. mlog_errno(status);
  1000. goto bail;
  1001. }
  1002. memset(new_bh->b_data, 0, sb->s_blocksize);
  1003. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  1004. de->inode = 0;
  1005. de->rec_len = cpu_to_le16(sb->s_blocksize);
  1006. status = ocfs2_journal_dirty(handle, new_bh);
  1007. if (status < 0) {
  1008. mlog_errno(status);
  1009. goto bail;
  1010. }
  1011. dir_i_size += dir->i_sb->s_blocksize;
  1012. i_size_write(dir, dir_i_size);
  1013. dir->i_blocks = ocfs2_inode_sector_count(dir);
  1014. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  1015. if (status < 0) {
  1016. mlog_errno(status);
  1017. goto bail;
  1018. }
  1019. *new_de_bh = new_bh;
  1020. get_bh(*new_de_bh);
  1021. bail:
  1022. if (drop_alloc_sem)
  1023. up_write(&OCFS2_I(dir)->ip_alloc_sem);
  1024. if (handle)
  1025. ocfs2_commit_trans(osb, handle);
  1026. if (data_ac)
  1027. ocfs2_free_alloc_context(data_ac);
  1028. if (meta_ac)
  1029. ocfs2_free_alloc_context(meta_ac);
  1030. if (new_bh)
  1031. brelse(new_bh);
  1032. mlog_exit(status);
  1033. return status;
  1034. }
  1035. /*
  1036. * Search the dir for a good spot, extending it if necessary. The
  1037. * block containing an appropriate record is returned in ret_de_bh.
  1038. */
  1039. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  1040. struct inode *dir,
  1041. struct buffer_head *parent_fe_bh,
  1042. const char *name,
  1043. int namelen,
  1044. struct buffer_head **ret_de_bh)
  1045. {
  1046. unsigned long offset;
  1047. struct buffer_head * bh = NULL;
  1048. unsigned short rec_len;
  1049. struct ocfs2_dinode *fe;
  1050. struct ocfs2_dir_entry *de;
  1051. struct super_block *sb;
  1052. int status;
  1053. mlog_entry_void();
  1054. mlog(0, "getting ready to insert namelen %d into dir %llu\n",
  1055. namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  1056. BUG_ON(!S_ISDIR(dir->i_mode));
  1057. fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1058. BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir));
  1059. sb = dir->i_sb;
  1060. if (!namelen) {
  1061. status = -EINVAL;
  1062. mlog_errno(status);
  1063. goto bail;
  1064. }
  1065. bh = ocfs2_bread(dir, 0, &status, 0);
  1066. if (!bh) {
  1067. mlog_errno(status);
  1068. goto bail;
  1069. }
  1070. rec_len = OCFS2_DIR_REC_LEN(namelen);
  1071. offset = 0;
  1072. de = (struct ocfs2_dir_entry *) bh->b_data;
  1073. while (1) {
  1074. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  1075. brelse(bh);
  1076. bh = NULL;
  1077. if (i_size_read(dir) <= offset) {
  1078. status = ocfs2_extend_dir(osb,
  1079. dir,
  1080. parent_fe_bh,
  1081. &bh);
  1082. if (status < 0) {
  1083. mlog_errno(status);
  1084. goto bail;
  1085. }
  1086. BUG_ON(!bh);
  1087. *ret_de_bh = bh;
  1088. get_bh(*ret_de_bh);
  1089. goto bail;
  1090. }
  1091. bh = ocfs2_bread(dir,
  1092. offset >> sb->s_blocksize_bits,
  1093. &status,
  1094. 0);
  1095. if (!bh) {
  1096. mlog_errno(status);
  1097. goto bail;
  1098. }
  1099. /* move to next block */
  1100. de = (struct ocfs2_dir_entry *) bh->b_data;
  1101. }
  1102. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  1103. status = -ENOENT;
  1104. goto bail;
  1105. }
  1106. if (ocfs2_match(namelen, name, de)) {
  1107. status = -EEXIST;
  1108. goto bail;
  1109. }
  1110. if (ocfs2_dirent_would_fit(de, rec_len)) {
  1111. /* Ok, we found a spot. Return this bh and let
  1112. * the caller actually fill it in. */
  1113. *ret_de_bh = bh;
  1114. get_bh(*ret_de_bh);
  1115. status = 0;
  1116. goto bail;
  1117. }
  1118. offset += le16_to_cpu(de->rec_len);
  1119. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  1120. }
  1121. status = 0;
  1122. bail:
  1123. if (bh)
  1124. brelse(bh);
  1125. mlog_exit(status);
  1126. return status;
  1127. }