dir.c 51 KB

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