dir.c 43 KB

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