dir.c 44 KB

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