dir.c 45 KB

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