dir.c 46 KB

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