dir.c 43 KB

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