dir.c 43 KB

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