dir.c 45 KB

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