dir.c 43 KB

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