dir.c 44 KB

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