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