dir.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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. unsigned int blocks_wanted,
  68. struct buffer_head **new_de_bh);
  69. static int ocfs2_do_extend_dir(struct super_block *sb,
  70. handle_t *handle,
  71. struct inode *dir,
  72. struct buffer_head *parent_fe_bh,
  73. struct ocfs2_alloc_context *data_ac,
  74. struct ocfs2_alloc_context *meta_ac,
  75. struct buffer_head **new_bh);
  76. /*
  77. * bh passed here can be an inode block or a dir data block, depending
  78. * on the inode inline data flag.
  79. */
  80. static int ocfs2_check_dir_entry(struct inode * dir,
  81. struct ocfs2_dir_entry * de,
  82. struct buffer_head * bh,
  83. unsigned long offset)
  84. {
  85. const char *error_msg = NULL;
  86. const int rlen = le16_to_cpu(de->rec_len);
  87. if (rlen < OCFS2_DIR_REC_LEN(1))
  88. error_msg = "rec_len is smaller than minimal";
  89. else if (rlen % 4 != 0)
  90. error_msg = "rec_len % 4 != 0";
  91. else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
  92. error_msg = "rec_len is too small for name_len";
  93. else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
  94. error_msg = "directory entry across blocks";
  95. if (error_msg != NULL)
  96. mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
  97. "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
  98. (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
  99. offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
  100. de->name_len);
  101. return error_msg == NULL ? 1 : 0;
  102. }
  103. static inline int ocfs2_match(int len,
  104. const char * const name,
  105. struct ocfs2_dir_entry *de)
  106. {
  107. if (len != de->name_len)
  108. return 0;
  109. if (!de->inode)
  110. return 0;
  111. return !memcmp(name, de->name, len);
  112. }
  113. /*
  114. * Returns 0 if not found, -1 on failure, and 1 on success
  115. */
  116. static int inline ocfs2_search_dirblock(struct buffer_head *bh,
  117. struct inode *dir,
  118. const char *name, int namelen,
  119. unsigned long offset,
  120. char *first_de,
  121. unsigned int bytes,
  122. struct ocfs2_dir_entry **res_dir)
  123. {
  124. struct ocfs2_dir_entry *de;
  125. char *dlimit, *de_buf;
  126. int de_len;
  127. int ret = 0;
  128. mlog_entry_void();
  129. de_buf = first_de;
  130. dlimit = de_buf + bytes;
  131. while (de_buf < dlimit) {
  132. /* this code is executed quadratically often */
  133. /* do minimal checking `by hand' */
  134. de = (struct ocfs2_dir_entry *) de_buf;
  135. if (de_buf + namelen <= dlimit &&
  136. ocfs2_match(namelen, name, de)) {
  137. /* found a match - just to be sure, do a full check */
  138. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  139. ret = -1;
  140. goto bail;
  141. }
  142. *res_dir = de;
  143. ret = 1;
  144. goto bail;
  145. }
  146. /* prevent looping on a bad block */
  147. de_len = le16_to_cpu(de->rec_len);
  148. if (de_len <= 0) {
  149. ret = -1;
  150. goto bail;
  151. }
  152. de_buf += de_len;
  153. offset += de_len;
  154. }
  155. bail:
  156. mlog_exit(ret);
  157. return ret;
  158. }
  159. static struct buffer_head *ocfs2_find_entry_id(const char *name,
  160. int namelen,
  161. struct inode *dir,
  162. struct ocfs2_dir_entry **res_dir)
  163. {
  164. int ret, found;
  165. struct buffer_head *di_bh = NULL;
  166. struct ocfs2_dinode *di;
  167. struct ocfs2_inline_data *data;
  168. ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
  169. &di_bh, OCFS2_BH_CACHED, dir);
  170. if (ret) {
  171. mlog_errno(ret);
  172. goto out;
  173. }
  174. di = (struct ocfs2_dinode *)di_bh->b_data;
  175. data = &di->id2.i_data;
  176. found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
  177. data->id_data, i_size_read(dir), res_dir);
  178. if (found == 1)
  179. return di_bh;
  180. brelse(di_bh);
  181. out:
  182. return NULL;
  183. }
  184. struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
  185. struct inode *dir,
  186. struct ocfs2_dir_entry **res_dir)
  187. {
  188. struct super_block *sb;
  189. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  190. struct buffer_head *bh, *ret = NULL;
  191. unsigned long start, block, b;
  192. int ra_max = 0; /* Number of bh's in the readahead
  193. buffer, bh_use[] */
  194. int ra_ptr = 0; /* Current index into readahead
  195. buffer */
  196. int num = 0;
  197. int nblocks, i, err;
  198. mlog_entry_void();
  199. sb = dir->i_sb;
  200. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  201. start = OCFS2_I(dir)->ip_dir_start_lookup;
  202. if (start >= nblocks)
  203. start = 0;
  204. block = start;
  205. restart:
  206. do {
  207. /*
  208. * We deal with the read-ahead logic here.
  209. */
  210. if (ra_ptr >= ra_max) {
  211. /* Refill the readahead buffer */
  212. ra_ptr = 0;
  213. b = block;
  214. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  215. /*
  216. * Terminate if we reach the end of the
  217. * directory and must wrap, or if our
  218. * search has finished at this block.
  219. */
  220. if (b >= nblocks || (num && block == start)) {
  221. bh_use[ra_max] = NULL;
  222. break;
  223. }
  224. num++;
  225. bh = ocfs2_bread(dir, b++, &err, 1);
  226. bh_use[ra_max] = bh;
  227. }
  228. }
  229. if ((bh = bh_use[ra_ptr++]) == NULL)
  230. goto next;
  231. wait_on_buffer(bh);
  232. if (!buffer_uptodate(bh)) {
  233. /* read error, skip block & hope for the best */
  234. ocfs2_error(dir->i_sb, "reading directory %llu, "
  235. "offset %lu\n",
  236. (unsigned long long)OCFS2_I(dir)->ip_blkno,
  237. block);
  238. brelse(bh);
  239. goto next;
  240. }
  241. i = ocfs2_search_dirblock(bh, dir, name, namelen,
  242. block << sb->s_blocksize_bits,
  243. bh->b_data, sb->s_blocksize,
  244. res_dir);
  245. if (i == 1) {
  246. OCFS2_I(dir)->ip_dir_start_lookup = block;
  247. ret = bh;
  248. goto cleanup_and_exit;
  249. } else {
  250. brelse(bh);
  251. if (i < 0)
  252. goto cleanup_and_exit;
  253. }
  254. next:
  255. if (++block >= nblocks)
  256. block = 0;
  257. } while (block != start);
  258. /*
  259. * If the directory has grown while we were searching, then
  260. * search the last part of the directory before giving up.
  261. */
  262. block = nblocks;
  263. nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  264. if (block < nblocks) {
  265. start = 0;
  266. goto restart;
  267. }
  268. cleanup_and_exit:
  269. /* Clean up the read-ahead blocks */
  270. for (; ra_ptr < ra_max; ra_ptr++)
  271. brelse(bh_use[ra_ptr]);
  272. mlog_exit_ptr(ret);
  273. return ret;
  274. }
  275. /*
  276. * Try to find an entry of the provided name within 'dir'.
  277. *
  278. * If nothing was found, NULL is returned. Otherwise, a buffer_head
  279. * and pointer to the dir entry are passed back.
  280. *
  281. * Caller can NOT assume anything about the contents of the
  282. * buffer_head - it is passed back only so that it can be passed into
  283. * any one of the manipulation functions (add entry, delete entry,
  284. * etc). As an example, bh in the extent directory case is a data
  285. * block, in the inline-data case it actually points to an inode.
  286. */
  287. struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
  288. struct inode *dir,
  289. struct ocfs2_dir_entry **res_dir)
  290. {
  291. *res_dir = NULL;
  292. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  293. return ocfs2_find_entry_id(name, namelen, dir, res_dir);
  294. return ocfs2_find_entry_el(name, namelen, dir, res_dir);
  295. }
  296. /*
  297. * Update inode number and type of a previously found directory entry.
  298. */
  299. int ocfs2_update_entry(struct inode *dir, handle_t *handle,
  300. struct buffer_head *de_bh, struct ocfs2_dir_entry *de,
  301. struct inode *new_entry_inode)
  302. {
  303. int ret;
  304. /*
  305. * The same code works fine for both inline-data and extent
  306. * based directories, so no need to split this up.
  307. */
  308. ret = ocfs2_journal_access(handle, dir, de_bh,
  309. OCFS2_JOURNAL_ACCESS_WRITE);
  310. if (ret) {
  311. mlog_errno(ret);
  312. goto out;
  313. }
  314. de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
  315. ocfs2_set_de_type(de, new_entry_inode->i_mode);
  316. ocfs2_journal_dirty(handle, de_bh);
  317. out:
  318. return ret;
  319. }
  320. static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
  321. struct ocfs2_dir_entry *de_del,
  322. struct buffer_head *bh, char *first_de,
  323. unsigned int bytes)
  324. {
  325. struct ocfs2_dir_entry *de, *pde;
  326. int i, status = -ENOENT;
  327. mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
  328. i = 0;
  329. pde = NULL;
  330. de = (struct ocfs2_dir_entry *) first_de;
  331. while (i < bytes) {
  332. if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
  333. status = -EIO;
  334. mlog_errno(status);
  335. goto bail;
  336. }
  337. if (de == de_del) {
  338. status = ocfs2_journal_access(handle, dir, bh,
  339. OCFS2_JOURNAL_ACCESS_WRITE);
  340. if (status < 0) {
  341. status = -EIO;
  342. mlog_errno(status);
  343. goto bail;
  344. }
  345. if (pde)
  346. pde->rec_len =
  347. cpu_to_le16(le16_to_cpu(pde->rec_len) +
  348. le16_to_cpu(de->rec_len));
  349. else
  350. de->inode = 0;
  351. dir->i_version++;
  352. status = ocfs2_journal_dirty(handle, bh);
  353. goto bail;
  354. }
  355. i += le16_to_cpu(de->rec_len);
  356. pde = de;
  357. de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
  358. }
  359. bail:
  360. mlog_exit(status);
  361. return status;
  362. }
  363. static inline int ocfs2_delete_entry_id(handle_t *handle,
  364. struct inode *dir,
  365. struct ocfs2_dir_entry *de_del,
  366. struct buffer_head *bh)
  367. {
  368. int ret;
  369. struct buffer_head *di_bh = NULL;
  370. struct ocfs2_dinode *di;
  371. struct ocfs2_inline_data *data;
  372. ret = ocfs2_read_block(OCFS2_SB(dir->i_sb), OCFS2_I(dir)->ip_blkno,
  373. &di_bh, OCFS2_BH_CACHED, dir);
  374. if (ret) {
  375. mlog_errno(ret);
  376. goto out;
  377. }
  378. di = (struct ocfs2_dinode *)di_bh->b_data;
  379. data = &di->id2.i_data;
  380. ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
  381. i_size_read(dir));
  382. brelse(di_bh);
  383. out:
  384. return ret;
  385. }
  386. static inline int ocfs2_delete_entry_el(handle_t *handle,
  387. struct inode *dir,
  388. struct ocfs2_dir_entry *de_del,
  389. struct buffer_head *bh)
  390. {
  391. return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
  392. bh->b_size);
  393. }
  394. /*
  395. * ocfs2_delete_entry deletes a directory entry by merging it with the
  396. * previous entry
  397. */
  398. int ocfs2_delete_entry(handle_t *handle,
  399. struct inode *dir,
  400. struct ocfs2_dir_entry *de_del,
  401. struct buffer_head *bh)
  402. {
  403. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  404. return ocfs2_delete_entry_id(handle, dir, de_del, bh);
  405. return ocfs2_delete_entry_el(handle, dir, de_del, bh);
  406. }
  407. /*
  408. * Check whether 'de' has enough room to hold an entry of
  409. * 'new_rec_len' bytes.
  410. */
  411. static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
  412. unsigned int new_rec_len)
  413. {
  414. unsigned int de_really_used;
  415. /* Check whether this is an empty record with enough space */
  416. if (le64_to_cpu(de->inode) == 0 &&
  417. le16_to_cpu(de->rec_len) >= new_rec_len)
  418. return 1;
  419. /*
  420. * Record might have free space at the end which we can
  421. * use.
  422. */
  423. de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
  424. if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
  425. return 1;
  426. return 0;
  427. }
  428. /* we don't always have a dentry for what we want to add, so people
  429. * like orphan dir can call this instead.
  430. *
  431. * If you pass me insert_bh, I'll skip the search of the other dir
  432. * blocks and put the record in there.
  433. */
  434. int __ocfs2_add_entry(handle_t *handle,
  435. struct inode *dir,
  436. const char *name, int namelen,
  437. struct inode *inode, u64 blkno,
  438. struct buffer_head *parent_fe_bh,
  439. struct buffer_head *insert_bh)
  440. {
  441. unsigned long offset;
  442. unsigned short rec_len;
  443. struct ocfs2_dir_entry *de, *de1;
  444. struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
  445. struct super_block *sb = dir->i_sb;
  446. int retval, status;
  447. unsigned int size = sb->s_blocksize;
  448. char *data_start = insert_bh->b_data;
  449. mlog_entry_void();
  450. if (!namelen)
  451. return -EINVAL;
  452. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  453. data_start = di->id2.i_data.id_data;
  454. size = i_size_read(dir);
  455. BUG_ON(insert_bh != parent_fe_bh);
  456. }
  457. rec_len = OCFS2_DIR_REC_LEN(namelen);
  458. offset = 0;
  459. de = (struct ocfs2_dir_entry *) data_start;
  460. while (1) {
  461. BUG_ON((char *)de >= (size + data_start));
  462. /* These checks should've already been passed by the
  463. * prepare function, but I guess we can leave them
  464. * here anyway. */
  465. if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
  466. retval = -ENOENT;
  467. goto bail;
  468. }
  469. if (ocfs2_match(namelen, name, de)) {
  470. retval = -EEXIST;
  471. goto bail;
  472. }
  473. if (ocfs2_dirent_would_fit(de, rec_len)) {
  474. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  475. retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  476. if (retval < 0) {
  477. mlog_errno(retval);
  478. goto bail;
  479. }
  480. status = ocfs2_journal_access(handle, dir, insert_bh,
  481. OCFS2_JOURNAL_ACCESS_WRITE);
  482. /* By now the buffer is marked for journaling */
  483. offset += le16_to_cpu(de->rec_len);
  484. if (le64_to_cpu(de->inode)) {
  485. de1 = (struct ocfs2_dir_entry *)((char *) de +
  486. OCFS2_DIR_REC_LEN(de->name_len));
  487. de1->rec_len =
  488. cpu_to_le16(le16_to_cpu(de->rec_len) -
  489. OCFS2_DIR_REC_LEN(de->name_len));
  490. de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  491. de = de1;
  492. }
  493. de->file_type = OCFS2_FT_UNKNOWN;
  494. if (blkno) {
  495. de->inode = cpu_to_le64(blkno);
  496. ocfs2_set_de_type(de, inode->i_mode);
  497. } else
  498. de->inode = 0;
  499. de->name_len = namelen;
  500. memcpy(de->name, name, namelen);
  501. dir->i_version++;
  502. status = ocfs2_journal_dirty(handle, insert_bh);
  503. retval = 0;
  504. goto bail;
  505. }
  506. offset += le16_to_cpu(de->rec_len);
  507. de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
  508. }
  509. /* when you think about it, the assert above should prevent us
  510. * from ever getting here. */
  511. retval = -ENOSPC;
  512. bail:
  513. mlog_exit(retval);
  514. return retval;
  515. }
  516. static int ocfs2_dir_foreach_blk_id(struct inode *inode,
  517. u64 *f_version,
  518. loff_t *f_pos, void *priv,
  519. filldir_t filldir, int *filldir_err)
  520. {
  521. int ret, i, filldir_ret;
  522. unsigned long offset = *f_pos;
  523. struct buffer_head *di_bh = NULL;
  524. struct ocfs2_dinode *di;
  525. struct ocfs2_inline_data *data;
  526. struct ocfs2_dir_entry *de;
  527. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), OCFS2_I(inode)->ip_blkno,
  528. &di_bh, OCFS2_BH_CACHED, inode);
  529. if (ret) {
  530. mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
  531. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  532. goto out;
  533. }
  534. di = (struct ocfs2_dinode *)di_bh->b_data;
  535. data = &di->id2.i_data;
  536. while (*f_pos < i_size_read(inode)) {
  537. revalidate:
  538. /* If the dir block has changed since the last call to
  539. * readdir(2), then we might be pointing to an invalid
  540. * dirent right now. Scan from the start of the block
  541. * to make sure. */
  542. if (*f_version != inode->i_version) {
  543. for (i = 0; i < i_size_read(inode) && i < offset; ) {
  544. de = (struct ocfs2_dir_entry *)
  545. (data->id_data + i);
  546. /* It's too expensive to do a full
  547. * dirent test each time round this
  548. * loop, but we do have to test at
  549. * least that it is non-zero. A
  550. * failure will be detected in the
  551. * dirent test below. */
  552. if (le16_to_cpu(de->rec_len) <
  553. OCFS2_DIR_REC_LEN(1))
  554. break;
  555. i += le16_to_cpu(de->rec_len);
  556. }
  557. *f_pos = offset = i;
  558. *f_version = inode->i_version;
  559. }
  560. de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
  561. if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
  562. /* On error, skip the f_pos to the end. */
  563. *f_pos = i_size_read(inode);
  564. goto out;
  565. }
  566. offset += le16_to_cpu(de->rec_len);
  567. if (le64_to_cpu(de->inode)) {
  568. /* We might block in the next section
  569. * if the data destination is
  570. * currently swapped out. So, use a
  571. * version stamp to detect whether or
  572. * not the directory has been modified
  573. * during the copy operation.
  574. */
  575. u64 version = *f_version;
  576. unsigned char d_type = DT_UNKNOWN;
  577. if (de->file_type < OCFS2_FT_MAX)
  578. d_type = ocfs2_filetype_table[de->file_type];
  579. filldir_ret = filldir(priv, de->name,
  580. de->name_len,
  581. *f_pos,
  582. le64_to_cpu(de->inode),
  583. d_type);
  584. if (filldir_ret) {
  585. if (filldir_err)
  586. *filldir_err = filldir_ret;
  587. break;
  588. }
  589. if (version != *f_version)
  590. goto revalidate;
  591. }
  592. *f_pos += le16_to_cpu(de->rec_len);
  593. }
  594. out:
  595. brelse(di_bh);
  596. return 0;
  597. }
  598. static int ocfs2_dir_foreach_blk_el(struct inode *inode,
  599. u64 *f_version,
  600. loff_t *f_pos, void *priv,
  601. filldir_t filldir, int *filldir_err)
  602. {
  603. int error = 0;
  604. unsigned long offset, blk, last_ra_blk = 0;
  605. int i, stored;
  606. struct buffer_head * bh, * tmp;
  607. struct ocfs2_dir_entry * de;
  608. int err;
  609. struct super_block * sb = inode->i_sb;
  610. unsigned int ra_sectors = 16;
  611. stored = 0;
  612. bh = NULL;
  613. offset = (*f_pos) & (sb->s_blocksize - 1);
  614. while (!error && !stored && *f_pos < i_size_read(inode)) {
  615. blk = (*f_pos) >> sb->s_blocksize_bits;
  616. bh = ocfs2_bread(inode, blk, &err, 0);
  617. if (!bh) {
  618. mlog(ML_ERROR,
  619. "directory #%llu contains a hole at offset %lld\n",
  620. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  621. *f_pos);
  622. *f_pos += sb->s_blocksize - offset;
  623. continue;
  624. }
  625. /* The idea here is to begin with 8k read-ahead and to stay
  626. * 4k ahead of our current position.
  627. *
  628. * TODO: Use the pagecache for this. We just need to
  629. * make sure it's cluster-safe... */
  630. if (!last_ra_blk
  631. || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
  632. for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
  633. i > 0; i--) {
  634. tmp = ocfs2_bread(inode, ++blk, &err, 1);
  635. if (tmp)
  636. brelse(tmp);
  637. }
  638. last_ra_blk = blk;
  639. ra_sectors = 8;
  640. }
  641. revalidate:
  642. /* If the dir block has changed since the last call to
  643. * readdir(2), then we might be pointing to an invalid
  644. * dirent right now. Scan from the start of the block
  645. * to make sure. */
  646. if (*f_version != inode->i_version) {
  647. for (i = 0; i < sb->s_blocksize && i < offset; ) {
  648. de = (struct ocfs2_dir_entry *) (bh->b_data + i);
  649. /* It's too expensive to do a full
  650. * dirent test each time round this
  651. * loop, but we do have to test at
  652. * least that it is non-zero. A
  653. * failure will be detected in the
  654. * dirent test below. */
  655. if (le16_to_cpu(de->rec_len) <
  656. OCFS2_DIR_REC_LEN(1))
  657. break;
  658. i += le16_to_cpu(de->rec_len);
  659. }
  660. offset = i;
  661. *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
  662. | offset;
  663. *f_version = inode->i_version;
  664. }
  665. while (!error && *f_pos < i_size_read(inode)
  666. && offset < sb->s_blocksize) {
  667. de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
  668. if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  669. /* On error, skip the f_pos to the
  670. next block. */
  671. *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
  672. brelse(bh);
  673. goto out;
  674. }
  675. offset += le16_to_cpu(de->rec_len);
  676. if (le64_to_cpu(de->inode)) {
  677. /* We might block in the next section
  678. * if the data destination is
  679. * currently swapped out. So, use a
  680. * version stamp to detect whether or
  681. * not the directory has been modified
  682. * during the copy operation.
  683. */
  684. unsigned long version = *f_version;
  685. unsigned char d_type = DT_UNKNOWN;
  686. if (de->file_type < OCFS2_FT_MAX)
  687. d_type = ocfs2_filetype_table[de->file_type];
  688. error = filldir(priv, de->name,
  689. de->name_len,
  690. *f_pos,
  691. le64_to_cpu(de->inode),
  692. d_type);
  693. if (error) {
  694. if (filldir_err)
  695. *filldir_err = error;
  696. break;
  697. }
  698. if (version != *f_version)
  699. goto revalidate;
  700. stored ++;
  701. }
  702. *f_pos += le16_to_cpu(de->rec_len);
  703. }
  704. offset = 0;
  705. brelse(bh);
  706. }
  707. stored = 0;
  708. out:
  709. return stored;
  710. }
  711. static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
  712. loff_t *f_pos, void *priv, filldir_t filldir,
  713. int *filldir_err)
  714. {
  715. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  716. return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
  717. filldir, filldir_err);
  718. return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
  719. filldir_err);
  720. }
  721. /*
  722. * This is intended to be called from inside other kernel functions,
  723. * so we fake some arguments.
  724. */
  725. int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
  726. filldir_t filldir)
  727. {
  728. int ret = 0, filldir_err = 0;
  729. u64 version = inode->i_version;
  730. while (*f_pos < i_size_read(inode)) {
  731. ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
  732. filldir, &filldir_err);
  733. if (ret || filldir_err)
  734. break;
  735. }
  736. if (ret > 0)
  737. ret = -EIO;
  738. return 0;
  739. }
  740. /*
  741. * ocfs2_readdir()
  742. *
  743. */
  744. int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
  745. {
  746. int error = 0;
  747. struct inode *inode = filp->f_path.dentry->d_inode;
  748. int lock_level = 0;
  749. mlog_entry("dirino=%llu\n",
  750. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  751. error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  752. if (lock_level && error >= 0) {
  753. /* We release EX lock which used to update atime
  754. * and get PR lock again to reduce contention
  755. * on commonly accessed directories. */
  756. ocfs2_meta_unlock(inode, 1);
  757. lock_level = 0;
  758. error = ocfs2_meta_lock(inode, NULL, 0);
  759. }
  760. if (error < 0) {
  761. if (error != -ENOENT)
  762. mlog_errno(error);
  763. /* we haven't got any yet, so propagate the error. */
  764. goto bail_nolock;
  765. }
  766. error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
  767. dirent, filldir, NULL);
  768. ocfs2_meta_unlock(inode, lock_level);
  769. bail_nolock:
  770. mlog_exit(error);
  771. return error;
  772. }
  773. /*
  774. * NOTE: this should always be called with parent dir i_mutex taken.
  775. */
  776. int ocfs2_find_files_on_disk(const char *name,
  777. int namelen,
  778. u64 *blkno,
  779. struct inode *inode,
  780. struct buffer_head **dirent_bh,
  781. struct ocfs2_dir_entry **dirent)
  782. {
  783. int status = -ENOENT;
  784. mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n",
  785. namelen, name, blkno, inode, dirent_bh, dirent);
  786. *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent);
  787. if (!*dirent_bh || !*dirent) {
  788. status = -ENOENT;
  789. goto leave;
  790. }
  791. *blkno = le64_to_cpu((*dirent)->inode);
  792. status = 0;
  793. leave:
  794. if (status < 0) {
  795. *dirent = NULL;
  796. if (*dirent_bh) {
  797. brelse(*dirent_bh);
  798. *dirent_bh = NULL;
  799. }
  800. }
  801. mlog_exit(status);
  802. return status;
  803. }
  804. /*
  805. * Convenience function for callers which just want the block number
  806. * mapped to a name and don't require the full dirent info, etc.
  807. */
  808. int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
  809. int namelen, u64 *blkno)
  810. {
  811. int ret;
  812. struct buffer_head *bh = NULL;
  813. struct ocfs2_dir_entry *dirent = NULL;
  814. ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent);
  815. brelse(bh);
  816. return ret;
  817. }
  818. /* Check for a name within a directory.
  819. *
  820. * Return 0 if the name does not exist
  821. * Return -EEXIST if the directory contains the name
  822. *
  823. * Callers should have i_mutex + a cluster lock on dir
  824. */
  825. int ocfs2_check_dir_for_entry(struct inode *dir,
  826. const char *name,
  827. int namelen)
  828. {
  829. int ret;
  830. struct buffer_head *dirent_bh = NULL;
  831. struct ocfs2_dir_entry *dirent = NULL;
  832. mlog_entry("dir %llu, name '%.*s'\n",
  833. (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
  834. ret = -EEXIST;
  835. dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent);
  836. if (dirent_bh)
  837. goto bail;
  838. ret = 0;
  839. bail:
  840. if (dirent_bh)
  841. brelse(dirent_bh);
  842. mlog_exit(ret);
  843. return ret;
  844. }
  845. struct ocfs2_empty_dir_priv {
  846. unsigned seen_dot;
  847. unsigned seen_dot_dot;
  848. unsigned seen_other;
  849. };
  850. static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
  851. loff_t pos, u64 ino, unsigned type)
  852. {
  853. struct ocfs2_empty_dir_priv *p = priv;
  854. /*
  855. * Check the positions of "." and ".." records to be sure
  856. * they're in the correct place.
  857. */
  858. if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
  859. p->seen_dot = 1;
  860. return 0;
  861. }
  862. if (name_len == 2 && !strncmp("..", name, 2) &&
  863. pos == OCFS2_DIR_REC_LEN(1)) {
  864. p->seen_dot_dot = 1;
  865. return 0;
  866. }
  867. p->seen_other = 1;
  868. return 1;
  869. }
  870. /*
  871. * routine to check that the specified directory is empty (for rmdir)
  872. *
  873. * Returns 1 if dir is empty, zero otherwise.
  874. */
  875. int ocfs2_empty_dir(struct inode *inode)
  876. {
  877. int ret;
  878. loff_t start = 0;
  879. struct ocfs2_empty_dir_priv priv;
  880. memset(&priv, 0, sizeof(priv));
  881. ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
  882. if (ret)
  883. mlog_errno(ret);
  884. if (!priv.seen_dot || !priv.seen_dot_dot) {
  885. mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
  886. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  887. /*
  888. * XXX: Is it really safe to allow an unlink to continue?
  889. */
  890. return 1;
  891. }
  892. return !priv.seen_other;
  893. }
  894. static void ocfs2_fill_initial_dirents(struct inode *inode,
  895. struct inode *parent,
  896. char *start, unsigned int size)
  897. {
  898. struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
  899. de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  900. de->name_len = 1;
  901. de->rec_len =
  902. cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  903. strcpy(de->name, ".");
  904. ocfs2_set_de_type(de, S_IFDIR);
  905. de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  906. de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  907. de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
  908. de->name_len = 2;
  909. strcpy(de->name, "..");
  910. ocfs2_set_de_type(de, S_IFDIR);
  911. }
  912. /*
  913. * This works together with code in ocfs2_mknod_locked() which sets
  914. * the inline-data flag and initializes the inline-data section.
  915. */
  916. static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
  917. handle_t *handle,
  918. struct inode *parent,
  919. struct inode *inode,
  920. struct buffer_head *di_bh)
  921. {
  922. int ret;
  923. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  924. struct ocfs2_inline_data *data = &di->id2.i_data;
  925. unsigned int size = le16_to_cpu(data->id_count);
  926. ret = ocfs2_journal_access(handle, inode, di_bh,
  927. OCFS2_JOURNAL_ACCESS_WRITE);
  928. if (ret) {
  929. mlog_errno(ret);
  930. goto out;
  931. }
  932. ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
  933. ocfs2_journal_dirty(handle, di_bh);
  934. if (ret) {
  935. mlog_errno(ret);
  936. goto out;
  937. }
  938. i_size_write(inode, size);
  939. inode->i_nlink = 2;
  940. inode->i_blocks = ocfs2_inode_sector_count(inode);
  941. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  942. if (ret < 0)
  943. mlog_errno(ret);
  944. out:
  945. return ret;
  946. }
  947. static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
  948. handle_t *handle,
  949. struct inode *parent,
  950. struct inode *inode,
  951. struct buffer_head *fe_bh,
  952. struct ocfs2_alloc_context *data_ac)
  953. {
  954. int status;
  955. struct buffer_head *new_bh = NULL;
  956. mlog_entry_void();
  957. status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  958. data_ac, NULL, &new_bh);
  959. if (status < 0) {
  960. mlog_errno(status);
  961. goto bail;
  962. }
  963. ocfs2_set_new_buffer_uptodate(inode, new_bh);
  964. status = ocfs2_journal_access(handle, inode, new_bh,
  965. OCFS2_JOURNAL_ACCESS_CREATE);
  966. if (status < 0) {
  967. mlog_errno(status);
  968. goto bail;
  969. }
  970. memset(new_bh->b_data, 0, osb->sb->s_blocksize);
  971. ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data,
  972. osb->sb->s_blocksize);
  973. status = ocfs2_journal_dirty(handle, new_bh);
  974. if (status < 0) {
  975. mlog_errno(status);
  976. goto bail;
  977. }
  978. i_size_write(inode, inode->i_sb->s_blocksize);
  979. inode->i_nlink = 2;
  980. inode->i_blocks = ocfs2_inode_sector_count(inode);
  981. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  982. if (status < 0) {
  983. mlog_errno(status);
  984. goto bail;
  985. }
  986. status = 0;
  987. bail:
  988. if (new_bh)
  989. brelse(new_bh);
  990. mlog_exit(status);
  991. return status;
  992. }
  993. int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  994. handle_t *handle,
  995. struct inode *parent,
  996. struct inode *inode,
  997. struct buffer_head *fe_bh,
  998. struct ocfs2_alloc_context *data_ac)
  999. {
  1000. BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
  1001. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1002. return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
  1003. return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
  1004. data_ac);
  1005. }
  1006. static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
  1007. unsigned int new_size)
  1008. {
  1009. struct ocfs2_dir_entry *de;
  1010. struct ocfs2_dir_entry *prev_de;
  1011. char *de_buf, *limit;
  1012. unsigned int bytes = new_size - old_size;
  1013. limit = start + old_size;
  1014. de_buf = start;
  1015. de = (struct ocfs2_dir_entry *)de_buf;
  1016. do {
  1017. prev_de = de;
  1018. de_buf += le16_to_cpu(de->rec_len);
  1019. de = (struct ocfs2_dir_entry *)de_buf;
  1020. } while (de_buf < limit);
  1021. le16_add_cpu(&prev_de->rec_len, bytes);
  1022. }
  1023. /*
  1024. * We allocate enough clusters to fulfill "blocks_wanted", but set
  1025. * i_size to exactly one block. Ocfs2_extend_dir() will handle the
  1026. * rest automatically for us.
  1027. *
  1028. * *first_block_bh is a pointer to the 1st data block allocated to the
  1029. * directory.
  1030. */
  1031. static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
  1032. unsigned int blocks_wanted,
  1033. struct buffer_head **first_block_bh)
  1034. {
  1035. int ret, credits = OCFS2_INLINE_TO_EXTENTS_CREDITS;
  1036. u32 alloc, bit_off, len;
  1037. struct super_block *sb = dir->i_sb;
  1038. u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits;
  1039. struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  1040. struct ocfs2_inode_info *oi = OCFS2_I(dir);
  1041. struct ocfs2_alloc_context *data_ac;
  1042. struct buffer_head *dirdata_bh = NULL;
  1043. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1044. handle_t *handle;
  1045. alloc = ocfs2_clusters_for_bytes(sb, bytes);
  1046. /*
  1047. * We should never need more than 2 clusters for this -
  1048. * maximum dirent size is far less than one block. In fact,
  1049. * the only time we'd need more than one cluster is if
  1050. * blocksize == clustersize and the dirent won't fit in the
  1051. * extra space that the expansion to a single block gives. As
  1052. * of today, that only happens on 4k/4k file systems.
  1053. */
  1054. BUG_ON(alloc > 2);
  1055. ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
  1056. if (ret) {
  1057. mlog_errno(ret);
  1058. goto out;
  1059. }
  1060. down_write(&oi->ip_alloc_sem);
  1061. /*
  1062. * Prepare for worst case allocation scenario of two seperate
  1063. * extents.
  1064. */
  1065. if (alloc == 2)
  1066. credits += OCFS2_SUBALLOC_ALLOC;
  1067. handle = ocfs2_start_trans(osb, credits);
  1068. if (IS_ERR(handle)) {
  1069. ret = PTR_ERR(handle);
  1070. mlog_errno(ret);
  1071. goto out_sem;
  1072. }
  1073. /*
  1074. * Try to claim as many clusters as the bitmap can give though
  1075. * if we only get one now, that's enough to continue. The rest
  1076. * will be claimed after the conversion to extents.
  1077. */
  1078. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
  1079. if (ret) {
  1080. mlog_errno(ret);
  1081. goto out_commit;
  1082. }
  1083. /*
  1084. * Operations are carefully ordered so that we set up the new
  1085. * data block first. The conversion from inline data to
  1086. * extents follows.
  1087. */
  1088. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  1089. dirdata_bh = sb_getblk(sb, blkno);
  1090. if (!dirdata_bh) {
  1091. ret = -EIO;
  1092. mlog_errno(ret);
  1093. goto out_commit;
  1094. }
  1095. ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
  1096. ret = ocfs2_journal_access(handle, dir, dirdata_bh,
  1097. OCFS2_JOURNAL_ACCESS_CREATE);
  1098. if (ret) {
  1099. mlog_errno(ret);
  1100. goto out_commit;
  1101. }
  1102. memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
  1103. memset(dirdata_bh->b_data + i_size_read(dir), 0,
  1104. sb->s_blocksize - i_size_read(dir));
  1105. ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir),
  1106. sb->s_blocksize);
  1107. ret = ocfs2_journal_dirty(handle, dirdata_bh);
  1108. if (ret) {
  1109. mlog_errno(ret);
  1110. goto out_commit;
  1111. }
  1112. /*
  1113. * Set extent, i_size, etc on the directory. After this, the
  1114. * inode should contain the same exact dirents as before and
  1115. * be fully accessible from system calls.
  1116. *
  1117. * We let the later dirent insert modify c/mtime - to the user
  1118. * the data hasn't changed.
  1119. */
  1120. ret = ocfs2_journal_access(handle, dir, di_bh,
  1121. OCFS2_JOURNAL_ACCESS_CREATE);
  1122. if (ret) {
  1123. mlog_errno(ret);
  1124. goto out_commit;
  1125. }
  1126. spin_lock(&oi->ip_lock);
  1127. oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  1128. di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  1129. spin_unlock(&oi->ip_lock);
  1130. ocfs2_dinode_new_extent_list(dir, di);
  1131. i_size_write(dir, sb->s_blocksize);
  1132. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  1133. di->i_size = cpu_to_le64(sb->s_blocksize);
  1134. di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
  1135. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
  1136. dir->i_blocks = ocfs2_inode_sector_count(dir);
  1137. /*
  1138. * This should never fail as our extent list is empty and all
  1139. * related blocks have been journaled already.
  1140. */
  1141. ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 0, blkno, len, 0,
  1142. NULL);
  1143. if (ret) {
  1144. mlog_errno(ret);
  1145. goto out;
  1146. }
  1147. ret = ocfs2_journal_dirty(handle, di_bh);
  1148. if (ret) {
  1149. mlog_errno(ret);
  1150. goto out_commit;
  1151. }
  1152. /*
  1153. * We asked for two clusters, but only got one in the 1st
  1154. * pass. Claim the 2nd cluster as a separate extent.
  1155. */
  1156. if (alloc > len) {
  1157. ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
  1158. &len);
  1159. if (ret) {
  1160. mlog_errno(ret);
  1161. goto out_commit;
  1162. }
  1163. blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  1164. ret = ocfs2_insert_extent(osb, handle, dir, di_bh, 1, blkno,
  1165. len, 0, NULL);
  1166. if (ret) {
  1167. mlog_errno(ret);
  1168. goto out;
  1169. }
  1170. }
  1171. *first_block_bh = dirdata_bh;
  1172. dirdata_bh = NULL;
  1173. out_commit:
  1174. ocfs2_commit_trans(osb, handle);
  1175. out_sem:
  1176. up_write(&oi->ip_alloc_sem);
  1177. out:
  1178. if (data_ac)
  1179. ocfs2_free_alloc_context(data_ac);
  1180. brelse(dirdata_bh);
  1181. return ret;
  1182. }
  1183. /* returns a bh of the 1st new block in the allocation. */
  1184. static int ocfs2_do_extend_dir(struct super_block *sb,
  1185. handle_t *handle,
  1186. struct inode *dir,
  1187. struct buffer_head *parent_fe_bh,
  1188. struct ocfs2_alloc_context *data_ac,
  1189. struct ocfs2_alloc_context *meta_ac,
  1190. struct buffer_head **new_bh)
  1191. {
  1192. int status;
  1193. int extend;
  1194. u64 p_blkno, v_blkno;
  1195. spin_lock(&OCFS2_I(dir)->ip_lock);
  1196. extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  1197. spin_unlock(&OCFS2_I(dir)->ip_lock);
  1198. if (extend) {
  1199. u32 offset = OCFS2_I(dir)->ip_clusters;
  1200. status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset,
  1201. 1, 0, parent_fe_bh, handle,
  1202. data_ac, meta_ac, NULL);
  1203. BUG_ON(status == -EAGAIN);
  1204. if (status < 0) {
  1205. mlog_errno(status);
  1206. goto bail;
  1207. }
  1208. }
  1209. v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  1210. status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
  1211. if (status < 0) {
  1212. mlog_errno(status);
  1213. goto bail;
  1214. }
  1215. *new_bh = sb_getblk(sb, p_blkno);
  1216. if (!*new_bh) {
  1217. status = -EIO;
  1218. mlog_errno(status);
  1219. goto bail;
  1220. }
  1221. status = 0;
  1222. bail:
  1223. mlog_exit(status);
  1224. return status;
  1225. }
  1226. /*
  1227. * Assumes you already have a cluster lock on the directory.
  1228. *
  1229. * 'blocks_wanted' is only used if we have an inline directory which
  1230. * is to be turned into an extent based one. The size of the dirent to
  1231. * insert might be larger than the space gained by growing to just one
  1232. * block, so we may have to grow the inode by two blocks in that case.
  1233. */
  1234. static int ocfs2_extend_dir(struct ocfs2_super *osb,
  1235. struct inode *dir,
  1236. struct buffer_head *parent_fe_bh,
  1237. unsigned int blocks_wanted,
  1238. struct buffer_head **new_de_bh)
  1239. {
  1240. int status = 0;
  1241. int credits, num_free_extents, drop_alloc_sem = 0;
  1242. loff_t dir_i_size;
  1243. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
  1244. struct ocfs2_alloc_context *data_ac = NULL;
  1245. struct ocfs2_alloc_context *meta_ac = NULL;
  1246. handle_t *handle = NULL;
  1247. struct buffer_head *new_bh = NULL;
  1248. struct ocfs2_dir_entry * de;
  1249. struct super_block *sb = osb->sb;
  1250. mlog_entry_void();
  1251. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1252. status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
  1253. blocks_wanted, &new_bh);
  1254. if (status) {
  1255. mlog_errno(status);
  1256. goto bail;
  1257. }
  1258. if (blocks_wanted == 1) {
  1259. /*
  1260. * If the new dirent will fit inside the space
  1261. * created by pushing out to one block, then
  1262. * we can complete the operation
  1263. * here. Otherwise we have to expand i_size
  1264. * and format the 2nd block below.
  1265. */
  1266. BUG_ON(new_bh == NULL);
  1267. goto bail_bh;
  1268. }
  1269. /*
  1270. * Get rid of 'new_bh' - we want to format the 2nd
  1271. * data block and return that instead.
  1272. */
  1273. brelse(new_bh);
  1274. new_bh = NULL;
  1275. dir_i_size = i_size_read(dir);
  1276. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  1277. goto do_extend;
  1278. }
  1279. dir_i_size = i_size_read(dir);
  1280. mlog(0, "extending dir %llu (i_size = %lld)\n",
  1281. (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
  1282. /* dir->i_size is always block aligned. */
  1283. spin_lock(&OCFS2_I(dir)->ip_lock);
  1284. if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  1285. spin_unlock(&OCFS2_I(dir)->ip_lock);
  1286. num_free_extents = ocfs2_num_free_extents(osb, dir, fe);
  1287. if (num_free_extents < 0) {
  1288. status = num_free_extents;
  1289. mlog_errno(status);
  1290. goto bail;
  1291. }
  1292. if (!num_free_extents) {
  1293. status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac);
  1294. if (status < 0) {
  1295. if (status != -ENOSPC)
  1296. mlog_errno(status);
  1297. goto bail;
  1298. }
  1299. }
  1300. status = ocfs2_reserve_clusters(osb, 1, &data_ac);
  1301. if (status < 0) {
  1302. if (status != -ENOSPC)
  1303. mlog_errno(status);
  1304. goto bail;
  1305. }
  1306. credits = ocfs2_calc_extend_credits(sb, fe, 1);
  1307. } else {
  1308. spin_unlock(&OCFS2_I(dir)->ip_lock);
  1309. credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  1310. }
  1311. do_extend:
  1312. down_write(&OCFS2_I(dir)->ip_alloc_sem);
  1313. drop_alloc_sem = 1;
  1314. handle = ocfs2_start_trans(osb, credits);
  1315. if (IS_ERR(handle)) {
  1316. status = PTR_ERR(handle);
  1317. handle = NULL;
  1318. mlog_errno(status);
  1319. goto bail;
  1320. }
  1321. status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  1322. data_ac, meta_ac, &new_bh);
  1323. if (status < 0) {
  1324. mlog_errno(status);
  1325. goto bail;
  1326. }
  1327. ocfs2_set_new_buffer_uptodate(dir, new_bh);
  1328. status = ocfs2_journal_access(handle, dir, new_bh,
  1329. OCFS2_JOURNAL_ACCESS_CREATE);
  1330. if (status < 0) {
  1331. mlog_errno(status);
  1332. goto bail;
  1333. }
  1334. memset(new_bh->b_data, 0, sb->s_blocksize);
  1335. de = (struct ocfs2_dir_entry *) new_bh->b_data;
  1336. de->inode = 0;
  1337. de->rec_len = cpu_to_le16(sb->s_blocksize);
  1338. status = ocfs2_journal_dirty(handle, new_bh);
  1339. if (status < 0) {
  1340. mlog_errno(status);
  1341. goto bail;
  1342. }
  1343. dir_i_size += dir->i_sb->s_blocksize;
  1344. i_size_write(dir, dir_i_size);
  1345. dir->i_blocks = ocfs2_inode_sector_count(dir);
  1346. status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  1347. if (status < 0) {
  1348. mlog_errno(status);
  1349. goto bail;
  1350. }
  1351. bail_bh:
  1352. *new_de_bh = new_bh;
  1353. get_bh(*new_de_bh);
  1354. bail:
  1355. if (drop_alloc_sem)
  1356. up_write(&OCFS2_I(dir)->ip_alloc_sem);
  1357. if (handle)
  1358. ocfs2_commit_trans(osb, handle);
  1359. if (data_ac)
  1360. ocfs2_free_alloc_context(data_ac);
  1361. if (meta_ac)
  1362. ocfs2_free_alloc_context(meta_ac);
  1363. if (new_bh)
  1364. brelse(new_bh);
  1365. mlog_exit(status);
  1366. return status;
  1367. }
  1368. static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
  1369. const char *name, int namelen,
  1370. struct buffer_head **ret_de_bh,
  1371. unsigned int *blocks_wanted)
  1372. {
  1373. int ret;
  1374. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1375. struct ocfs2_dir_entry *de, *last_de = NULL;
  1376. char *de_buf, *limit;
  1377. unsigned long offset = 0;
  1378. unsigned int rec_len, new_rec_len;
  1379. de_buf = di->id2.i_data.id_data;
  1380. limit = de_buf + i_size_read(dir);
  1381. rec_len = OCFS2_DIR_REC_LEN(namelen);
  1382. while (de_buf < limit) {
  1383. de = (struct ocfs2_dir_entry *)de_buf;
  1384. if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
  1385. ret = -ENOENT;
  1386. goto out;
  1387. }
  1388. if (ocfs2_match(namelen, name, de)) {
  1389. ret = -EEXIST;
  1390. goto out;
  1391. }
  1392. if (ocfs2_dirent_would_fit(de, rec_len)) {
  1393. /* Ok, we found a spot. Return this bh and let
  1394. * the caller actually fill it in. */
  1395. *ret_de_bh = di_bh;
  1396. get_bh(*ret_de_bh);
  1397. ret = 0;
  1398. goto out;
  1399. }
  1400. last_de = de;
  1401. de_buf += le16_to_cpu(de->rec_len);
  1402. offset += le16_to_cpu(de->rec_len);
  1403. }
  1404. /*
  1405. * We're going to require expansion of the directory - figure
  1406. * out how many blocks we'll need so that a place for the
  1407. * dirent can be found.
  1408. */
  1409. *blocks_wanted = 1;
  1410. new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir));
  1411. if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
  1412. *blocks_wanted = 2;
  1413. ret = -ENOSPC;
  1414. out:
  1415. return ret;
  1416. }
  1417. static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
  1418. int namelen, struct buffer_head **ret_de_bh)
  1419. {
  1420. unsigned long offset;
  1421. struct buffer_head *bh = NULL;
  1422. unsigned short rec_len;
  1423. struct ocfs2_dir_entry *de;
  1424. struct super_block *sb = dir->i_sb;
  1425. int status;
  1426. bh = ocfs2_bread(dir, 0, &status, 0);
  1427. if (!bh) {
  1428. mlog_errno(status);
  1429. goto bail;
  1430. }
  1431. rec_len = OCFS2_DIR_REC_LEN(namelen);
  1432. offset = 0;
  1433. de = (struct ocfs2_dir_entry *) bh->b_data;
  1434. while (1) {
  1435. if ((char *)de >= sb->s_blocksize + bh->b_data) {
  1436. brelse(bh);
  1437. bh = NULL;
  1438. if (i_size_read(dir) <= offset) {
  1439. /*
  1440. * Caller will have to expand this
  1441. * directory.
  1442. */
  1443. status = -ENOSPC;
  1444. goto bail;
  1445. }
  1446. bh = ocfs2_bread(dir,
  1447. offset >> sb->s_blocksize_bits,
  1448. &status,
  1449. 0);
  1450. if (!bh) {
  1451. mlog_errno(status);
  1452. goto bail;
  1453. }
  1454. /* move to next block */
  1455. de = (struct ocfs2_dir_entry *) bh->b_data;
  1456. }
  1457. if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  1458. status = -ENOENT;
  1459. goto bail;
  1460. }
  1461. if (ocfs2_match(namelen, name, de)) {
  1462. status = -EEXIST;
  1463. goto bail;
  1464. }
  1465. if (ocfs2_dirent_would_fit(de, rec_len)) {
  1466. /* Ok, we found a spot. Return this bh and let
  1467. * the caller actually fill it in. */
  1468. *ret_de_bh = bh;
  1469. get_bh(*ret_de_bh);
  1470. status = 0;
  1471. goto bail;
  1472. }
  1473. offset += le16_to_cpu(de->rec_len);
  1474. de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  1475. }
  1476. status = 0;
  1477. bail:
  1478. if (bh)
  1479. brelse(bh);
  1480. mlog_exit(status);
  1481. return status;
  1482. }
  1483. int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  1484. struct inode *dir,
  1485. struct buffer_head *parent_fe_bh,
  1486. const char *name,
  1487. int namelen,
  1488. struct buffer_head **ret_de_bh)
  1489. {
  1490. int ret;
  1491. unsigned int blocks_wanted = 1;
  1492. struct buffer_head *bh = NULL;
  1493. mlog(0, "getting ready to insert namelen %d into dir %llu\n",
  1494. namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
  1495. *ret_de_bh = NULL;
  1496. if (!namelen) {
  1497. ret = -EINVAL;
  1498. mlog_errno(ret);
  1499. goto out;
  1500. }
  1501. if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1502. ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
  1503. namelen, &bh, &blocks_wanted);
  1504. } else
  1505. ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
  1506. if (ret && ret != -ENOSPC) {
  1507. mlog_errno(ret);
  1508. goto out;
  1509. }
  1510. if (ret == -ENOSPC) {
  1511. /*
  1512. * We have to expand the directory to add this name.
  1513. */
  1514. BUG_ON(bh);
  1515. ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
  1516. &bh);
  1517. if (ret) {
  1518. if (ret != -ENOSPC)
  1519. mlog_errno(ret);
  1520. goto out;
  1521. }
  1522. BUG_ON(!bh);
  1523. }
  1524. *ret_de_bh = bh;
  1525. bh = NULL;
  1526. out:
  1527. if (bh)
  1528. brelse(bh);
  1529. return ret;
  1530. }