dir.c 44 KB

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