dir.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. /*
  10. * Implements Extendible Hashing as described in:
  11. * "Extendible Hashing" by Fagin, et al in
  12. * __ACM Trans. on Database Systems__, Sept 1979.
  13. *
  14. *
  15. * Here's the layout of dirents which is essentially the same as that of ext2
  16. * within a single block. The field de_name_len is the number of bytes
  17. * actually required for the name (no null terminator). The field de_rec_len
  18. * is the number of bytes allocated to the dirent. The offset of the next
  19. * dirent in the block is (dirent + dirent->de_rec_len). When a dirent is
  20. * deleted, the preceding dirent inherits its allocated space, ie
  21. * prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained
  22. * by adding de_rec_len to the current dirent, this essentially causes the
  23. * deleted dirent to get jumped over when iterating through all the dirents.
  24. *
  25. * When deleting the first dirent in a block, there is no previous dirent so
  26. * the field de_ino is set to zero to designate it as deleted. When allocating
  27. * a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the
  28. * first dirent has (de_ino == 0) and de_rec_len is large enough, this first
  29. * dirent is allocated. Otherwise it must go through all the 'used' dirents
  30. * searching for one in which the amount of total space minus the amount of
  31. * used space will provide enough space for the new dirent.
  32. *
  33. * There are two types of blocks in which dirents reside. In a stuffed dinode,
  34. * the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of
  35. * the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the
  36. * beginning of the leaf block. The dirents reside in leaves when
  37. *
  38. * dip->i_di.di_flags & GFS2_DIF_EXHASH is true
  39. *
  40. * Otherwise, the dirents are "linear", within a single stuffed dinode block.
  41. *
  42. * When the dirents are in leaves, the actual contents of the directory file are
  43. * used as an array of 64-bit block pointers pointing to the leaf blocks. The
  44. * dirents are NOT in the directory file itself. There can be more than one
  45. * block pointer in the array that points to the same leaf. In fact, when a
  46. * directory is first converted from linear to exhash, all of the pointers
  47. * point to the same leaf.
  48. *
  49. * When a leaf is completely full, the size of the hash table can be
  50. * doubled unless it is already at the maximum size which is hard coded into
  51. * GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list,
  52. * but never before the maximum hash table size has been reached.
  53. */
  54. #include <linux/sched.h>
  55. #include <linux/slab.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/buffer_head.h>
  58. #include <linux/sort.h>
  59. #include <linux/gfs2_ondisk.h>
  60. #include <linux/crc32.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/lm_interface.h>
  63. #include "gfs2.h"
  64. #include "incore.h"
  65. #include "dir.h"
  66. #include "glock.h"
  67. #include "inode.h"
  68. #include "meta_io.h"
  69. #include "quota.h"
  70. #include "rgrp.h"
  71. #include "trans.h"
  72. #include "bmap.h"
  73. #include "util.h"
  74. #define IS_LEAF 1 /* Hashed (leaf) directory */
  75. #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */
  76. #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1)
  77. #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1))
  78. typedef int (*leaf_call_t) (struct gfs2_inode *dip, u32 index, u32 len,
  79. u64 leaf_no, void *data);
  80. typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent,
  81. const struct qstr *name, void *opaque);
  82. int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block,
  83. struct buffer_head **bhp)
  84. {
  85. struct buffer_head *bh;
  86. bh = gfs2_meta_new(ip->i_gl, block);
  87. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  88. gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD);
  89. gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
  90. *bhp = bh;
  91. return 0;
  92. }
  93. static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block,
  94. struct buffer_head **bhp)
  95. {
  96. struct buffer_head *bh;
  97. int error;
  98. error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh);
  99. if (error)
  100. return error;
  101. if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) {
  102. brelse(bh);
  103. return -EIO;
  104. }
  105. *bhp = bh;
  106. return 0;
  107. }
  108. static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf,
  109. unsigned int offset, unsigned int size)
  110. {
  111. struct buffer_head *dibh;
  112. int error;
  113. error = gfs2_meta_inode_buffer(ip, &dibh);
  114. if (error)
  115. return error;
  116. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  117. memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size);
  118. if (ip->i_di.di_size < offset + size)
  119. ip->i_di.di_size = offset + size;
  120. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  121. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  122. brelse(dibh);
  123. return size;
  124. }
  125. /**
  126. * gfs2_dir_write_data - Write directory information to the inode
  127. * @ip: The GFS2 inode
  128. * @buf: The buffer containing information to be written
  129. * @offset: The file offset to start writing at
  130. * @size: The amount of data to write
  131. *
  132. * Returns: The number of bytes correctly written or error code
  133. */
  134. static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf,
  135. u64 offset, unsigned int size)
  136. {
  137. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  138. struct buffer_head *dibh;
  139. u64 lblock, dblock;
  140. u32 extlen = 0;
  141. unsigned int o;
  142. int copied = 0;
  143. int error = 0;
  144. if (!size)
  145. return 0;
  146. if (gfs2_is_stuffed(ip) &&
  147. offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  148. return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset,
  149. size);
  150. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  151. return -EINVAL;
  152. if (gfs2_is_stuffed(ip)) {
  153. error = gfs2_unstuff_dinode(ip, NULL);
  154. if (error)
  155. return error;
  156. }
  157. lblock = offset;
  158. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  159. while (copied < size) {
  160. unsigned int amount;
  161. struct buffer_head *bh;
  162. int new;
  163. amount = size - copied;
  164. if (amount > sdp->sd_sb.sb_bsize - o)
  165. amount = sdp->sd_sb.sb_bsize - o;
  166. if (!extlen) {
  167. new = 1;
  168. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  169. &dblock, &extlen);
  170. if (error)
  171. goto fail;
  172. error = -EIO;
  173. if (gfs2_assert_withdraw(sdp, dblock))
  174. goto fail;
  175. }
  176. if (amount == sdp->sd_jbsize || new)
  177. error = gfs2_dir_get_new_buffer(ip, dblock, &bh);
  178. else
  179. error = gfs2_dir_get_existing_buffer(ip, dblock, &bh);
  180. if (error)
  181. goto fail;
  182. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  183. memcpy(bh->b_data + o, buf, amount);
  184. brelse(bh);
  185. if (error)
  186. goto fail;
  187. buf += amount;
  188. copied += amount;
  189. lblock++;
  190. dblock++;
  191. extlen--;
  192. o = sizeof(struct gfs2_meta_header);
  193. }
  194. out:
  195. error = gfs2_meta_inode_buffer(ip, &dibh);
  196. if (error)
  197. return error;
  198. if (ip->i_di.di_size < offset + copied)
  199. ip->i_di.di_size = offset + copied;
  200. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  201. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  202. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  203. brelse(dibh);
  204. return copied;
  205. fail:
  206. if (copied)
  207. goto out;
  208. return error;
  209. }
  210. static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf,
  211. u64 offset, unsigned int size)
  212. {
  213. struct buffer_head *dibh;
  214. int error;
  215. error = gfs2_meta_inode_buffer(ip, &dibh);
  216. if (!error) {
  217. offset += sizeof(struct gfs2_dinode);
  218. memcpy(buf, dibh->b_data + offset, size);
  219. brelse(dibh);
  220. }
  221. return (error) ? error : size;
  222. }
  223. /**
  224. * gfs2_dir_read_data - Read a data from a directory inode
  225. * @ip: The GFS2 Inode
  226. * @buf: The buffer to place result into
  227. * @offset: File offset to begin jdata_readng from
  228. * @size: Amount of data to transfer
  229. *
  230. * Returns: The amount of data actually copied or the error
  231. */
  232. static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset,
  233. unsigned int size, unsigned ra)
  234. {
  235. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  236. u64 lblock, dblock;
  237. u32 extlen = 0;
  238. unsigned int o;
  239. int copied = 0;
  240. int error = 0;
  241. if (offset >= ip->i_di.di_size)
  242. return 0;
  243. if (offset + size > ip->i_di.di_size)
  244. size = ip->i_di.di_size - offset;
  245. if (!size)
  246. return 0;
  247. if (gfs2_is_stuffed(ip))
  248. return gfs2_dir_read_stuffed(ip, buf, offset, size);
  249. if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip)))
  250. return -EINVAL;
  251. lblock = offset;
  252. o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header);
  253. while (copied < size) {
  254. unsigned int amount;
  255. struct buffer_head *bh;
  256. int new;
  257. amount = size - copied;
  258. if (amount > sdp->sd_sb.sb_bsize - o)
  259. amount = sdp->sd_sb.sb_bsize - o;
  260. if (!extlen) {
  261. new = 0;
  262. error = gfs2_extent_map(&ip->i_inode, lblock, &new,
  263. &dblock, &extlen);
  264. if (error || !dblock)
  265. goto fail;
  266. BUG_ON(extlen < 1);
  267. if (!ra)
  268. extlen = 1;
  269. bh = gfs2_meta_ra(ip->i_gl, dblock, extlen);
  270. }
  271. if (!bh) {
  272. error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh);
  273. if (error)
  274. goto fail;
  275. }
  276. error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD);
  277. if (error) {
  278. brelse(bh);
  279. goto fail;
  280. }
  281. dblock++;
  282. extlen--;
  283. memcpy(buf, bh->b_data + o, amount);
  284. brelse(bh);
  285. bh = NULL;
  286. buf += amount;
  287. copied += amount;
  288. lblock++;
  289. o = sizeof(struct gfs2_meta_header);
  290. }
  291. return copied;
  292. fail:
  293. return (copied) ? copied : error;
  294. }
  295. static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent,
  296. const struct qstr *name, int ret)
  297. {
  298. if (dent->de_inum.no_addr != 0 &&
  299. be32_to_cpu(dent->de_hash) == name->hash &&
  300. be16_to_cpu(dent->de_name_len) == name->len &&
  301. memcmp(dent+1, name->name, name->len) == 0)
  302. return ret;
  303. return 0;
  304. }
  305. static int gfs2_dirent_find(const struct gfs2_dirent *dent,
  306. const struct qstr *name,
  307. void *opaque)
  308. {
  309. return __gfs2_dirent_find(dent, name, 1);
  310. }
  311. static int gfs2_dirent_prev(const struct gfs2_dirent *dent,
  312. const struct qstr *name,
  313. void *opaque)
  314. {
  315. return __gfs2_dirent_find(dent, name, 2);
  316. }
  317. /*
  318. * name->name holds ptr to start of block.
  319. * name->len holds size of block.
  320. */
  321. static int gfs2_dirent_last(const struct gfs2_dirent *dent,
  322. const struct qstr *name,
  323. void *opaque)
  324. {
  325. const char *start = name->name;
  326. const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len);
  327. if (name->len == (end - start))
  328. return 1;
  329. return 0;
  330. }
  331. static int gfs2_dirent_find_space(const struct gfs2_dirent *dent,
  332. const struct qstr *name,
  333. void *opaque)
  334. {
  335. unsigned required = GFS2_DIRENT_SIZE(name->len);
  336. unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  337. unsigned totlen = be16_to_cpu(dent->de_rec_len);
  338. if (!dent->de_inum.no_addr)
  339. actual = GFS2_DIRENT_SIZE(0);
  340. if (totlen - actual >= required)
  341. return 1;
  342. return 0;
  343. }
  344. struct dirent_gather {
  345. const struct gfs2_dirent **pdent;
  346. unsigned offset;
  347. };
  348. static int gfs2_dirent_gather(const struct gfs2_dirent *dent,
  349. const struct qstr *name,
  350. void *opaque)
  351. {
  352. struct dirent_gather *g = opaque;
  353. if (dent->de_inum.no_addr) {
  354. g->pdent[g->offset++] = dent;
  355. }
  356. return 0;
  357. }
  358. /*
  359. * Other possible things to check:
  360. * - Inode located within filesystem size (and on valid block)
  361. * - Valid directory entry type
  362. * Not sure how heavy-weight we want to make this... could also check
  363. * hash is correct for example, but that would take a lot of extra time.
  364. * For now the most important thing is to check that the various sizes
  365. * are correct.
  366. */
  367. static int gfs2_check_dirent(struct gfs2_dirent *dent, unsigned int offset,
  368. unsigned int size, unsigned int len, int first)
  369. {
  370. const char *msg = "gfs2_dirent too small";
  371. if (unlikely(size < sizeof(struct gfs2_dirent)))
  372. goto error;
  373. msg = "gfs2_dirent misaligned";
  374. if (unlikely(offset & 0x7))
  375. goto error;
  376. msg = "gfs2_dirent points beyond end of block";
  377. if (unlikely(offset + size > len))
  378. goto error;
  379. msg = "zero inode number";
  380. if (unlikely(!first && !dent->de_inum.no_addr))
  381. goto error;
  382. msg = "name length is greater than space in dirent";
  383. if (dent->de_inum.no_addr &&
  384. unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) >
  385. size))
  386. goto error;
  387. return 0;
  388. error:
  389. printk(KERN_WARNING "gfs2_check_dirent: %s (%s)\n", msg,
  390. first ? "first in block" : "not first in block");
  391. return -EIO;
  392. }
  393. static int gfs2_dirent_offset(const void *buf)
  394. {
  395. const struct gfs2_meta_header *h = buf;
  396. int offset;
  397. BUG_ON(buf == NULL);
  398. switch(be32_to_cpu(h->mh_type)) {
  399. case GFS2_METATYPE_LF:
  400. offset = sizeof(struct gfs2_leaf);
  401. break;
  402. case GFS2_METATYPE_DI:
  403. offset = sizeof(struct gfs2_dinode);
  404. break;
  405. default:
  406. goto wrong_type;
  407. }
  408. return offset;
  409. wrong_type:
  410. printk(KERN_WARNING "gfs2_scan_dirent: wrong block type %u\n",
  411. be32_to_cpu(h->mh_type));
  412. return -1;
  413. }
  414. static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf,
  415. unsigned int len, gfs2_dscan_t scan,
  416. const struct qstr *name,
  417. void *opaque)
  418. {
  419. struct gfs2_dirent *dent, *prev;
  420. unsigned offset;
  421. unsigned size;
  422. int ret = 0;
  423. ret = gfs2_dirent_offset(buf);
  424. if (ret < 0)
  425. goto consist_inode;
  426. offset = ret;
  427. prev = NULL;
  428. dent = buf + offset;
  429. size = be16_to_cpu(dent->de_rec_len);
  430. if (gfs2_check_dirent(dent, offset, size, len, 1))
  431. goto consist_inode;
  432. do {
  433. ret = scan(dent, name, opaque);
  434. if (ret)
  435. break;
  436. offset += size;
  437. if (offset == len)
  438. break;
  439. prev = dent;
  440. dent = buf + offset;
  441. size = be16_to_cpu(dent->de_rec_len);
  442. if (gfs2_check_dirent(dent, offset, size, len, 0))
  443. goto consist_inode;
  444. } while(1);
  445. switch(ret) {
  446. case 0:
  447. return NULL;
  448. case 1:
  449. return dent;
  450. case 2:
  451. return prev ? prev : dent;
  452. default:
  453. BUG_ON(ret > 0);
  454. return ERR_PTR(ret);
  455. }
  456. consist_inode:
  457. gfs2_consist_inode(GFS2_I(inode));
  458. return ERR_PTR(-EIO);
  459. }
  460. /**
  461. * dirent_first - Return the first dirent
  462. * @dip: the directory
  463. * @bh: The buffer
  464. * @dent: Pointer to list of dirents
  465. *
  466. * return first dirent whether bh points to leaf or stuffed dinode
  467. *
  468. * Returns: IS_LEAF, IS_DINODE, or -errno
  469. */
  470. static int dirent_first(struct gfs2_inode *dip, struct buffer_head *bh,
  471. struct gfs2_dirent **dent)
  472. {
  473. struct gfs2_meta_header *h = (struct gfs2_meta_header *)bh->b_data;
  474. if (be32_to_cpu(h->mh_type) == GFS2_METATYPE_LF) {
  475. if (gfs2_meta_check(GFS2_SB(&dip->i_inode), bh))
  476. return -EIO;
  477. *dent = (struct gfs2_dirent *)(bh->b_data +
  478. sizeof(struct gfs2_leaf));
  479. return IS_LEAF;
  480. } else {
  481. if (gfs2_metatype_check(GFS2_SB(&dip->i_inode), bh, GFS2_METATYPE_DI))
  482. return -EIO;
  483. *dent = (struct gfs2_dirent *)(bh->b_data +
  484. sizeof(struct gfs2_dinode));
  485. return IS_DINODE;
  486. }
  487. }
  488. static int dirent_check_reclen(struct gfs2_inode *dip,
  489. const struct gfs2_dirent *d, const void *end_p)
  490. {
  491. const void *ptr = d;
  492. u16 rec_len = be16_to_cpu(d->de_rec_len);
  493. if (unlikely(rec_len < sizeof(struct gfs2_dirent)))
  494. goto broken;
  495. ptr += rec_len;
  496. if (ptr < end_p)
  497. return rec_len;
  498. if (ptr == end_p)
  499. return -ENOENT;
  500. broken:
  501. gfs2_consist_inode(dip);
  502. return -EIO;
  503. }
  504. /**
  505. * dirent_next - Next dirent
  506. * @dip: the directory
  507. * @bh: The buffer
  508. * @dent: Pointer to list of dirents
  509. *
  510. * Returns: 0 on success, error code otherwise
  511. */
  512. static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh,
  513. struct gfs2_dirent **dent)
  514. {
  515. struct gfs2_dirent *cur = *dent, *tmp;
  516. char *bh_end = bh->b_data + bh->b_size;
  517. int ret;
  518. ret = dirent_check_reclen(dip, cur, bh_end);
  519. if (ret < 0)
  520. return ret;
  521. tmp = (void *)cur + ret;
  522. ret = dirent_check_reclen(dip, tmp, bh_end);
  523. if (ret == -EIO)
  524. return ret;
  525. /* Only the first dent could ever have de_inum.no_addr == 0 */
  526. if (!tmp->de_inum.no_addr) {
  527. gfs2_consist_inode(dip);
  528. return -EIO;
  529. }
  530. *dent = tmp;
  531. return 0;
  532. }
  533. /**
  534. * dirent_del - Delete a dirent
  535. * @dip: The GFS2 inode
  536. * @bh: The buffer
  537. * @prev: The previous dirent
  538. * @cur: The current dirent
  539. *
  540. */
  541. static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh,
  542. struct gfs2_dirent *prev, struct gfs2_dirent *cur)
  543. {
  544. u16 cur_rec_len, prev_rec_len;
  545. if (!cur->de_inum.no_addr) {
  546. gfs2_consist_inode(dip);
  547. return;
  548. }
  549. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  550. /* If there is no prev entry, this is the first entry in the block.
  551. The de_rec_len is already as big as it needs to be. Just zero
  552. out the inode number and return. */
  553. if (!prev) {
  554. cur->de_inum.no_addr = 0; /* No endianess worries */
  555. return;
  556. }
  557. /* Combine this dentry with the previous one. */
  558. prev_rec_len = be16_to_cpu(prev->de_rec_len);
  559. cur_rec_len = be16_to_cpu(cur->de_rec_len);
  560. if ((char *)prev + prev_rec_len != (char *)cur)
  561. gfs2_consist_inode(dip);
  562. if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size)
  563. gfs2_consist_inode(dip);
  564. prev_rec_len += cur_rec_len;
  565. prev->de_rec_len = cpu_to_be16(prev_rec_len);
  566. }
  567. /*
  568. * Takes a dent from which to grab space as an argument. Returns the
  569. * newly created dent.
  570. */
  571. static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode,
  572. struct gfs2_dirent *dent,
  573. const struct qstr *name,
  574. struct buffer_head *bh)
  575. {
  576. struct gfs2_inode *ip = GFS2_I(inode);
  577. struct gfs2_dirent *ndent;
  578. unsigned offset = 0, totlen;
  579. if (dent->de_inum.no_addr)
  580. offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len));
  581. totlen = be16_to_cpu(dent->de_rec_len);
  582. BUG_ON(offset + name->len > totlen);
  583. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  584. ndent = (struct gfs2_dirent *)((char *)dent + offset);
  585. dent->de_rec_len = cpu_to_be16(offset);
  586. gfs2_qstr2dirent(name, totlen - offset, ndent);
  587. return ndent;
  588. }
  589. static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode,
  590. struct buffer_head *bh,
  591. const struct qstr *name)
  592. {
  593. struct gfs2_dirent *dent;
  594. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  595. gfs2_dirent_find_space, name, NULL);
  596. if (!dent || IS_ERR(dent))
  597. return dent;
  598. return gfs2_init_dirent(inode, dent, name, bh);
  599. }
  600. static int get_leaf(struct gfs2_inode *dip, u64 leaf_no,
  601. struct buffer_head **bhp)
  602. {
  603. int error;
  604. error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp);
  605. if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) {
  606. /* printk(KERN_INFO "block num=%llu\n", leaf_no); */
  607. error = -EIO;
  608. }
  609. return error;
  610. }
  611. /**
  612. * get_leaf_nr - Get a leaf number associated with the index
  613. * @dip: The GFS2 inode
  614. * @index:
  615. * @leaf_out:
  616. *
  617. * Returns: 0 on success, error code otherwise
  618. */
  619. static int get_leaf_nr(struct gfs2_inode *dip, u32 index,
  620. u64 *leaf_out)
  621. {
  622. u64 leaf_no;
  623. int error;
  624. error = gfs2_dir_read_data(dip, (char *)&leaf_no,
  625. index * sizeof(u64),
  626. sizeof(u64), 0);
  627. if (error != sizeof(u64))
  628. return (error < 0) ? error : -EIO;
  629. *leaf_out = be64_to_cpu(leaf_no);
  630. return 0;
  631. }
  632. static int get_first_leaf(struct gfs2_inode *dip, u32 index,
  633. struct buffer_head **bh_out)
  634. {
  635. u64 leaf_no;
  636. int error;
  637. error = get_leaf_nr(dip, index, &leaf_no);
  638. if (!error)
  639. error = get_leaf(dip, leaf_no, bh_out);
  640. return error;
  641. }
  642. static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode,
  643. const struct qstr *name,
  644. gfs2_dscan_t scan,
  645. struct buffer_head **pbh)
  646. {
  647. struct buffer_head *bh;
  648. struct gfs2_dirent *dent;
  649. struct gfs2_inode *ip = GFS2_I(inode);
  650. int error;
  651. if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
  652. struct gfs2_leaf *leaf;
  653. unsigned hsize = 1 << ip->i_di.di_depth;
  654. unsigned index;
  655. u64 ln;
  656. if (hsize * sizeof(u64) != ip->i_di.di_size) {
  657. gfs2_consist_inode(ip);
  658. return ERR_PTR(-EIO);
  659. }
  660. index = name->hash >> (32 - ip->i_di.di_depth);
  661. error = get_first_leaf(ip, index, &bh);
  662. if (error)
  663. return ERR_PTR(error);
  664. do {
  665. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  666. scan, name, NULL);
  667. if (dent)
  668. goto got_dent;
  669. leaf = (struct gfs2_leaf *)bh->b_data;
  670. ln = be64_to_cpu(leaf->lf_next);
  671. brelse(bh);
  672. if (!ln)
  673. break;
  674. error = get_leaf(ip, ln, &bh);
  675. } while(!error);
  676. return error ? ERR_PTR(error) : NULL;
  677. }
  678. error = gfs2_meta_inode_buffer(ip, &bh);
  679. if (error)
  680. return ERR_PTR(error);
  681. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL);
  682. got_dent:
  683. if (unlikely(dent == NULL || IS_ERR(dent))) {
  684. brelse(bh);
  685. bh = NULL;
  686. }
  687. *pbh = bh;
  688. return dent;
  689. }
  690. static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth)
  691. {
  692. struct gfs2_inode *ip = GFS2_I(inode);
  693. u64 bn = gfs2_alloc_meta(ip);
  694. struct buffer_head *bh = gfs2_meta_new(ip->i_gl, bn);
  695. struct gfs2_leaf *leaf;
  696. struct gfs2_dirent *dent;
  697. struct qstr name = { .name = "", .len = 0, .hash = 0 };
  698. if (!bh)
  699. return NULL;
  700. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  701. gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF);
  702. leaf = (struct gfs2_leaf *)bh->b_data;
  703. leaf->lf_depth = cpu_to_be16(depth);
  704. leaf->lf_entries = 0;
  705. leaf->lf_dirent_format = cpu_to_be16(GFS2_FORMAT_DE);
  706. leaf->lf_next = 0;
  707. memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved));
  708. dent = (struct gfs2_dirent *)(leaf+1);
  709. gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent);
  710. *pbh = bh;
  711. return leaf;
  712. }
  713. /**
  714. * dir_make_exhash - Convert a stuffed directory into an ExHash directory
  715. * @dip: The GFS2 inode
  716. *
  717. * Returns: 0 on success, error code otherwise
  718. */
  719. static int dir_make_exhash(struct inode *inode)
  720. {
  721. struct gfs2_inode *dip = GFS2_I(inode);
  722. struct gfs2_sbd *sdp = GFS2_SB(inode);
  723. struct gfs2_dirent *dent;
  724. struct qstr args;
  725. struct buffer_head *bh, *dibh;
  726. struct gfs2_leaf *leaf;
  727. int y;
  728. u32 x;
  729. u64 *lp, bn;
  730. int error;
  731. error = gfs2_meta_inode_buffer(dip, &dibh);
  732. if (error)
  733. return error;
  734. /* Turn over a new leaf */
  735. leaf = new_leaf(inode, &bh, 0);
  736. if (!leaf)
  737. return -ENOSPC;
  738. bn = bh->b_blocknr;
  739. gfs2_assert(sdp, dip->i_di.di_entries < (1 << 16));
  740. leaf->lf_entries = cpu_to_be16(dip->i_di.di_entries);
  741. /* Copy dirents */
  742. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh,
  743. sizeof(struct gfs2_dinode));
  744. /* Find last entry */
  745. x = 0;
  746. args.len = bh->b_size - sizeof(struct gfs2_dinode) +
  747. sizeof(struct gfs2_leaf);
  748. args.name = bh->b_data;
  749. dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size,
  750. gfs2_dirent_last, &args, NULL);
  751. if (!dent) {
  752. brelse(bh);
  753. brelse(dibh);
  754. return -EIO;
  755. }
  756. if (IS_ERR(dent)) {
  757. brelse(bh);
  758. brelse(dibh);
  759. return PTR_ERR(dent);
  760. }
  761. /* Adjust the last dirent's record length
  762. (Remember that dent still points to the last entry.) */
  763. dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) +
  764. sizeof(struct gfs2_dinode) -
  765. sizeof(struct gfs2_leaf));
  766. brelse(bh);
  767. /* We're done with the new leaf block, now setup the new
  768. hash table. */
  769. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  770. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  771. lp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  772. for (x = sdp->sd_hash_ptrs; x--; lp++)
  773. *lp = cpu_to_be64(bn);
  774. dip->i_di.di_size = sdp->sd_sb.sb_bsize / 2;
  775. dip->i_di.di_blocks++;
  776. dip->i_di.di_flags |= GFS2_DIF_EXHASH;
  777. dip->i_di.di_payload_format = 0;
  778. for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ;
  779. dip->i_di.di_depth = y;
  780. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  781. brelse(dibh);
  782. return 0;
  783. }
  784. /**
  785. * dir_split_leaf - Split a leaf block into two
  786. * @dip: The GFS2 inode
  787. * @index:
  788. * @leaf_no:
  789. *
  790. * Returns: 0 on success, error code on failure
  791. */
  792. static int dir_split_leaf(struct inode *inode, const struct qstr *name)
  793. {
  794. struct gfs2_inode *dip = GFS2_I(inode);
  795. struct buffer_head *nbh, *obh, *dibh;
  796. struct gfs2_leaf *nleaf, *oleaf;
  797. struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new;
  798. u32 start, len, half_len, divider;
  799. u64 bn, *lp, leaf_no;
  800. u32 index;
  801. int x, moved = 0;
  802. int error;
  803. index = name->hash >> (32 - dip->i_di.di_depth);
  804. error = get_leaf_nr(dip, index, &leaf_no);
  805. if (error)
  806. return error;
  807. /* Get the old leaf block */
  808. error = get_leaf(dip, leaf_no, &obh);
  809. if (error)
  810. return error;
  811. oleaf = (struct gfs2_leaf *)obh->b_data;
  812. if (dip->i_di.di_depth == be16_to_cpu(oleaf->lf_depth)) {
  813. brelse(obh);
  814. return 1; /* can't split */
  815. }
  816. gfs2_trans_add_bh(dip->i_gl, obh, 1);
  817. nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1);
  818. if (!nleaf) {
  819. brelse(obh);
  820. return -ENOSPC;
  821. }
  822. bn = nbh->b_blocknr;
  823. /* Compute the start and len of leaf pointers in the hash table. */
  824. len = 1 << (dip->i_di.di_depth - be16_to_cpu(oleaf->lf_depth));
  825. half_len = len >> 1;
  826. if (!half_len) {
  827. printk(KERN_WARNING "di_depth %u lf_depth %u index %u\n", dip->i_di.di_depth, be16_to_cpu(oleaf->lf_depth), index);
  828. gfs2_consist_inode(dip);
  829. error = -EIO;
  830. goto fail_brelse;
  831. }
  832. start = (index & ~(len - 1));
  833. /* Change the pointers.
  834. Don't bother distinguishing stuffed from non-stuffed.
  835. This code is complicated enough already. */
  836. lp = kmalloc(half_len * sizeof(u64), GFP_NOFS | __GFP_NOFAIL);
  837. /* Change the pointers */
  838. for (x = 0; x < half_len; x++)
  839. lp[x] = cpu_to_be64(bn);
  840. error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64),
  841. half_len * sizeof(u64));
  842. if (error != half_len * sizeof(u64)) {
  843. if (error >= 0)
  844. error = -EIO;
  845. goto fail_lpfree;
  846. }
  847. kfree(lp);
  848. /* Compute the divider */
  849. divider = (start + half_len) << (32 - dip->i_di.di_depth);
  850. /* Copy the entries */
  851. dirent_first(dip, obh, &dent);
  852. do {
  853. next = dent;
  854. if (dirent_next(dip, obh, &next))
  855. next = NULL;
  856. if (dent->de_inum.no_addr &&
  857. be32_to_cpu(dent->de_hash) < divider) {
  858. struct qstr str;
  859. str.name = (char*)(dent+1);
  860. str.len = be16_to_cpu(dent->de_name_len);
  861. str.hash = be32_to_cpu(dent->de_hash);
  862. new = gfs2_dirent_alloc(inode, nbh, &str);
  863. if (IS_ERR(new)) {
  864. error = PTR_ERR(new);
  865. break;
  866. }
  867. new->de_inum = dent->de_inum; /* No endian worries */
  868. new->de_type = dent->de_type; /* No endian worries */
  869. nleaf->lf_entries = cpu_to_be16(be16_to_cpu(nleaf->lf_entries)+1);
  870. dirent_del(dip, obh, prev, dent);
  871. if (!oleaf->lf_entries)
  872. gfs2_consist_inode(dip);
  873. oleaf->lf_entries = cpu_to_be16(be16_to_cpu(oleaf->lf_entries)-1);
  874. if (!prev)
  875. prev = dent;
  876. moved = 1;
  877. } else {
  878. prev = dent;
  879. }
  880. dent = next;
  881. } while (dent);
  882. oleaf->lf_depth = nleaf->lf_depth;
  883. error = gfs2_meta_inode_buffer(dip, &dibh);
  884. if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) {
  885. dip->i_di.di_blocks++;
  886. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  887. brelse(dibh);
  888. }
  889. brelse(obh);
  890. brelse(nbh);
  891. return error;
  892. fail_lpfree:
  893. kfree(lp);
  894. fail_brelse:
  895. brelse(obh);
  896. brelse(nbh);
  897. return error;
  898. }
  899. /**
  900. * dir_double_exhash - Double size of ExHash table
  901. * @dip: The GFS2 dinode
  902. *
  903. * Returns: 0 on success, error code on failure
  904. */
  905. static int dir_double_exhash(struct gfs2_inode *dip)
  906. {
  907. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  908. struct buffer_head *dibh;
  909. u32 hsize;
  910. u64 *buf;
  911. u64 *from, *to;
  912. u64 block;
  913. int x;
  914. int error = 0;
  915. hsize = 1 << dip->i_di.di_depth;
  916. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  917. gfs2_consist_inode(dip);
  918. return -EIO;
  919. }
  920. /* Allocate both the "from" and "to" buffers in one big chunk */
  921. buf = kcalloc(3, sdp->sd_hash_bsize, GFP_KERNEL | __GFP_NOFAIL);
  922. for (block = dip->i_di.di_size >> sdp->sd_hash_bsize_shift; block--;) {
  923. error = gfs2_dir_read_data(dip, (char *)buf,
  924. block * sdp->sd_hash_bsize,
  925. sdp->sd_hash_bsize, 1);
  926. if (error != sdp->sd_hash_bsize) {
  927. if (error >= 0)
  928. error = -EIO;
  929. goto fail;
  930. }
  931. from = buf;
  932. to = (u64 *)((char *)buf + sdp->sd_hash_bsize);
  933. for (x = sdp->sd_hash_ptrs; x--; from++) {
  934. *to++ = *from; /* No endianess worries */
  935. *to++ = *from;
  936. }
  937. error = gfs2_dir_write_data(dip,
  938. (char *)buf + sdp->sd_hash_bsize,
  939. block * sdp->sd_sb.sb_bsize,
  940. sdp->sd_sb.sb_bsize);
  941. if (error != sdp->sd_sb.sb_bsize) {
  942. if (error >= 0)
  943. error = -EIO;
  944. goto fail;
  945. }
  946. }
  947. kfree(buf);
  948. error = gfs2_meta_inode_buffer(dip, &dibh);
  949. if (!gfs2_assert_withdraw(sdp, !error)) {
  950. dip->i_di.di_depth++;
  951. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  952. brelse(dibh);
  953. }
  954. return error;
  955. fail:
  956. kfree(buf);
  957. return error;
  958. }
  959. /**
  960. * compare_dents - compare directory entries by hash value
  961. * @a: first dent
  962. * @b: second dent
  963. *
  964. * When comparing the hash entries of @a to @b:
  965. * gt: returns 1
  966. * lt: returns -1
  967. * eq: returns 0
  968. */
  969. static int compare_dents(const void *a, const void *b)
  970. {
  971. const struct gfs2_dirent *dent_a, *dent_b;
  972. u32 hash_a, hash_b;
  973. int ret = 0;
  974. dent_a = *(const struct gfs2_dirent **)a;
  975. hash_a = be32_to_cpu(dent_a->de_hash);
  976. dent_b = *(const struct gfs2_dirent **)b;
  977. hash_b = be32_to_cpu(dent_b->de_hash);
  978. if (hash_a > hash_b)
  979. ret = 1;
  980. else if (hash_a < hash_b)
  981. ret = -1;
  982. else {
  983. unsigned int len_a = be16_to_cpu(dent_a->de_name_len);
  984. unsigned int len_b = be16_to_cpu(dent_b->de_name_len);
  985. if (len_a > len_b)
  986. ret = 1;
  987. else if (len_a < len_b)
  988. ret = -1;
  989. else
  990. ret = memcmp(dent_a + 1, dent_b + 1, len_a);
  991. }
  992. return ret;
  993. }
  994. /**
  995. * do_filldir_main - read out directory entries
  996. * @dip: The GFS2 inode
  997. * @offset: The offset in the file to read from
  998. * @opaque: opaque data to pass to filldir
  999. * @filldir: The function to pass entries to
  1000. * @darr: an array of struct gfs2_dirent pointers to read
  1001. * @entries: the number of entries in darr
  1002. * @copied: pointer to int that's non-zero if a entry has been copied out
  1003. *
  1004. * Jump through some hoops to make sure that if there are hash collsions,
  1005. * they are read out at the beginning of a buffer. We want to minimize
  1006. * the possibility that they will fall into different readdir buffers or
  1007. * that someone will want to seek to that location.
  1008. *
  1009. * Returns: errno, >0 on exception from filldir
  1010. */
  1011. static int do_filldir_main(struct gfs2_inode *dip, u64 *offset,
  1012. void *opaque, gfs2_filldir_t filldir,
  1013. const struct gfs2_dirent **darr, u32 entries,
  1014. int *copied)
  1015. {
  1016. const struct gfs2_dirent *dent, *dent_next;
  1017. struct gfs2_inum inum;
  1018. u64 off, off_next;
  1019. unsigned int x, y;
  1020. int run = 0;
  1021. int error = 0;
  1022. sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL);
  1023. dent_next = darr[0];
  1024. off_next = be32_to_cpu(dent_next->de_hash);
  1025. off_next = gfs2_disk_hash2offset(off_next);
  1026. for (x = 0, y = 1; x < entries; x++, y++) {
  1027. dent = dent_next;
  1028. off = off_next;
  1029. if (y < entries) {
  1030. dent_next = darr[y];
  1031. off_next = be32_to_cpu(dent_next->de_hash);
  1032. off_next = gfs2_disk_hash2offset(off_next);
  1033. if (off < *offset)
  1034. continue;
  1035. *offset = off;
  1036. if (off_next == off) {
  1037. if (*copied && !run)
  1038. return 1;
  1039. run = 1;
  1040. } else
  1041. run = 0;
  1042. } else {
  1043. if (off < *offset)
  1044. continue;
  1045. *offset = off;
  1046. }
  1047. gfs2_inum_in(&inum, (char *)&dent->de_inum);
  1048. error = filldir(opaque, (const char *)(dent + 1),
  1049. be16_to_cpu(dent->de_name_len),
  1050. off, &inum,
  1051. be16_to_cpu(dent->de_type));
  1052. if (error)
  1053. return 1;
  1054. *copied = 1;
  1055. }
  1056. /* Increment the *offset by one, so the next time we come into the
  1057. do_filldir fxn, we get the next entry instead of the last one in the
  1058. current leaf */
  1059. (*offset)++;
  1060. return 0;
  1061. }
  1062. static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque,
  1063. gfs2_filldir_t filldir, int *copied,
  1064. unsigned *depth, u64 leaf_no)
  1065. {
  1066. struct gfs2_inode *ip = GFS2_I(inode);
  1067. struct buffer_head *bh;
  1068. struct gfs2_leaf *lf;
  1069. unsigned entries = 0;
  1070. unsigned leaves = 0;
  1071. const struct gfs2_dirent **darr, *dent;
  1072. struct dirent_gather g;
  1073. struct buffer_head **larr;
  1074. int leaf = 0;
  1075. int error, i;
  1076. u64 lfn = leaf_no;
  1077. do {
  1078. error = get_leaf(ip, lfn, &bh);
  1079. if (error)
  1080. goto out;
  1081. lf = (struct gfs2_leaf *)bh->b_data;
  1082. if (leaves == 0)
  1083. *depth = be16_to_cpu(lf->lf_depth);
  1084. entries += be16_to_cpu(lf->lf_entries);
  1085. leaves++;
  1086. lfn = be64_to_cpu(lf->lf_next);
  1087. brelse(bh);
  1088. } while(lfn);
  1089. if (!entries)
  1090. return 0;
  1091. error = -ENOMEM;
  1092. larr = vmalloc((leaves + entries) * sizeof(void *));
  1093. if (!larr)
  1094. goto out;
  1095. darr = (const struct gfs2_dirent **)(larr + leaves);
  1096. g.pdent = darr;
  1097. g.offset = 0;
  1098. lfn = leaf_no;
  1099. do {
  1100. error = get_leaf(ip, lfn, &bh);
  1101. if (error)
  1102. goto out_kfree;
  1103. lf = (struct gfs2_leaf *)bh->b_data;
  1104. lfn = be64_to_cpu(lf->lf_next);
  1105. if (lf->lf_entries) {
  1106. dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size,
  1107. gfs2_dirent_gather, NULL, &g);
  1108. error = PTR_ERR(dent);
  1109. if (IS_ERR(dent)) {
  1110. goto out_kfree;
  1111. }
  1112. error = 0;
  1113. larr[leaf++] = bh;
  1114. } else {
  1115. brelse(bh);
  1116. }
  1117. } while(lfn);
  1118. error = do_filldir_main(ip, offset, opaque, filldir, darr,
  1119. entries, copied);
  1120. out_kfree:
  1121. for(i = 0; i < leaf; i++)
  1122. brelse(larr[i]);
  1123. vfree(larr);
  1124. out:
  1125. return error;
  1126. }
  1127. /**
  1128. * dir_e_read - Reads the entries from a directory into a filldir buffer
  1129. * @dip: dinode pointer
  1130. * @offset: the hash of the last entry read shifted to the right once
  1131. * @opaque: buffer for the filldir function to fill
  1132. * @filldir: points to the filldir function to use
  1133. *
  1134. * Returns: errno
  1135. */
  1136. static int dir_e_read(struct inode *inode, u64 *offset, void *opaque,
  1137. gfs2_filldir_t filldir)
  1138. {
  1139. struct gfs2_inode *dip = GFS2_I(inode);
  1140. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1141. u32 hsize, len = 0;
  1142. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1143. u32 hash, index;
  1144. u64 *lp;
  1145. int copied = 0;
  1146. int error = 0;
  1147. unsigned depth = 0;
  1148. hsize = 1 << dip->i_di.di_depth;
  1149. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  1150. gfs2_consist_inode(dip);
  1151. return -EIO;
  1152. }
  1153. hash = gfs2_dir_offset2hash(*offset);
  1154. index = hash >> (32 - dip->i_di.di_depth);
  1155. lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
  1156. if (!lp)
  1157. return -ENOMEM;
  1158. while (index < hsize) {
  1159. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1160. ht_offset = index - lp_offset;
  1161. if (ht_offset_cur != ht_offset) {
  1162. error = gfs2_dir_read_data(dip, (char *)lp,
  1163. ht_offset * sizeof(u64),
  1164. sdp->sd_hash_bsize, 1);
  1165. if (error != sdp->sd_hash_bsize) {
  1166. if (error >= 0)
  1167. error = -EIO;
  1168. goto out;
  1169. }
  1170. ht_offset_cur = ht_offset;
  1171. }
  1172. error = gfs2_dir_read_leaf(inode, offset, opaque, filldir,
  1173. &copied, &depth,
  1174. be64_to_cpu(lp[lp_offset]));
  1175. if (error)
  1176. break;
  1177. len = 1 << (dip->i_di.di_depth - depth);
  1178. index = (index & ~(len - 1)) + len;
  1179. }
  1180. out:
  1181. kfree(lp);
  1182. if (error > 0)
  1183. error = 0;
  1184. return error;
  1185. }
  1186. int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque,
  1187. gfs2_filldir_t filldir)
  1188. {
  1189. struct gfs2_inode *dip = GFS2_I(inode);
  1190. struct dirent_gather g;
  1191. const struct gfs2_dirent **darr, *dent;
  1192. struct buffer_head *dibh;
  1193. int copied = 0;
  1194. int error;
  1195. if (!dip->i_di.di_entries)
  1196. return 0;
  1197. if (dip->i_di.di_flags & GFS2_DIF_EXHASH)
  1198. return dir_e_read(inode, offset, opaque, filldir);
  1199. if (!gfs2_is_stuffed(dip)) {
  1200. gfs2_consist_inode(dip);
  1201. return -EIO;
  1202. }
  1203. error = gfs2_meta_inode_buffer(dip, &dibh);
  1204. if (error)
  1205. return error;
  1206. error = -ENOMEM;
  1207. darr = kmalloc(dip->i_di.di_entries * sizeof(struct gfs2_dirent *),
  1208. GFP_KERNEL);
  1209. if (darr) {
  1210. g.pdent = darr;
  1211. g.offset = 0;
  1212. dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size,
  1213. gfs2_dirent_gather, NULL, &g);
  1214. if (IS_ERR(dent)) {
  1215. error = PTR_ERR(dent);
  1216. goto out;
  1217. }
  1218. error = do_filldir_main(dip, offset, opaque, filldir, darr,
  1219. dip->i_di.di_entries, &copied);
  1220. out:
  1221. kfree(darr);
  1222. }
  1223. if (error > 0)
  1224. error = 0;
  1225. brelse(dibh);
  1226. return error;
  1227. }
  1228. /**
  1229. * gfs2_dir_search - Search a directory
  1230. * @dip: The GFS2 inode
  1231. * @filename:
  1232. * @inode:
  1233. *
  1234. * This routine searches a directory for a file or another directory.
  1235. * Assumes a glock is held on dip.
  1236. *
  1237. * Returns: errno
  1238. */
  1239. int gfs2_dir_search(struct inode *dir, const struct qstr *name,
  1240. struct gfs2_inum *inum, unsigned int *type)
  1241. {
  1242. struct buffer_head *bh;
  1243. struct gfs2_dirent *dent;
  1244. dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh);
  1245. if (dent) {
  1246. if (IS_ERR(dent))
  1247. return PTR_ERR(dent);
  1248. if (inum)
  1249. gfs2_inum_in(inum, (char *)&dent->de_inum);
  1250. if (type)
  1251. *type = be16_to_cpu(dent->de_type);
  1252. brelse(bh);
  1253. return 0;
  1254. }
  1255. return -ENOENT;
  1256. }
  1257. static int dir_new_leaf(struct inode *inode, const struct qstr *name)
  1258. {
  1259. struct buffer_head *bh, *obh;
  1260. struct gfs2_inode *ip = GFS2_I(inode);
  1261. struct gfs2_leaf *leaf, *oleaf;
  1262. int error;
  1263. u32 index;
  1264. u64 bn;
  1265. index = name->hash >> (32 - ip->i_di.di_depth);
  1266. error = get_first_leaf(ip, index, &obh);
  1267. if (error)
  1268. return error;
  1269. do {
  1270. oleaf = (struct gfs2_leaf *)obh->b_data;
  1271. bn = be64_to_cpu(oleaf->lf_next);
  1272. if (!bn)
  1273. break;
  1274. brelse(obh);
  1275. error = get_leaf(ip, bn, &obh);
  1276. if (error)
  1277. return error;
  1278. } while(1);
  1279. gfs2_trans_add_bh(ip->i_gl, obh, 1);
  1280. leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth));
  1281. if (!leaf) {
  1282. brelse(obh);
  1283. return -ENOSPC;
  1284. }
  1285. oleaf->lf_next = cpu_to_be64(bh->b_blocknr);
  1286. brelse(bh);
  1287. brelse(obh);
  1288. error = gfs2_meta_inode_buffer(ip, &bh);
  1289. if (error)
  1290. return error;
  1291. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1292. ip->i_di.di_blocks++;
  1293. gfs2_dinode_out(&ip->i_di, bh->b_data);
  1294. brelse(bh);
  1295. return 0;
  1296. }
  1297. /**
  1298. * gfs2_dir_add - Add new filename into directory
  1299. * @dip: The GFS2 inode
  1300. * @filename: The new name
  1301. * @inode: The inode number of the entry
  1302. * @type: The type of the entry
  1303. *
  1304. * Returns: 0 on success, error code on failure
  1305. */
  1306. int gfs2_dir_add(struct inode *inode, const struct qstr *name,
  1307. const struct gfs2_inum *inum, unsigned type)
  1308. {
  1309. struct gfs2_inode *ip = GFS2_I(inode);
  1310. struct buffer_head *bh;
  1311. struct gfs2_dirent *dent;
  1312. struct gfs2_leaf *leaf;
  1313. int error;
  1314. while(1) {
  1315. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space,
  1316. &bh);
  1317. if (dent) {
  1318. if (IS_ERR(dent))
  1319. return PTR_ERR(dent);
  1320. dent = gfs2_init_dirent(inode, dent, name, bh);
  1321. gfs2_inum_out(inum, (char *)&dent->de_inum);
  1322. dent->de_type = cpu_to_be16(type);
  1323. if (ip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1324. leaf = (struct gfs2_leaf *)bh->b_data;
  1325. leaf->lf_entries = cpu_to_be16(be16_to_cpu(leaf->lf_entries) + 1);
  1326. }
  1327. brelse(bh);
  1328. error = gfs2_meta_inode_buffer(ip, &bh);
  1329. if (error)
  1330. break;
  1331. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  1332. ip->i_di.di_entries++;
  1333. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  1334. gfs2_dinode_out(&ip->i_di, bh->b_data);
  1335. brelse(bh);
  1336. error = 0;
  1337. break;
  1338. }
  1339. if (!(ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
  1340. error = dir_make_exhash(inode);
  1341. if (error)
  1342. break;
  1343. continue;
  1344. }
  1345. error = dir_split_leaf(inode, name);
  1346. if (error == 0)
  1347. continue;
  1348. if (error < 0)
  1349. break;
  1350. if (ip->i_di.di_depth < GFS2_DIR_MAX_DEPTH) {
  1351. error = dir_double_exhash(ip);
  1352. if (error)
  1353. break;
  1354. error = dir_split_leaf(inode, name);
  1355. if (error < 0)
  1356. break;
  1357. if (error == 0)
  1358. continue;
  1359. }
  1360. error = dir_new_leaf(inode, name);
  1361. if (!error)
  1362. continue;
  1363. error = -ENOSPC;
  1364. break;
  1365. }
  1366. return error;
  1367. }
  1368. /**
  1369. * gfs2_dir_del - Delete a directory entry
  1370. * @dip: The GFS2 inode
  1371. * @filename: The filename
  1372. *
  1373. * Returns: 0 on success, error code on failure
  1374. */
  1375. int gfs2_dir_del(struct gfs2_inode *dip, const struct qstr *name)
  1376. {
  1377. struct gfs2_dirent *dent, *prev = NULL;
  1378. struct buffer_head *bh;
  1379. int error;
  1380. /* Returns _either_ the entry (if its first in block) or the
  1381. previous entry otherwise */
  1382. dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh);
  1383. if (!dent) {
  1384. gfs2_consist_inode(dip);
  1385. return -EIO;
  1386. }
  1387. if (IS_ERR(dent)) {
  1388. gfs2_consist_inode(dip);
  1389. return PTR_ERR(dent);
  1390. }
  1391. /* If not first in block, adjust pointers accordingly */
  1392. if (gfs2_dirent_find(dent, name, NULL) == 0) {
  1393. prev = dent;
  1394. dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len));
  1395. }
  1396. dirent_del(dip, bh, prev, dent);
  1397. if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1398. struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data;
  1399. u16 entries = be16_to_cpu(leaf->lf_entries);
  1400. if (!entries)
  1401. gfs2_consist_inode(dip);
  1402. leaf->lf_entries = cpu_to_be16(--entries);
  1403. }
  1404. brelse(bh);
  1405. error = gfs2_meta_inode_buffer(dip, &bh);
  1406. if (error)
  1407. return error;
  1408. if (!dip->i_di.di_entries)
  1409. gfs2_consist_inode(dip);
  1410. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1411. dip->i_di.di_entries--;
  1412. dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
  1413. gfs2_dinode_out(&dip->i_di, bh->b_data);
  1414. brelse(bh);
  1415. mark_inode_dirty(&dip->i_inode);
  1416. return error;
  1417. }
  1418. /**
  1419. * gfs2_dir_mvino - Change inode number of directory entry
  1420. * @dip: The GFS2 inode
  1421. * @filename:
  1422. * @new_inode:
  1423. *
  1424. * This routine changes the inode number of a directory entry. It's used
  1425. * by rename to change ".." when a directory is moved.
  1426. * Assumes a glock is held on dvp.
  1427. *
  1428. * Returns: errno
  1429. */
  1430. int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename,
  1431. struct gfs2_inum *inum, unsigned int new_type)
  1432. {
  1433. struct buffer_head *bh;
  1434. struct gfs2_dirent *dent;
  1435. int error;
  1436. dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh);
  1437. if (!dent) {
  1438. gfs2_consist_inode(dip);
  1439. return -EIO;
  1440. }
  1441. if (IS_ERR(dent))
  1442. return PTR_ERR(dent);
  1443. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1444. gfs2_inum_out(inum, (char *)&dent->de_inum);
  1445. dent->de_type = cpu_to_be16(new_type);
  1446. if (dip->i_di.di_flags & GFS2_DIF_EXHASH) {
  1447. brelse(bh);
  1448. error = gfs2_meta_inode_buffer(dip, &bh);
  1449. if (error)
  1450. return error;
  1451. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1452. }
  1453. dip->i_di.di_mtime = dip->i_di.di_ctime = get_seconds();
  1454. gfs2_dinode_out(&dip->i_di, bh->b_data);
  1455. brelse(bh);
  1456. return 0;
  1457. }
  1458. /**
  1459. * foreach_leaf - call a function for each leaf in a directory
  1460. * @dip: the directory
  1461. * @lc: the function to call for each each
  1462. * @data: private data to pass to it
  1463. *
  1464. * Returns: errno
  1465. */
  1466. static int foreach_leaf(struct gfs2_inode *dip, leaf_call_t lc, void *data)
  1467. {
  1468. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1469. struct buffer_head *bh;
  1470. struct gfs2_leaf *leaf;
  1471. u32 hsize, len;
  1472. u32 ht_offset, lp_offset, ht_offset_cur = -1;
  1473. u32 index = 0;
  1474. u64 *lp;
  1475. u64 leaf_no;
  1476. int error = 0;
  1477. hsize = 1 << dip->i_di.di_depth;
  1478. if (hsize * sizeof(u64) != dip->i_di.di_size) {
  1479. gfs2_consist_inode(dip);
  1480. return -EIO;
  1481. }
  1482. lp = kmalloc(sdp->sd_hash_bsize, GFP_KERNEL);
  1483. if (!lp)
  1484. return -ENOMEM;
  1485. while (index < hsize) {
  1486. lp_offset = index & (sdp->sd_hash_ptrs - 1);
  1487. ht_offset = index - lp_offset;
  1488. if (ht_offset_cur != ht_offset) {
  1489. error = gfs2_dir_read_data(dip, (char *)lp,
  1490. ht_offset * sizeof(u64),
  1491. sdp->sd_hash_bsize, 1);
  1492. if (error != sdp->sd_hash_bsize) {
  1493. if (error >= 0)
  1494. error = -EIO;
  1495. goto out;
  1496. }
  1497. ht_offset_cur = ht_offset;
  1498. }
  1499. leaf_no = be64_to_cpu(lp[lp_offset]);
  1500. if (leaf_no) {
  1501. error = get_leaf(dip, leaf_no, &bh);
  1502. if (error)
  1503. goto out;
  1504. leaf = (struct gfs2_leaf *)bh->b_data;
  1505. len = 1 << (dip->i_di.di_depth - be16_to_cpu(leaf->lf_depth));
  1506. brelse(bh);
  1507. error = lc(dip, index, len, leaf_no, data);
  1508. if (error)
  1509. goto out;
  1510. index = (index & ~(len - 1)) + len;
  1511. } else
  1512. index++;
  1513. }
  1514. if (index != hsize) {
  1515. gfs2_consist_inode(dip);
  1516. error = -EIO;
  1517. }
  1518. out:
  1519. kfree(lp);
  1520. return error;
  1521. }
  1522. /**
  1523. * leaf_dealloc - Deallocate a directory leaf
  1524. * @dip: the directory
  1525. * @index: the hash table offset in the directory
  1526. * @len: the number of pointers to this leaf
  1527. * @leaf_no: the leaf number
  1528. * @data: not used
  1529. *
  1530. * Returns: errno
  1531. */
  1532. static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len,
  1533. u64 leaf_no, void *data)
  1534. {
  1535. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1536. struct gfs2_leaf *tmp_leaf;
  1537. struct gfs2_rgrp_list rlist;
  1538. struct buffer_head *bh, *dibh;
  1539. u64 blk, nblk;
  1540. unsigned int rg_blocks = 0, l_blocks = 0;
  1541. char *ht;
  1542. unsigned int x, size = len * sizeof(u64);
  1543. int error;
  1544. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  1545. ht = kzalloc(size, GFP_KERNEL);
  1546. if (!ht)
  1547. return -ENOMEM;
  1548. gfs2_alloc_get(dip);
  1549. error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  1550. if (error)
  1551. goto out;
  1552. error = gfs2_rindex_hold(sdp, &dip->i_alloc.al_ri_gh);
  1553. if (error)
  1554. goto out_qs;
  1555. /* Count the number of leaves */
  1556. for (blk = leaf_no; blk; blk = nblk) {
  1557. error = get_leaf(dip, blk, &bh);
  1558. if (error)
  1559. goto out_rlist;
  1560. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1561. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1562. brelse(bh);
  1563. gfs2_rlist_add(sdp, &rlist, blk);
  1564. l_blocks++;
  1565. }
  1566. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  1567. for (x = 0; x < rlist.rl_rgrps; x++) {
  1568. struct gfs2_rgrpd *rgd;
  1569. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  1570. rg_blocks += rgd->rd_ri.ri_length;
  1571. }
  1572. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1573. if (error)
  1574. goto out_rlist;
  1575. error = gfs2_trans_begin(sdp,
  1576. rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) +
  1577. RES_DINODE + RES_STATFS + RES_QUOTA, l_blocks);
  1578. if (error)
  1579. goto out_rg_gunlock;
  1580. for (blk = leaf_no; blk; blk = nblk) {
  1581. error = get_leaf(dip, blk, &bh);
  1582. if (error)
  1583. goto out_end_trans;
  1584. tmp_leaf = (struct gfs2_leaf *)bh->b_data;
  1585. nblk = be64_to_cpu(tmp_leaf->lf_next);
  1586. brelse(bh);
  1587. gfs2_free_meta(dip, blk, 1);
  1588. if (!dip->i_di.di_blocks)
  1589. gfs2_consist_inode(dip);
  1590. dip->i_di.di_blocks--;
  1591. }
  1592. error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size);
  1593. if (error != size) {
  1594. if (error >= 0)
  1595. error = -EIO;
  1596. goto out_end_trans;
  1597. }
  1598. error = gfs2_meta_inode_buffer(dip, &dibh);
  1599. if (error)
  1600. goto out_end_trans;
  1601. gfs2_trans_add_bh(dip->i_gl, dibh, 1);
  1602. gfs2_dinode_out(&dip->i_di, dibh->b_data);
  1603. brelse(dibh);
  1604. out_end_trans:
  1605. gfs2_trans_end(sdp);
  1606. out_rg_gunlock:
  1607. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  1608. out_rlist:
  1609. gfs2_rlist_free(&rlist);
  1610. gfs2_glock_dq_uninit(&dip->i_alloc.al_ri_gh);
  1611. out_qs:
  1612. gfs2_quota_unhold(dip);
  1613. out:
  1614. gfs2_alloc_put(dip);
  1615. kfree(ht);
  1616. return error;
  1617. }
  1618. /**
  1619. * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory
  1620. * @dip: the directory
  1621. *
  1622. * Dealloc all on-disk directory leaves to FREEMETA state
  1623. * Change on-disk inode type to "regular file"
  1624. *
  1625. * Returns: errno
  1626. */
  1627. int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip)
  1628. {
  1629. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1630. struct buffer_head *bh;
  1631. int error;
  1632. /* Dealloc on-disk leaves to FREEMETA state */
  1633. error = foreach_leaf(dip, leaf_dealloc, NULL);
  1634. if (error)
  1635. return error;
  1636. /* Make this a regular file in case we crash.
  1637. (We don't want to free these blocks a second time.) */
  1638. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  1639. if (error)
  1640. return error;
  1641. error = gfs2_meta_inode_buffer(dip, &bh);
  1642. if (!error) {
  1643. gfs2_trans_add_bh(dip->i_gl, bh, 1);
  1644. ((struct gfs2_dinode *)bh->b_data)->di_mode =
  1645. cpu_to_be32(S_IFREG);
  1646. brelse(bh);
  1647. }
  1648. gfs2_trans_end(sdp);
  1649. return error;
  1650. }
  1651. /**
  1652. * gfs2_diradd_alloc_required - find if adding entry will require an allocation
  1653. * @ip: the file being written to
  1654. * @filname: the filename that's going to be added
  1655. *
  1656. * Returns: 1 if alloc required, 0 if not, -ve on error
  1657. */
  1658. int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name)
  1659. {
  1660. struct gfs2_dirent *dent;
  1661. struct buffer_head *bh;
  1662. dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh);
  1663. if (!dent) {
  1664. return 1;
  1665. }
  1666. if (IS_ERR(dent))
  1667. return PTR_ERR(dent);
  1668. brelse(bh);
  1669. return 0;
  1670. }