namei.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. /*
  2. * linux/fs/ext4/namei.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/namei.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. * Directory entry file type support and forward compatibility hooks
  18. * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998
  19. * Hash Tree Directory indexing (c)
  20. * Daniel Phillips, 2001
  21. * Hash Tree Directory indexing porting
  22. * Christopher Li, 2002
  23. * Hash Tree Directory indexing cleanup
  24. * Theodore Ts'o, 2002
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/jbd2.h>
  29. #include <linux/time.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/quotaops.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/bio.h>
  36. #include "ext4.h"
  37. #include "ext4_jbd2.h"
  38. #include "namei.h"
  39. #include "xattr.h"
  40. #include "acl.h"
  41. /*
  42. * define how far ahead to read directories while searching them.
  43. */
  44. #define NAMEI_RA_CHUNKS 2
  45. #define NAMEI_RA_BLOCKS 4
  46. #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
  47. #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b))
  48. static struct buffer_head *ext4_append(handle_t *handle,
  49. struct inode *inode,
  50. ext4_lblk_t *block, int *err)
  51. {
  52. struct buffer_head *bh;
  53. *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
  54. bh = ext4_bread(handle, inode, *block, 1, err);
  55. if (bh) {
  56. inode->i_size += inode->i_sb->s_blocksize;
  57. EXT4_I(inode)->i_disksize = inode->i_size;
  58. *err = ext4_journal_get_write_access(handle, bh);
  59. if (*err) {
  60. brelse(bh);
  61. bh = NULL;
  62. }
  63. }
  64. return bh;
  65. }
  66. #ifndef assert
  67. #define assert(test) J_ASSERT(test)
  68. #endif
  69. #ifdef DX_DEBUG
  70. #define dxtrace(command) command
  71. #else
  72. #define dxtrace(command)
  73. #endif
  74. struct fake_dirent
  75. {
  76. __le32 inode;
  77. __le16 rec_len;
  78. u8 name_len;
  79. u8 file_type;
  80. };
  81. struct dx_countlimit
  82. {
  83. __le16 limit;
  84. __le16 count;
  85. };
  86. struct dx_entry
  87. {
  88. __le32 hash;
  89. __le32 block;
  90. };
  91. /*
  92. * dx_root_info is laid out so that if it should somehow get overlaid by a
  93. * dirent the two low bits of the hash version will be zero. Therefore, the
  94. * hash version mod 4 should never be 0. Sincerely, the paranoia department.
  95. */
  96. struct dx_root
  97. {
  98. struct fake_dirent dot;
  99. char dot_name[4];
  100. struct fake_dirent dotdot;
  101. char dotdot_name[4];
  102. struct dx_root_info
  103. {
  104. __le32 reserved_zero;
  105. u8 hash_version;
  106. u8 info_length; /* 8 */
  107. u8 indirect_levels;
  108. u8 unused_flags;
  109. }
  110. info;
  111. struct dx_entry entries[0];
  112. };
  113. struct dx_node
  114. {
  115. struct fake_dirent fake;
  116. struct dx_entry entries[0];
  117. };
  118. struct dx_frame
  119. {
  120. struct buffer_head *bh;
  121. struct dx_entry *entries;
  122. struct dx_entry *at;
  123. };
  124. struct dx_map_entry
  125. {
  126. u32 hash;
  127. u16 offs;
  128. u16 size;
  129. };
  130. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
  131. static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
  132. static inline unsigned dx_get_hash(struct dx_entry *entry);
  133. static void dx_set_hash(struct dx_entry *entry, unsigned value);
  134. static unsigned dx_get_count(struct dx_entry *entries);
  135. static unsigned dx_get_limit(struct dx_entry *entries);
  136. static void dx_set_count(struct dx_entry *entries, unsigned value);
  137. static void dx_set_limit(struct dx_entry *entries, unsigned value);
  138. static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
  139. static unsigned dx_node_limit(struct inode *dir);
  140. static struct dx_frame *dx_probe(const struct qstr *d_name,
  141. struct inode *dir,
  142. struct dx_hash_info *hinfo,
  143. struct dx_frame *frame,
  144. int *err);
  145. static void dx_release(struct dx_frame *frames);
  146. static int dx_make_map(struct ext4_dir_entry_2 *de, int size,
  147. struct dx_hash_info *hinfo, struct dx_map_entry map[]);
  148. static void dx_sort_map(struct dx_map_entry *map, unsigned count);
  149. static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
  150. struct dx_map_entry *offsets, int count);
  151. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size);
  152. static void dx_insert_block(struct dx_frame *frame,
  153. u32 hash, ext4_lblk_t block);
  154. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  155. struct dx_frame *frame,
  156. struct dx_frame *frames,
  157. __u32 *start_hash);
  158. static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
  159. const struct qstr *d_name,
  160. struct ext4_dir_entry_2 **res_dir,
  161. int *err);
  162. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  163. struct inode *inode);
  164. /*
  165. * p is at least 6 bytes before the end of page
  166. */
  167. static inline struct ext4_dir_entry_2 *
  168. ext4_next_entry(struct ext4_dir_entry_2 *p)
  169. {
  170. return (struct ext4_dir_entry_2 *)((char *)p +
  171. ext4_rec_len_from_disk(p->rec_len));
  172. }
  173. /*
  174. * Future: use high four bits of block for coalesce-on-delete flags
  175. * Mask them off for now.
  176. */
  177. static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
  178. {
  179. return le32_to_cpu(entry->block) & 0x00ffffff;
  180. }
  181. static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
  182. {
  183. entry->block = cpu_to_le32(value);
  184. }
  185. static inline unsigned dx_get_hash(struct dx_entry *entry)
  186. {
  187. return le32_to_cpu(entry->hash);
  188. }
  189. static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
  190. {
  191. entry->hash = cpu_to_le32(value);
  192. }
  193. static inline unsigned dx_get_count(struct dx_entry *entries)
  194. {
  195. return le16_to_cpu(((struct dx_countlimit *) entries)->count);
  196. }
  197. static inline unsigned dx_get_limit(struct dx_entry *entries)
  198. {
  199. return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
  200. }
  201. static inline void dx_set_count(struct dx_entry *entries, unsigned value)
  202. {
  203. ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
  204. }
  205. static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
  206. {
  207. ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
  208. }
  209. static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
  210. {
  211. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
  212. EXT4_DIR_REC_LEN(2) - infosize;
  213. return entry_space / sizeof(struct dx_entry);
  214. }
  215. static inline unsigned dx_node_limit(struct inode *dir)
  216. {
  217. unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
  218. return entry_space / sizeof(struct dx_entry);
  219. }
  220. /*
  221. * Debug
  222. */
  223. #ifdef DX_DEBUG
  224. static void dx_show_index(char * label, struct dx_entry *entries)
  225. {
  226. int i, n = dx_get_count (entries);
  227. printk(KERN_DEBUG "%s index ", label);
  228. for (i = 0; i < n; i++) {
  229. printk("%x->%lu ", i ? dx_get_hash(entries + i) :
  230. 0, (unsigned long)dx_get_block(entries + i));
  231. }
  232. printk("\n");
  233. }
  234. struct stats
  235. {
  236. unsigned names;
  237. unsigned space;
  238. unsigned bcount;
  239. };
  240. static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
  241. int size, int show_names)
  242. {
  243. unsigned names = 0, space = 0;
  244. char *base = (char *) de;
  245. struct dx_hash_info h = *hinfo;
  246. printk("names: ");
  247. while ((char *) de < base + size)
  248. {
  249. if (de->inode)
  250. {
  251. if (show_names)
  252. {
  253. int len = de->name_len;
  254. char *name = de->name;
  255. while (len--) printk("%c", *name++);
  256. ext4fs_dirhash(de->name, de->name_len, &h);
  257. printk(":%x.%u ", h.hash,
  258. ((char *) de - base));
  259. }
  260. space += EXT4_DIR_REC_LEN(de->name_len);
  261. names++;
  262. }
  263. de = ext4_next_entry(de);
  264. }
  265. printk("(%i)\n", names);
  266. return (struct stats) { names, space, 1 };
  267. }
  268. struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
  269. struct dx_entry *entries, int levels)
  270. {
  271. unsigned blocksize = dir->i_sb->s_blocksize;
  272. unsigned count = dx_get_count(entries), names = 0, space = 0, i;
  273. unsigned bcount = 0;
  274. struct buffer_head *bh;
  275. int err;
  276. printk("%i indexed blocks...\n", count);
  277. for (i = 0; i < count; i++, entries++)
  278. {
  279. ext4_lblk_t block = dx_get_block(entries);
  280. ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
  281. u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
  282. struct stats stats;
  283. printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
  284. if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
  285. stats = levels?
  286. dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
  287. dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
  288. names += stats.names;
  289. space += stats.space;
  290. bcount += stats.bcount;
  291. brelse(bh);
  292. }
  293. if (bcount)
  294. printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
  295. levels ? "" : " ", names, space/bcount,
  296. (space/bcount)*100/blocksize);
  297. return (struct stats) { names, space, bcount};
  298. }
  299. #endif /* DX_DEBUG */
  300. /*
  301. * Probe for a directory leaf block to search.
  302. *
  303. * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
  304. * error in the directory index, and the caller should fall back to
  305. * searching the directory normally. The callers of dx_probe **MUST**
  306. * check for this error code, and make sure it never gets reflected
  307. * back to userspace.
  308. */
  309. static struct dx_frame *
  310. dx_probe(const struct qstr *d_name, struct inode *dir,
  311. struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
  312. {
  313. unsigned count, indirect;
  314. struct dx_entry *at, *entries, *p, *q, *m;
  315. struct dx_root *root;
  316. struct buffer_head *bh;
  317. struct dx_frame *frame = frame_in;
  318. u32 hash;
  319. frame->bh = NULL;
  320. if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
  321. goto fail;
  322. root = (struct dx_root *) bh->b_data;
  323. if (root->info.hash_version != DX_HASH_TEA &&
  324. root->info.hash_version != DX_HASH_HALF_MD4 &&
  325. root->info.hash_version != DX_HASH_LEGACY) {
  326. ext4_warning(dir->i_sb, __func__,
  327. "Unrecognised inode hash code %d",
  328. root->info.hash_version);
  329. brelse(bh);
  330. *err = ERR_BAD_DX_DIR;
  331. goto fail;
  332. }
  333. hinfo->hash_version = root->info.hash_version;
  334. if (hinfo->hash_version <= DX_HASH_TEA)
  335. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  336. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  337. if (d_name)
  338. ext4fs_dirhash(d_name->name, d_name->len, hinfo);
  339. hash = hinfo->hash;
  340. if (root->info.unused_flags & 1) {
  341. ext4_warning(dir->i_sb, __func__,
  342. "Unimplemented inode hash flags: %#06x",
  343. root->info.unused_flags);
  344. brelse(bh);
  345. *err = ERR_BAD_DX_DIR;
  346. goto fail;
  347. }
  348. if ((indirect = root->info.indirect_levels) > 1) {
  349. ext4_warning(dir->i_sb, __func__,
  350. "Unimplemented inode hash depth: %#06x",
  351. root->info.indirect_levels);
  352. brelse(bh);
  353. *err = ERR_BAD_DX_DIR;
  354. goto fail;
  355. }
  356. entries = (struct dx_entry *) (((char *)&root->info) +
  357. root->info.info_length);
  358. if (dx_get_limit(entries) != dx_root_limit(dir,
  359. root->info.info_length)) {
  360. ext4_warning(dir->i_sb, __func__,
  361. "dx entry: limit != root limit");
  362. brelse(bh);
  363. *err = ERR_BAD_DX_DIR;
  364. goto fail;
  365. }
  366. dxtrace(printk("Look up %x", hash));
  367. while (1)
  368. {
  369. count = dx_get_count(entries);
  370. if (!count || count > dx_get_limit(entries)) {
  371. ext4_warning(dir->i_sb, __func__,
  372. "dx entry: no count or count > limit");
  373. brelse(bh);
  374. *err = ERR_BAD_DX_DIR;
  375. goto fail2;
  376. }
  377. p = entries + 1;
  378. q = entries + count - 1;
  379. while (p <= q)
  380. {
  381. m = p + (q - p)/2;
  382. dxtrace(printk("."));
  383. if (dx_get_hash(m) > hash)
  384. q = m - 1;
  385. else
  386. p = m + 1;
  387. }
  388. if (0) // linear search cross check
  389. {
  390. unsigned n = count - 1;
  391. at = entries;
  392. while (n--)
  393. {
  394. dxtrace(printk(","));
  395. if (dx_get_hash(++at) > hash)
  396. {
  397. at--;
  398. break;
  399. }
  400. }
  401. assert (at == p - 1);
  402. }
  403. at = p - 1;
  404. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  405. frame->bh = bh;
  406. frame->entries = entries;
  407. frame->at = at;
  408. if (!indirect--) return frame;
  409. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  410. goto fail2;
  411. at = entries = ((struct dx_node *) bh->b_data)->entries;
  412. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  413. ext4_warning(dir->i_sb, __func__,
  414. "dx entry: limit != node limit");
  415. brelse(bh);
  416. *err = ERR_BAD_DX_DIR;
  417. goto fail2;
  418. }
  419. frame++;
  420. frame->bh = NULL;
  421. }
  422. fail2:
  423. while (frame >= frame_in) {
  424. brelse(frame->bh);
  425. frame--;
  426. }
  427. fail:
  428. if (*err == ERR_BAD_DX_DIR)
  429. ext4_warning(dir->i_sb, __func__,
  430. "Corrupt dir inode %ld, running e2fsck is "
  431. "recommended.", dir->i_ino);
  432. return NULL;
  433. }
  434. static void dx_release (struct dx_frame *frames)
  435. {
  436. if (frames[0].bh == NULL)
  437. return;
  438. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  439. brelse(frames[1].bh);
  440. brelse(frames[0].bh);
  441. }
  442. /*
  443. * This function increments the frame pointer to search the next leaf
  444. * block, and reads in the necessary intervening nodes if the search
  445. * should be necessary. Whether or not the search is necessary is
  446. * controlled by the hash parameter. If the hash value is even, then
  447. * the search is only continued if the next block starts with that
  448. * hash value. This is used if we are searching for a specific file.
  449. *
  450. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  451. *
  452. * This function returns 1 if the caller should continue to search,
  453. * or 0 if it should not. If there is an error reading one of the
  454. * index blocks, it will a negative error code.
  455. *
  456. * If start_hash is non-null, it will be filled in with the starting
  457. * hash of the next page.
  458. */
  459. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  460. struct dx_frame *frame,
  461. struct dx_frame *frames,
  462. __u32 *start_hash)
  463. {
  464. struct dx_frame *p;
  465. struct buffer_head *bh;
  466. int err, num_frames = 0;
  467. __u32 bhash;
  468. p = frame;
  469. /*
  470. * Find the next leaf page by incrementing the frame pointer.
  471. * If we run out of entries in the interior node, loop around and
  472. * increment pointer in the parent node. When we break out of
  473. * this loop, num_frames indicates the number of interior
  474. * nodes need to be read.
  475. */
  476. while (1) {
  477. if (++(p->at) < p->entries + dx_get_count(p->entries))
  478. break;
  479. if (p == frames)
  480. return 0;
  481. num_frames++;
  482. p--;
  483. }
  484. /*
  485. * If the hash is 1, then continue only if the next page has a
  486. * continuation hash of any value. This is used for readdir
  487. * handling. Otherwise, check to see if the hash matches the
  488. * desired contiuation hash. If it doesn't, return since
  489. * there's no point to read in the successive index pages.
  490. */
  491. bhash = dx_get_hash(p->at);
  492. if (start_hash)
  493. *start_hash = bhash;
  494. if ((hash & 1) == 0) {
  495. if ((bhash & ~1) != hash)
  496. return 0;
  497. }
  498. /*
  499. * If the hash is HASH_NB_ALWAYS, we always go to the next
  500. * block so no check is necessary
  501. */
  502. while (num_frames--) {
  503. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  504. 0, &err)))
  505. return err; /* Failure */
  506. p++;
  507. brelse(p->bh);
  508. p->bh = bh;
  509. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  510. }
  511. return 1;
  512. }
  513. /*
  514. * This function fills a red-black tree with information from a
  515. * directory block. It returns the number directory entries loaded
  516. * into the tree. If there is an error it is returned in err.
  517. */
  518. static int htree_dirblock_to_tree(struct file *dir_file,
  519. struct inode *dir, ext4_lblk_t block,
  520. struct dx_hash_info *hinfo,
  521. __u32 start_hash, __u32 start_minor_hash)
  522. {
  523. struct buffer_head *bh;
  524. struct ext4_dir_entry_2 *de, *top;
  525. int err, count = 0;
  526. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  527. (unsigned long)block));
  528. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  529. return err;
  530. de = (struct ext4_dir_entry_2 *) bh->b_data;
  531. top = (struct ext4_dir_entry_2 *) ((char *) de +
  532. dir->i_sb->s_blocksize -
  533. EXT4_DIR_REC_LEN(0));
  534. for (; de < top; de = ext4_next_entry(de)) {
  535. if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
  536. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  537. +((char *)de - bh->b_data))) {
  538. /* On error, skip the f_pos to the next block. */
  539. dir_file->f_pos = (dir_file->f_pos |
  540. (dir->i_sb->s_blocksize - 1)) + 1;
  541. brelse(bh);
  542. return count;
  543. }
  544. ext4fs_dirhash(de->name, de->name_len, hinfo);
  545. if ((hinfo->hash < start_hash) ||
  546. ((hinfo->hash == start_hash) &&
  547. (hinfo->minor_hash < start_minor_hash)))
  548. continue;
  549. if (de->inode == 0)
  550. continue;
  551. if ((err = ext4_htree_store_dirent(dir_file,
  552. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  553. brelse(bh);
  554. return err;
  555. }
  556. count++;
  557. }
  558. brelse(bh);
  559. return count;
  560. }
  561. /*
  562. * This function fills a red-black tree with information from a
  563. * directory. We start scanning the directory in hash order, starting
  564. * at start_hash and start_minor_hash.
  565. *
  566. * This function returns the number of entries inserted into the tree,
  567. * or a negative error code.
  568. */
  569. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  570. __u32 start_minor_hash, __u32 *next_hash)
  571. {
  572. struct dx_hash_info hinfo;
  573. struct ext4_dir_entry_2 *de;
  574. struct dx_frame frames[2], *frame;
  575. struct inode *dir;
  576. ext4_lblk_t block;
  577. int count = 0;
  578. int ret, err;
  579. __u32 hashval;
  580. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  581. start_hash, start_minor_hash));
  582. dir = dir_file->f_path.dentry->d_inode;
  583. if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
  584. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  585. if (hinfo.hash_version <= DX_HASH_TEA)
  586. hinfo.hash_version +=
  587. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  588. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  589. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  590. start_hash, start_minor_hash);
  591. *next_hash = ~0;
  592. return count;
  593. }
  594. hinfo.hash = start_hash;
  595. hinfo.minor_hash = 0;
  596. frame = dx_probe(NULL, dir, &hinfo, frames, &err);
  597. if (!frame)
  598. return err;
  599. /* Add '.' and '..' from the htree header */
  600. if (!start_hash && !start_minor_hash) {
  601. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  602. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  603. goto errout;
  604. count++;
  605. }
  606. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  607. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  608. de = ext4_next_entry(de);
  609. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  610. goto errout;
  611. count++;
  612. }
  613. while (1) {
  614. block = dx_get_block(frame->at);
  615. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  616. start_hash, start_minor_hash);
  617. if (ret < 0) {
  618. err = ret;
  619. goto errout;
  620. }
  621. count += ret;
  622. hashval = ~0;
  623. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  624. frame, frames, &hashval);
  625. *next_hash = hashval;
  626. if (ret < 0) {
  627. err = ret;
  628. goto errout;
  629. }
  630. /*
  631. * Stop if: (a) there are no more entries, or
  632. * (b) we have inserted at least one entry and the
  633. * next hash value is not a continuation
  634. */
  635. if ((ret == 0) ||
  636. (count && ((hashval & 1) == 0)))
  637. break;
  638. }
  639. dx_release(frames);
  640. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  641. "next hash: %x\n", count, *next_hash));
  642. return count;
  643. errout:
  644. dx_release(frames);
  645. return (err);
  646. }
  647. /*
  648. * Directory block splitting, compacting
  649. */
  650. /*
  651. * Create map of hash values, offsets, and sizes, stored at end of block.
  652. * Returns number of entries mapped.
  653. */
  654. static int dx_make_map (struct ext4_dir_entry_2 *de, int size,
  655. struct dx_hash_info *hinfo, struct dx_map_entry *map_tail)
  656. {
  657. int count = 0;
  658. char *base = (char *) de;
  659. struct dx_hash_info h = *hinfo;
  660. while ((char *) de < base + size)
  661. {
  662. if (de->name_len && de->inode) {
  663. ext4fs_dirhash(de->name, de->name_len, &h);
  664. map_tail--;
  665. map_tail->hash = h.hash;
  666. map_tail->offs = (u16) ((char *) de - base);
  667. map_tail->size = le16_to_cpu(de->rec_len);
  668. count++;
  669. cond_resched();
  670. }
  671. /* XXX: do we need to check rec_len == 0 case? -Chris */
  672. de = ext4_next_entry(de);
  673. }
  674. return count;
  675. }
  676. /* Sort map by hash value */
  677. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  678. {
  679. struct dx_map_entry *p, *q, *top = map + count - 1;
  680. int more;
  681. /* Combsort until bubble sort doesn't suck */
  682. while (count > 2) {
  683. count = count*10/13;
  684. if (count - 9 < 2) /* 9, 10 -> 11 */
  685. count = 11;
  686. for (p = top, q = p - count; q >= map; p--, q--)
  687. if (p->hash < q->hash)
  688. swap(*p, *q);
  689. }
  690. /* Garden variety bubble sort */
  691. do {
  692. more = 0;
  693. q = top;
  694. while (q-- > map) {
  695. if (q[1].hash >= q[0].hash)
  696. continue;
  697. swap(*(q+1), *q);
  698. more = 1;
  699. }
  700. } while(more);
  701. }
  702. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  703. {
  704. struct dx_entry *entries = frame->entries;
  705. struct dx_entry *old = frame->at, *new = old + 1;
  706. int count = dx_get_count(entries);
  707. assert(count < dx_get_limit(entries));
  708. assert(old < entries + count);
  709. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  710. dx_set_hash(new, hash);
  711. dx_set_block(new, block);
  712. dx_set_count(entries, count + 1);
  713. }
  714. static void ext4_update_dx_flag(struct inode *inode)
  715. {
  716. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  717. EXT4_FEATURE_COMPAT_DIR_INDEX))
  718. EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
  719. }
  720. /*
  721. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  722. *
  723. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  724. * `de != NULL' is guaranteed by caller.
  725. */
  726. static inline int ext4_match (int len, const char * const name,
  727. struct ext4_dir_entry_2 * de)
  728. {
  729. if (len != de->name_len)
  730. return 0;
  731. if (!de->inode)
  732. return 0;
  733. return !memcmp(name, de->name, len);
  734. }
  735. /*
  736. * Returns 0 if not found, -1 on failure, and 1 on success
  737. */
  738. static inline int search_dirblock(struct buffer_head *bh,
  739. struct inode *dir,
  740. const struct qstr *d_name,
  741. unsigned int offset,
  742. struct ext4_dir_entry_2 ** res_dir)
  743. {
  744. struct ext4_dir_entry_2 * de;
  745. char * dlimit;
  746. int de_len;
  747. const char *name = d_name->name;
  748. int namelen = d_name->len;
  749. de = (struct ext4_dir_entry_2 *) bh->b_data;
  750. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  751. while ((char *) de < dlimit) {
  752. /* this code is executed quadratically often */
  753. /* do minimal checking `by hand' */
  754. if ((char *) de + namelen <= dlimit &&
  755. ext4_match (namelen, name, de)) {
  756. /* found a match - just to be sure, do a full check */
  757. if (!ext4_check_dir_entry("ext4_find_entry",
  758. dir, de, bh, offset))
  759. return -1;
  760. *res_dir = de;
  761. return 1;
  762. }
  763. /* prevent looping on a bad block */
  764. de_len = ext4_rec_len_from_disk(de->rec_len);
  765. if (de_len <= 0)
  766. return -1;
  767. offset += de_len;
  768. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  769. }
  770. return 0;
  771. }
  772. /*
  773. * ext4_find_entry()
  774. *
  775. * finds an entry in the specified directory with the wanted name. It
  776. * returns the cache buffer in which the entry was found, and the entry
  777. * itself (as a parameter - res_dir). It does NOT read the inode of the
  778. * entry - you'll have to do that yourself if you want to.
  779. *
  780. * The returned buffer_head has ->b_count elevated. The caller is expected
  781. * to brelse() it when appropriate.
  782. */
  783. static struct buffer_head * ext4_find_entry (struct inode *dir,
  784. const struct qstr *d_name,
  785. struct ext4_dir_entry_2 ** res_dir)
  786. {
  787. struct super_block *sb;
  788. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  789. struct buffer_head *bh, *ret = NULL;
  790. ext4_lblk_t start, block, b;
  791. int ra_max = 0; /* Number of bh's in the readahead
  792. buffer, bh_use[] */
  793. int ra_ptr = 0; /* Current index into readahead
  794. buffer */
  795. int num = 0;
  796. ext4_lblk_t nblocks;
  797. int i, err;
  798. int namelen;
  799. *res_dir = NULL;
  800. sb = dir->i_sb;
  801. namelen = d_name->len;
  802. if (namelen > EXT4_NAME_LEN)
  803. return NULL;
  804. if (is_dx(dir)) {
  805. bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
  806. /*
  807. * On success, or if the error was file not found,
  808. * return. Otherwise, fall back to doing a search the
  809. * old fashioned way.
  810. */
  811. if (bh || (err != ERR_BAD_DX_DIR))
  812. return bh;
  813. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  814. "falling back\n"));
  815. }
  816. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  817. start = EXT4_I(dir)->i_dir_start_lookup;
  818. if (start >= nblocks)
  819. start = 0;
  820. block = start;
  821. restart:
  822. do {
  823. /*
  824. * We deal with the read-ahead logic here.
  825. */
  826. if (ra_ptr >= ra_max) {
  827. /* Refill the readahead buffer */
  828. ra_ptr = 0;
  829. b = block;
  830. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  831. /*
  832. * Terminate if we reach the end of the
  833. * directory and must wrap, or if our
  834. * search has finished at this block.
  835. */
  836. if (b >= nblocks || (num && block == start)) {
  837. bh_use[ra_max] = NULL;
  838. break;
  839. }
  840. num++;
  841. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  842. bh_use[ra_max] = bh;
  843. if (bh)
  844. ll_rw_block(READ_META, 1, &bh);
  845. }
  846. }
  847. if ((bh = bh_use[ra_ptr++]) == NULL)
  848. goto next;
  849. wait_on_buffer(bh);
  850. if (!buffer_uptodate(bh)) {
  851. /* read error, skip block & hope for the best */
  852. ext4_error(sb, __func__, "reading directory #%lu "
  853. "offset %lu", dir->i_ino,
  854. (unsigned long)block);
  855. brelse(bh);
  856. goto next;
  857. }
  858. i = search_dirblock(bh, dir, d_name,
  859. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  860. if (i == 1) {
  861. EXT4_I(dir)->i_dir_start_lookup = block;
  862. ret = bh;
  863. goto cleanup_and_exit;
  864. } else {
  865. brelse(bh);
  866. if (i < 0)
  867. goto cleanup_and_exit;
  868. }
  869. next:
  870. if (++block >= nblocks)
  871. block = 0;
  872. } while (block != start);
  873. /*
  874. * If the directory has grown while we were searching, then
  875. * search the last part of the directory before giving up.
  876. */
  877. block = nblocks;
  878. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  879. if (block < nblocks) {
  880. start = 0;
  881. goto restart;
  882. }
  883. cleanup_and_exit:
  884. /* Clean up the read-ahead blocks */
  885. for (; ra_ptr < ra_max; ra_ptr++)
  886. brelse(bh_use[ra_ptr]);
  887. return ret;
  888. }
  889. static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
  890. struct ext4_dir_entry_2 **res_dir, int *err)
  891. {
  892. struct super_block * sb;
  893. struct dx_hash_info hinfo;
  894. u32 hash;
  895. struct dx_frame frames[2], *frame;
  896. struct ext4_dir_entry_2 *de, *top;
  897. struct buffer_head *bh;
  898. ext4_lblk_t block;
  899. int retval;
  900. int namelen = d_name->len;
  901. const u8 *name = d_name->name;
  902. sb = dir->i_sb;
  903. /* NFS may look up ".." - look at dx_root directory block */
  904. if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
  905. if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
  906. return NULL;
  907. } else {
  908. frame = frames;
  909. frame->bh = NULL; /* for dx_release() */
  910. frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
  911. dx_set_block(frame->at, 0); /* dx_root block is 0 */
  912. }
  913. hash = hinfo.hash;
  914. do {
  915. block = dx_get_block(frame->at);
  916. if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
  917. goto errout;
  918. de = (struct ext4_dir_entry_2 *) bh->b_data;
  919. top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
  920. EXT4_DIR_REC_LEN(0));
  921. for (; de < top; de = ext4_next_entry(de)) {
  922. int off = (block << EXT4_BLOCK_SIZE_BITS(sb))
  923. + ((char *) de - bh->b_data);
  924. if (!ext4_check_dir_entry(__func__, dir, de, bh, off)) {
  925. brelse(bh);
  926. *err = ERR_BAD_DX_DIR;
  927. goto errout;
  928. }
  929. if (ext4_match(namelen, name, de)) {
  930. *res_dir = de;
  931. dx_release(frames);
  932. return bh;
  933. }
  934. }
  935. brelse(bh);
  936. /* Check to see if we should continue to search */
  937. retval = ext4_htree_next_block(dir, hash, frame,
  938. frames, NULL);
  939. if (retval < 0) {
  940. ext4_warning(sb, __func__,
  941. "error reading index page in directory #%lu",
  942. dir->i_ino);
  943. *err = retval;
  944. goto errout;
  945. }
  946. } while (retval == 1);
  947. *err = -ENOENT;
  948. errout:
  949. dxtrace(printk(KERN_DEBUG "%s not found\n", name));
  950. dx_release (frames);
  951. return NULL;
  952. }
  953. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  954. {
  955. struct inode *inode;
  956. struct ext4_dir_entry_2 *de;
  957. struct buffer_head *bh;
  958. if (dentry->d_name.len > EXT4_NAME_LEN)
  959. return ERR_PTR(-ENAMETOOLONG);
  960. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  961. inode = NULL;
  962. if (bh) {
  963. __u32 ino = le32_to_cpu(de->inode);
  964. brelse(bh);
  965. if (!ext4_valid_inum(dir->i_sb, ino)) {
  966. ext4_error(dir->i_sb, "ext4_lookup",
  967. "bad inode number: %u", ino);
  968. return ERR_PTR(-EIO);
  969. }
  970. inode = ext4_iget(dir->i_sb, ino);
  971. if (IS_ERR(inode))
  972. return ERR_CAST(inode);
  973. }
  974. return d_splice_alias(inode, dentry);
  975. }
  976. struct dentry *ext4_get_parent(struct dentry *child)
  977. {
  978. __u32 ino;
  979. struct inode *inode;
  980. static const struct qstr dotdot = {
  981. .name = "..",
  982. .len = 2,
  983. };
  984. struct ext4_dir_entry_2 * de;
  985. struct buffer_head *bh;
  986. bh = ext4_find_entry(child->d_inode, &dotdot, &de);
  987. inode = NULL;
  988. if (!bh)
  989. return ERR_PTR(-ENOENT);
  990. ino = le32_to_cpu(de->inode);
  991. brelse(bh);
  992. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  993. ext4_error(child->d_inode->i_sb, "ext4_get_parent",
  994. "bad inode number: %u", ino);
  995. return ERR_PTR(-EIO);
  996. }
  997. return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
  998. }
  999. #define S_SHIFT 12
  1000. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  1001. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  1002. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  1003. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  1004. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  1005. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  1006. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  1007. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  1008. };
  1009. static inline void ext4_set_de_type(struct super_block *sb,
  1010. struct ext4_dir_entry_2 *de,
  1011. umode_t mode) {
  1012. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1013. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1014. }
  1015. /*
  1016. * Move count entries from end of map between two memory locations.
  1017. * Returns pointer to last entry moved.
  1018. */
  1019. static struct ext4_dir_entry_2 *
  1020. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count)
  1021. {
  1022. unsigned rec_len = 0;
  1023. while (count--) {
  1024. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs);
  1025. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1026. memcpy (to, de, rec_len);
  1027. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1028. ext4_rec_len_to_disk(rec_len);
  1029. de->inode = 0;
  1030. map++;
  1031. to += rec_len;
  1032. }
  1033. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1034. }
  1035. /*
  1036. * Compact each dir entry in the range to the minimal rec_len.
  1037. * Returns pointer to last entry in range.
  1038. */
  1039. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size)
  1040. {
  1041. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1042. unsigned rec_len = 0;
  1043. prev = to = de;
  1044. while ((char*)de < base + size) {
  1045. next = ext4_next_entry(de);
  1046. if (de->inode && de->name_len) {
  1047. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1048. if (de > to)
  1049. memmove(to, de, rec_len);
  1050. to->rec_len = ext4_rec_len_to_disk(rec_len);
  1051. prev = to;
  1052. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1053. }
  1054. de = next;
  1055. }
  1056. return prev;
  1057. }
  1058. /*
  1059. * Split a full leaf block to make room for a new dir entry.
  1060. * Allocate a new block, and move entries so that they are approx. equally full.
  1061. * Returns pointer to de in block into which the new entry will be inserted.
  1062. */
  1063. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1064. struct buffer_head **bh,struct dx_frame *frame,
  1065. struct dx_hash_info *hinfo, int *error)
  1066. {
  1067. unsigned blocksize = dir->i_sb->s_blocksize;
  1068. unsigned count, continued;
  1069. struct buffer_head *bh2;
  1070. ext4_lblk_t newblock;
  1071. u32 hash2;
  1072. struct dx_map_entry *map;
  1073. char *data1 = (*bh)->b_data, *data2;
  1074. unsigned split, move, size;
  1075. struct ext4_dir_entry_2 *de = NULL, *de2;
  1076. int err = 0, i;
  1077. bh2 = ext4_append (handle, dir, &newblock, &err);
  1078. if (!(bh2)) {
  1079. brelse(*bh);
  1080. *bh = NULL;
  1081. goto errout;
  1082. }
  1083. BUFFER_TRACE(*bh, "get_write_access");
  1084. err = ext4_journal_get_write_access(handle, *bh);
  1085. if (err)
  1086. goto journal_error;
  1087. BUFFER_TRACE(frame->bh, "get_write_access");
  1088. err = ext4_journal_get_write_access(handle, frame->bh);
  1089. if (err)
  1090. goto journal_error;
  1091. data2 = bh2->b_data;
  1092. /* create map in the end of data2 block */
  1093. map = (struct dx_map_entry *) (data2 + blocksize);
  1094. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1095. blocksize, hinfo, map);
  1096. map -= count;
  1097. dx_sort_map(map, count);
  1098. /* Split the existing block in the middle, size-wise */
  1099. size = 0;
  1100. move = 0;
  1101. for (i = count-1; i >= 0; i--) {
  1102. /* is more than half of this entry in 2nd half of the block? */
  1103. if (size + map[i].size/2 > blocksize/2)
  1104. break;
  1105. size += map[i].size;
  1106. move++;
  1107. }
  1108. /* map index at which we will split */
  1109. split = count - move;
  1110. hash2 = map[split].hash;
  1111. continued = hash2 == map[split - 1].hash;
  1112. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1113. (unsigned long)dx_get_block(frame->at),
  1114. hash2, split, count-split));
  1115. /* Fancy dance to stay within two buffers */
  1116. de2 = dx_move_dirents(data1, data2, map + split, count - split);
  1117. de = dx_pack_dirents(data1, blocksize);
  1118. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
  1119. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2);
  1120. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1121. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1122. /* Which block gets the new entry? */
  1123. if (hinfo->hash >= hash2)
  1124. {
  1125. swap(*bh, bh2);
  1126. de = de2;
  1127. }
  1128. dx_insert_block(frame, hash2 + continued, newblock);
  1129. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1130. if (err)
  1131. goto journal_error;
  1132. err = ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1133. if (err)
  1134. goto journal_error;
  1135. brelse(bh2);
  1136. dxtrace(dx_show_index("frame", frame->entries));
  1137. return de;
  1138. journal_error:
  1139. brelse(*bh);
  1140. brelse(bh2);
  1141. *bh = NULL;
  1142. ext4_std_error(dir->i_sb, err);
  1143. errout:
  1144. *error = err;
  1145. return NULL;
  1146. }
  1147. /*
  1148. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1149. * it points to a directory entry which is guaranteed to be large
  1150. * enough for new directory entry. If de is NULL, then
  1151. * add_dirent_to_buf will attempt search the directory block for
  1152. * space. It will return -ENOSPC if no space is available, and -EIO
  1153. * and -EEXIST if directory entry already exists.
  1154. *
  1155. * NOTE! bh is NOT released in the case where ENOSPC is returned. In
  1156. * all other cases bh is released.
  1157. */
  1158. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1159. struct inode *inode, struct ext4_dir_entry_2 *de,
  1160. struct buffer_head *bh)
  1161. {
  1162. struct inode *dir = dentry->d_parent->d_inode;
  1163. const char *name = dentry->d_name.name;
  1164. int namelen = dentry->d_name.len;
  1165. unsigned int offset = 0;
  1166. unsigned short reclen;
  1167. int nlen, rlen, err;
  1168. char *top;
  1169. reclen = EXT4_DIR_REC_LEN(namelen);
  1170. if (!de) {
  1171. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1172. top = bh->b_data + dir->i_sb->s_blocksize - reclen;
  1173. while ((char *) de <= top) {
  1174. if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
  1175. bh, offset)) {
  1176. brelse(bh);
  1177. return -EIO;
  1178. }
  1179. if (ext4_match(namelen, name, de)) {
  1180. brelse(bh);
  1181. return -EEXIST;
  1182. }
  1183. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1184. rlen = ext4_rec_len_from_disk(de->rec_len);
  1185. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1186. break;
  1187. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1188. offset += rlen;
  1189. }
  1190. if ((char *) de > top)
  1191. return -ENOSPC;
  1192. }
  1193. BUFFER_TRACE(bh, "get_write_access");
  1194. err = ext4_journal_get_write_access(handle, bh);
  1195. if (err) {
  1196. ext4_std_error(dir->i_sb, err);
  1197. brelse(bh);
  1198. return err;
  1199. }
  1200. /* By now the buffer is marked for journaling */
  1201. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1202. rlen = ext4_rec_len_from_disk(de->rec_len);
  1203. if (de->inode) {
  1204. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1205. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen);
  1206. de->rec_len = ext4_rec_len_to_disk(nlen);
  1207. de = de1;
  1208. }
  1209. de->file_type = EXT4_FT_UNKNOWN;
  1210. if (inode) {
  1211. de->inode = cpu_to_le32(inode->i_ino);
  1212. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1213. } else
  1214. de->inode = 0;
  1215. de->name_len = namelen;
  1216. memcpy(de->name, name, namelen);
  1217. /*
  1218. * XXX shouldn't update any times until successful
  1219. * completion of syscall, but too many callers depend
  1220. * on this.
  1221. *
  1222. * XXX similarly, too many callers depend on
  1223. * ext4_new_inode() setting the times, but error
  1224. * recovery deletes the inode, so the worst that can
  1225. * happen is that the times are slightly out of date
  1226. * and/or different from the directory change time.
  1227. */
  1228. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1229. ext4_update_dx_flag(dir);
  1230. dir->i_version++;
  1231. ext4_mark_inode_dirty(handle, dir);
  1232. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1233. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1234. if (err)
  1235. ext4_std_error(dir->i_sb, err);
  1236. brelse(bh);
  1237. return 0;
  1238. }
  1239. /*
  1240. * This converts a one block unindexed directory to a 3 block indexed
  1241. * directory, and adds the dentry to the indexed directory.
  1242. */
  1243. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1244. struct inode *inode, struct buffer_head *bh)
  1245. {
  1246. struct inode *dir = dentry->d_parent->d_inode;
  1247. const char *name = dentry->d_name.name;
  1248. int namelen = dentry->d_name.len;
  1249. struct buffer_head *bh2;
  1250. struct dx_root *root;
  1251. struct dx_frame frames[2], *frame;
  1252. struct dx_entry *entries;
  1253. struct ext4_dir_entry_2 *de, *de2;
  1254. char *data1, *top;
  1255. unsigned len;
  1256. int retval;
  1257. unsigned blocksize;
  1258. struct dx_hash_info hinfo;
  1259. ext4_lblk_t block;
  1260. struct fake_dirent *fde;
  1261. blocksize = dir->i_sb->s_blocksize;
  1262. dxtrace(printk(KERN_DEBUG "Creating index\n"));
  1263. retval = ext4_journal_get_write_access(handle, bh);
  1264. if (retval) {
  1265. ext4_std_error(dir->i_sb, retval);
  1266. brelse(bh);
  1267. return retval;
  1268. }
  1269. root = (struct dx_root *) bh->b_data;
  1270. bh2 = ext4_append(handle, dir, &block, &retval);
  1271. if (!(bh2)) {
  1272. brelse(bh);
  1273. return retval;
  1274. }
  1275. EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
  1276. data1 = bh2->b_data;
  1277. /* The 0th block becomes the root, move the dirents out */
  1278. fde = &root->dotdot;
  1279. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1280. ext4_rec_len_from_disk(fde->rec_len));
  1281. len = ((char *) root) + blocksize - (char *) de;
  1282. memcpy (data1, de, len);
  1283. de = (struct ext4_dir_entry_2 *) data1;
  1284. top = data1 + len;
  1285. while ((char *)(de2 = ext4_next_entry(de)) < top)
  1286. de = de2;
  1287. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de);
  1288. /* Initialize the root; the dot dirents already exist */
  1289. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1290. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2));
  1291. memset (&root->info, 0, sizeof(root->info));
  1292. root->info.info_length = sizeof(root->info);
  1293. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1294. entries = root->entries;
  1295. dx_set_block(entries, 1);
  1296. dx_set_count(entries, 1);
  1297. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1298. /* Initialize as for dx_probe */
  1299. hinfo.hash_version = root->info.hash_version;
  1300. if (hinfo.hash_version <= DX_HASH_TEA)
  1301. hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1302. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1303. ext4fs_dirhash(name, namelen, &hinfo);
  1304. frame = frames;
  1305. frame->entries = entries;
  1306. frame->at = entries;
  1307. frame->bh = bh;
  1308. bh = bh2;
  1309. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1310. dx_release (frames);
  1311. if (!(de))
  1312. return retval;
  1313. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1314. }
  1315. /*
  1316. * ext4_add_entry()
  1317. *
  1318. * adds a file entry to the specified directory, using the same
  1319. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1320. *
  1321. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1322. * may not sleep between calling this and putting something into
  1323. * the entry, as someone else might have used it while you slept.
  1324. */
  1325. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1326. struct inode *inode)
  1327. {
  1328. struct inode *dir = dentry->d_parent->d_inode;
  1329. struct buffer_head *bh;
  1330. struct ext4_dir_entry_2 *de;
  1331. struct super_block *sb;
  1332. int retval;
  1333. int dx_fallback=0;
  1334. unsigned blocksize;
  1335. ext4_lblk_t block, blocks;
  1336. sb = dir->i_sb;
  1337. blocksize = sb->s_blocksize;
  1338. if (!dentry->d_name.len)
  1339. return -EINVAL;
  1340. if (is_dx(dir)) {
  1341. retval = ext4_dx_add_entry(handle, dentry, inode);
  1342. if (!retval || (retval != ERR_BAD_DX_DIR))
  1343. return retval;
  1344. EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
  1345. dx_fallback++;
  1346. ext4_mark_inode_dirty(handle, dir);
  1347. }
  1348. blocks = dir->i_size >> sb->s_blocksize_bits;
  1349. for (block = 0; block < blocks; block++) {
  1350. bh = ext4_bread(handle, dir, block, 0, &retval);
  1351. if(!bh)
  1352. return retval;
  1353. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1354. if (retval != -ENOSPC)
  1355. return retval;
  1356. if (blocks == 1 && !dx_fallback &&
  1357. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1358. return make_indexed_dir(handle, dentry, inode, bh);
  1359. brelse(bh);
  1360. }
  1361. bh = ext4_append(handle, dir, &block, &retval);
  1362. if (!bh)
  1363. return retval;
  1364. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1365. de->inode = 0;
  1366. de->rec_len = ext4_rec_len_to_disk(blocksize);
  1367. return add_dirent_to_buf(handle, dentry, inode, de, bh);
  1368. }
  1369. /*
  1370. * Returns 0 for success, or a negative error value
  1371. */
  1372. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1373. struct inode *inode)
  1374. {
  1375. struct dx_frame frames[2], *frame;
  1376. struct dx_entry *entries, *at;
  1377. struct dx_hash_info hinfo;
  1378. struct buffer_head *bh;
  1379. struct inode *dir = dentry->d_parent->d_inode;
  1380. struct super_block *sb = dir->i_sb;
  1381. struct ext4_dir_entry_2 *de;
  1382. int err;
  1383. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1384. if (!frame)
  1385. return err;
  1386. entries = frame->entries;
  1387. at = frame->at;
  1388. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1389. goto cleanup;
  1390. BUFFER_TRACE(bh, "get_write_access");
  1391. err = ext4_journal_get_write_access(handle, bh);
  1392. if (err)
  1393. goto journal_error;
  1394. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1395. if (err != -ENOSPC) {
  1396. bh = NULL;
  1397. goto cleanup;
  1398. }
  1399. /* Block full, should compress but for now just split */
  1400. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1401. dx_get_count(entries), dx_get_limit(entries)));
  1402. /* Need to split index? */
  1403. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1404. ext4_lblk_t newblock;
  1405. unsigned icount = dx_get_count(entries);
  1406. int levels = frame - frames;
  1407. struct dx_entry *entries2;
  1408. struct dx_node *node2;
  1409. struct buffer_head *bh2;
  1410. if (levels && (dx_get_count(frames->entries) ==
  1411. dx_get_limit(frames->entries))) {
  1412. ext4_warning(sb, __func__,
  1413. "Directory index full!");
  1414. err = -ENOSPC;
  1415. goto cleanup;
  1416. }
  1417. bh2 = ext4_append (handle, dir, &newblock, &err);
  1418. if (!(bh2))
  1419. goto cleanup;
  1420. node2 = (struct dx_node *)(bh2->b_data);
  1421. entries2 = node2->entries;
  1422. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize);
  1423. node2->fake.inode = 0;
  1424. BUFFER_TRACE(frame->bh, "get_write_access");
  1425. err = ext4_journal_get_write_access(handle, frame->bh);
  1426. if (err)
  1427. goto journal_error;
  1428. if (levels) {
  1429. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1430. unsigned hash2 = dx_get_hash(entries + icount1);
  1431. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1432. icount1, icount2));
  1433. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1434. err = ext4_journal_get_write_access(handle,
  1435. frames[0].bh);
  1436. if (err)
  1437. goto journal_error;
  1438. memcpy((char *) entries2, (char *) (entries + icount1),
  1439. icount2 * sizeof(struct dx_entry));
  1440. dx_set_count(entries, icount1);
  1441. dx_set_count(entries2, icount2);
  1442. dx_set_limit(entries2, dx_node_limit(dir));
  1443. /* Which index block gets the new entry? */
  1444. if (at - entries >= icount1) {
  1445. frame->at = at = at - entries - icount1 + entries2;
  1446. frame->entries = entries = entries2;
  1447. swap(frame->bh, bh2);
  1448. }
  1449. dx_insert_block(frames + 0, hash2, newblock);
  1450. dxtrace(dx_show_index("node", frames[1].entries));
  1451. dxtrace(dx_show_index("node",
  1452. ((struct dx_node *) bh2->b_data)->entries));
  1453. err = ext4_handle_dirty_metadata(handle, inode, bh2);
  1454. if (err)
  1455. goto journal_error;
  1456. brelse (bh2);
  1457. } else {
  1458. dxtrace(printk(KERN_DEBUG
  1459. "Creating second level index...\n"));
  1460. memcpy((char *) entries2, (char *) entries,
  1461. icount * sizeof(struct dx_entry));
  1462. dx_set_limit(entries2, dx_node_limit(dir));
  1463. /* Set up root */
  1464. dx_set_count(entries, 1);
  1465. dx_set_block(entries + 0, newblock);
  1466. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1467. /* Add new access path frame */
  1468. frame = frames + 1;
  1469. frame->at = at = at - entries + entries2;
  1470. frame->entries = entries = entries2;
  1471. frame->bh = bh2;
  1472. err = ext4_journal_get_write_access(handle,
  1473. frame->bh);
  1474. if (err)
  1475. goto journal_error;
  1476. }
  1477. ext4_handle_dirty_metadata(handle, inode, frames[0].bh);
  1478. }
  1479. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1480. if (!de)
  1481. goto cleanup;
  1482. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1483. bh = NULL;
  1484. goto cleanup;
  1485. journal_error:
  1486. ext4_std_error(dir->i_sb, err);
  1487. cleanup:
  1488. if (bh)
  1489. brelse(bh);
  1490. dx_release(frames);
  1491. return err;
  1492. }
  1493. /*
  1494. * ext4_delete_entry deletes a directory entry by merging it with the
  1495. * previous entry
  1496. */
  1497. static int ext4_delete_entry(handle_t *handle,
  1498. struct inode *dir,
  1499. struct ext4_dir_entry_2 *de_del,
  1500. struct buffer_head *bh)
  1501. {
  1502. struct ext4_dir_entry_2 *de, *pde;
  1503. int i;
  1504. i = 0;
  1505. pde = NULL;
  1506. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1507. while (i < bh->b_size) {
  1508. if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
  1509. return -EIO;
  1510. if (de == de_del) {
  1511. BUFFER_TRACE(bh, "get_write_access");
  1512. ext4_journal_get_write_access(handle, bh);
  1513. if (pde)
  1514. pde->rec_len = ext4_rec_len_to_disk(
  1515. ext4_rec_len_from_disk(pde->rec_len) +
  1516. ext4_rec_len_from_disk(de->rec_len));
  1517. else
  1518. de->inode = 0;
  1519. dir->i_version++;
  1520. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1521. ext4_handle_dirty_metadata(handle, dir, bh);
  1522. return 0;
  1523. }
  1524. i += ext4_rec_len_from_disk(de->rec_len);
  1525. pde = de;
  1526. de = ext4_next_entry(de);
  1527. }
  1528. return -ENOENT;
  1529. }
  1530. /*
  1531. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1532. * since this indicates that nlinks count was previously 1.
  1533. */
  1534. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1535. {
  1536. inc_nlink(inode);
  1537. if (is_dx(inode) && inode->i_nlink > 1) {
  1538. /* limit is 16-bit i_links_count */
  1539. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1540. inode->i_nlink = 1;
  1541. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1542. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1543. }
  1544. }
  1545. }
  1546. /*
  1547. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1548. * directory has >EXT4_LINK_MAX subdirs.
  1549. */
  1550. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1551. {
  1552. drop_nlink(inode);
  1553. if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
  1554. inc_nlink(inode);
  1555. }
  1556. static int ext4_add_nondir(handle_t *handle,
  1557. struct dentry *dentry, struct inode *inode)
  1558. {
  1559. int err = ext4_add_entry(handle, dentry, inode);
  1560. if (!err) {
  1561. ext4_mark_inode_dirty(handle, inode);
  1562. d_instantiate(dentry, inode);
  1563. unlock_new_inode(inode);
  1564. return 0;
  1565. }
  1566. drop_nlink(inode);
  1567. unlock_new_inode(inode);
  1568. iput(inode);
  1569. return err;
  1570. }
  1571. /*
  1572. * By the time this is called, we already have created
  1573. * the directory cache entry for the new file, but it
  1574. * is so far negative - it has no inode.
  1575. *
  1576. * If the create succeeds, we fill in the inode information
  1577. * with d_instantiate().
  1578. */
  1579. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1580. struct nameidata *nd)
  1581. {
  1582. handle_t *handle;
  1583. struct inode *inode;
  1584. int err, retries = 0;
  1585. retry:
  1586. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1587. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1588. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1589. if (IS_ERR(handle))
  1590. return PTR_ERR(handle);
  1591. if (IS_DIRSYNC(dir))
  1592. ext4_handle_sync(handle);
  1593. inode = ext4_new_inode (handle, dir, mode);
  1594. err = PTR_ERR(inode);
  1595. if (!IS_ERR(inode)) {
  1596. inode->i_op = &ext4_file_inode_operations;
  1597. inode->i_fop = &ext4_file_operations;
  1598. ext4_set_aops(inode);
  1599. err = ext4_add_nondir(handle, dentry, inode);
  1600. }
  1601. ext4_journal_stop(handle);
  1602. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1603. goto retry;
  1604. return err;
  1605. }
  1606. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1607. int mode, dev_t rdev)
  1608. {
  1609. handle_t *handle;
  1610. struct inode *inode;
  1611. int err, retries = 0;
  1612. if (!new_valid_dev(rdev))
  1613. return -EINVAL;
  1614. retry:
  1615. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1616. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1617. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1618. if (IS_ERR(handle))
  1619. return PTR_ERR(handle);
  1620. if (IS_DIRSYNC(dir))
  1621. ext4_handle_sync(handle);
  1622. inode = ext4_new_inode(handle, dir, mode);
  1623. err = PTR_ERR(inode);
  1624. if (!IS_ERR(inode)) {
  1625. init_special_inode(inode, inode->i_mode, rdev);
  1626. #ifdef CONFIG_EXT4_FS_XATTR
  1627. inode->i_op = &ext4_special_inode_operations;
  1628. #endif
  1629. err = ext4_add_nondir(handle, dentry, inode);
  1630. }
  1631. ext4_journal_stop(handle);
  1632. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1633. goto retry;
  1634. return err;
  1635. }
  1636. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1637. {
  1638. handle_t *handle;
  1639. struct inode *inode;
  1640. struct buffer_head *dir_block;
  1641. struct ext4_dir_entry_2 *de;
  1642. int err, retries = 0;
  1643. if (EXT4_DIR_LINK_MAX(dir))
  1644. return -EMLINK;
  1645. retry:
  1646. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1647. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1648. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  1649. if (IS_ERR(handle))
  1650. return PTR_ERR(handle);
  1651. if (IS_DIRSYNC(dir))
  1652. ext4_handle_sync(handle);
  1653. inode = ext4_new_inode(handle, dir, S_IFDIR | mode);
  1654. err = PTR_ERR(inode);
  1655. if (IS_ERR(inode))
  1656. goto out_stop;
  1657. inode->i_op = &ext4_dir_inode_operations;
  1658. inode->i_fop = &ext4_dir_operations;
  1659. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1660. dir_block = ext4_bread(handle, inode, 0, 1, &err);
  1661. if (!dir_block)
  1662. goto out_clear_inode;
  1663. BUFFER_TRACE(dir_block, "get_write_access");
  1664. ext4_journal_get_write_access(handle, dir_block);
  1665. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1666. de->inode = cpu_to_le32(inode->i_ino);
  1667. de->name_len = 1;
  1668. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len));
  1669. strcpy(de->name, ".");
  1670. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1671. de = ext4_next_entry(de);
  1672. de->inode = cpu_to_le32(dir->i_ino);
  1673. de->rec_len = ext4_rec_len_to_disk(inode->i_sb->s_blocksize -
  1674. EXT4_DIR_REC_LEN(1));
  1675. de->name_len = 2;
  1676. strcpy(de->name, "..");
  1677. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1678. inode->i_nlink = 2;
  1679. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  1680. ext4_handle_dirty_metadata(handle, dir, dir_block);
  1681. brelse(dir_block);
  1682. ext4_mark_inode_dirty(handle, inode);
  1683. err = ext4_add_entry(handle, dentry, inode);
  1684. if (err) {
  1685. out_clear_inode:
  1686. clear_nlink(inode);
  1687. unlock_new_inode(inode);
  1688. ext4_mark_inode_dirty(handle, inode);
  1689. iput(inode);
  1690. goto out_stop;
  1691. }
  1692. ext4_inc_count(handle, dir);
  1693. ext4_update_dx_flag(dir);
  1694. ext4_mark_inode_dirty(handle, dir);
  1695. d_instantiate(dentry, inode);
  1696. unlock_new_inode(inode);
  1697. out_stop:
  1698. ext4_journal_stop(handle);
  1699. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1700. goto retry;
  1701. return err;
  1702. }
  1703. /*
  1704. * routine to check that the specified directory is empty (for rmdir)
  1705. */
  1706. static int empty_dir(struct inode *inode)
  1707. {
  1708. unsigned int offset;
  1709. struct buffer_head *bh;
  1710. struct ext4_dir_entry_2 *de, *de1;
  1711. struct super_block *sb;
  1712. int err = 0;
  1713. sb = inode->i_sb;
  1714. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1715. !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
  1716. if (err)
  1717. ext4_error(inode->i_sb, __func__,
  1718. "error %d reading directory #%lu offset 0",
  1719. err, inode->i_ino);
  1720. else
  1721. ext4_warning(inode->i_sb, __func__,
  1722. "bad directory (dir #%lu) - no data block",
  1723. inode->i_ino);
  1724. return 1;
  1725. }
  1726. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1727. de1 = ext4_next_entry(de);
  1728. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1729. !le32_to_cpu(de1->inode) ||
  1730. strcmp(".", de->name) ||
  1731. strcmp("..", de1->name)) {
  1732. ext4_warning(inode->i_sb, "empty_dir",
  1733. "bad directory (dir #%lu) - no `.' or `..'",
  1734. inode->i_ino);
  1735. brelse(bh);
  1736. return 1;
  1737. }
  1738. offset = ext4_rec_len_from_disk(de->rec_len) +
  1739. ext4_rec_len_from_disk(de1->rec_len);
  1740. de = ext4_next_entry(de1);
  1741. while (offset < inode->i_size) {
  1742. if (!bh ||
  1743. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1744. err = 0;
  1745. brelse(bh);
  1746. bh = ext4_bread(NULL, inode,
  1747. offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
  1748. if (!bh) {
  1749. if (err)
  1750. ext4_error(sb, __func__,
  1751. "error %d reading directory"
  1752. " #%lu offset %u",
  1753. err, inode->i_ino, offset);
  1754. offset += sb->s_blocksize;
  1755. continue;
  1756. }
  1757. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1758. }
  1759. if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
  1760. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1761. sb->s_blocksize);
  1762. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1763. continue;
  1764. }
  1765. if (le32_to_cpu(de->inode)) {
  1766. brelse(bh);
  1767. return 0;
  1768. }
  1769. offset += ext4_rec_len_from_disk(de->rec_len);
  1770. de = ext4_next_entry(de);
  1771. }
  1772. brelse(bh);
  1773. return 1;
  1774. }
  1775. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1776. * such inodes, starting at the superblock, in case we crash before the
  1777. * file is closed/deleted, or in case the inode truncate spans multiple
  1778. * transactions and the last transaction is not recovered after a crash.
  1779. *
  1780. * At filesystem recovery time, we walk this list deleting unlinked
  1781. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1782. */
  1783. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1784. {
  1785. struct super_block *sb = inode->i_sb;
  1786. struct ext4_iloc iloc;
  1787. int err = 0, rc;
  1788. if (!ext4_handle_valid(handle))
  1789. return 0;
  1790. lock_super(sb);
  1791. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1792. goto out_unlock;
  1793. /* Orphan handling is only valid for files with data blocks
  1794. * being truncated, or files being unlinked. */
  1795. /* @@@ FIXME: Observation from aviro:
  1796. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1797. * here (on lock_super()), so race with ext4_link() which might bump
  1798. * ->i_nlink. For, say it, character device. Not a regular file,
  1799. * not a directory, not a symlink and ->i_nlink > 0.
  1800. */
  1801. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1802. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1803. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1804. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1805. if (err)
  1806. goto out_unlock;
  1807. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1808. if (err)
  1809. goto out_unlock;
  1810. /* Insert this inode at the head of the on-disk orphan list... */
  1811. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1812. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1813. err = ext4_handle_dirty_metadata(handle, inode, EXT4_SB(sb)->s_sbh);
  1814. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1815. if (!err)
  1816. err = rc;
  1817. /* Only add to the head of the in-memory list if all the
  1818. * previous operations succeeded. If the orphan_add is going to
  1819. * fail (possibly taking the journal offline), we can't risk
  1820. * leaving the inode on the orphan list: stray orphan-list
  1821. * entries can cause panics at unmount time.
  1822. *
  1823. * This is safe: on error we're going to ignore the orphan list
  1824. * anyway on the next recovery. */
  1825. if (!err)
  1826. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1827. jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
  1828. jbd_debug(4, "orphan inode %lu will point to %d\n",
  1829. inode->i_ino, NEXT_ORPHAN(inode));
  1830. out_unlock:
  1831. unlock_super(sb);
  1832. ext4_std_error(inode->i_sb, err);
  1833. return err;
  1834. }
  1835. /*
  1836. * ext4_orphan_del() removes an unlinked or truncated inode from the list
  1837. * of such inodes stored on disk, because it is finally being cleaned up.
  1838. */
  1839. int ext4_orphan_del(handle_t *handle, struct inode *inode)
  1840. {
  1841. struct list_head *prev;
  1842. struct ext4_inode_info *ei = EXT4_I(inode);
  1843. struct ext4_sb_info *sbi;
  1844. __u32 ino_next;
  1845. struct ext4_iloc iloc;
  1846. int err = 0;
  1847. if (!ext4_handle_valid(handle))
  1848. return 0;
  1849. lock_super(inode->i_sb);
  1850. if (list_empty(&ei->i_orphan)) {
  1851. unlock_super(inode->i_sb);
  1852. return 0;
  1853. }
  1854. ino_next = NEXT_ORPHAN(inode);
  1855. prev = ei->i_orphan.prev;
  1856. sbi = EXT4_SB(inode->i_sb);
  1857. jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
  1858. list_del_init(&ei->i_orphan);
  1859. /* If we're on an error path, we may not have a valid
  1860. * transaction handle with which to update the orphan list on
  1861. * disk, but we still need to remove the inode from the linked
  1862. * list in memory. */
  1863. if (sbi->s_journal && !handle)
  1864. goto out;
  1865. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1866. if (err)
  1867. goto out_err;
  1868. if (prev == &sbi->s_orphan) {
  1869. jbd_debug(4, "superblock will point to %u\n", ino_next);
  1870. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  1871. err = ext4_journal_get_write_access(handle, sbi->s_sbh);
  1872. if (err)
  1873. goto out_brelse;
  1874. sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
  1875. err = ext4_handle_dirty_metadata(handle, inode, sbi->s_sbh);
  1876. } else {
  1877. struct ext4_iloc iloc2;
  1878. struct inode *i_prev =
  1879. &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
  1880. jbd_debug(4, "orphan inode %lu will point to %u\n",
  1881. i_prev->i_ino, ino_next);
  1882. err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
  1883. if (err)
  1884. goto out_brelse;
  1885. NEXT_ORPHAN(i_prev) = ino_next;
  1886. err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
  1887. }
  1888. if (err)
  1889. goto out_brelse;
  1890. NEXT_ORPHAN(inode) = 0;
  1891. err = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1892. out_err:
  1893. ext4_std_error(inode->i_sb, err);
  1894. out:
  1895. unlock_super(inode->i_sb);
  1896. return err;
  1897. out_brelse:
  1898. brelse(iloc.bh);
  1899. goto out_err;
  1900. }
  1901. static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
  1902. {
  1903. int retval;
  1904. struct inode *inode;
  1905. struct buffer_head *bh;
  1906. struct ext4_dir_entry_2 *de;
  1907. handle_t *handle;
  1908. /* Initialize quotas before so that eventual writes go in
  1909. * separate transaction */
  1910. DQUOT_INIT(dentry->d_inode);
  1911. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1912. if (IS_ERR(handle))
  1913. return PTR_ERR(handle);
  1914. retval = -ENOENT;
  1915. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1916. if (!bh)
  1917. goto end_rmdir;
  1918. if (IS_DIRSYNC(dir))
  1919. ext4_handle_sync(handle);
  1920. inode = dentry->d_inode;
  1921. retval = -EIO;
  1922. if (le32_to_cpu(de->inode) != inode->i_ino)
  1923. goto end_rmdir;
  1924. retval = -ENOTEMPTY;
  1925. if (!empty_dir(inode))
  1926. goto end_rmdir;
  1927. retval = ext4_delete_entry(handle, dir, de, bh);
  1928. if (retval)
  1929. goto end_rmdir;
  1930. if (!EXT4_DIR_LINK_EMPTY(inode))
  1931. ext4_warning(inode->i_sb, "ext4_rmdir",
  1932. "empty directory has too many links (%d)",
  1933. inode->i_nlink);
  1934. inode->i_version++;
  1935. clear_nlink(inode);
  1936. /* There's no need to set i_disksize: the fact that i_nlink is
  1937. * zero will ensure that the right thing happens during any
  1938. * recovery. */
  1939. inode->i_size = 0;
  1940. ext4_orphan_add(handle, inode);
  1941. inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
  1942. ext4_mark_inode_dirty(handle, inode);
  1943. ext4_dec_count(handle, dir);
  1944. ext4_update_dx_flag(dir);
  1945. ext4_mark_inode_dirty(handle, dir);
  1946. end_rmdir:
  1947. ext4_journal_stop(handle);
  1948. brelse(bh);
  1949. return retval;
  1950. }
  1951. static int ext4_unlink(struct inode *dir, struct dentry *dentry)
  1952. {
  1953. int retval;
  1954. struct inode *inode;
  1955. struct buffer_head *bh;
  1956. struct ext4_dir_entry_2 *de;
  1957. handle_t *handle;
  1958. /* Initialize quotas before so that eventual writes go
  1959. * in separate transaction */
  1960. DQUOT_INIT(dentry->d_inode);
  1961. handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
  1962. if (IS_ERR(handle))
  1963. return PTR_ERR(handle);
  1964. if (IS_DIRSYNC(dir))
  1965. ext4_handle_sync(handle);
  1966. retval = -ENOENT;
  1967. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  1968. if (!bh)
  1969. goto end_unlink;
  1970. inode = dentry->d_inode;
  1971. retval = -EIO;
  1972. if (le32_to_cpu(de->inode) != inode->i_ino)
  1973. goto end_unlink;
  1974. if (!inode->i_nlink) {
  1975. ext4_warning(inode->i_sb, "ext4_unlink",
  1976. "Deleting nonexistent file (%lu), %d",
  1977. inode->i_ino, inode->i_nlink);
  1978. inode->i_nlink = 1;
  1979. }
  1980. retval = ext4_delete_entry(handle, dir, de, bh);
  1981. if (retval)
  1982. goto end_unlink;
  1983. dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
  1984. ext4_update_dx_flag(dir);
  1985. ext4_mark_inode_dirty(handle, dir);
  1986. drop_nlink(inode);
  1987. if (!inode->i_nlink)
  1988. ext4_orphan_add(handle, inode);
  1989. inode->i_ctime = ext4_current_time(inode);
  1990. ext4_mark_inode_dirty(handle, inode);
  1991. retval = 0;
  1992. end_unlink:
  1993. ext4_journal_stop(handle);
  1994. brelse(bh);
  1995. return retval;
  1996. }
  1997. static int ext4_symlink(struct inode *dir,
  1998. struct dentry *dentry, const char *symname)
  1999. {
  2000. handle_t *handle;
  2001. struct inode *inode;
  2002. int l, err, retries = 0;
  2003. l = strlen(symname)+1;
  2004. if (l > dir->i_sb->s_blocksize)
  2005. return -ENAMETOOLONG;
  2006. retry:
  2007. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2008. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 +
  2009. 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb));
  2010. if (IS_ERR(handle))
  2011. return PTR_ERR(handle);
  2012. if (IS_DIRSYNC(dir))
  2013. ext4_handle_sync(handle);
  2014. inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO);
  2015. err = PTR_ERR(inode);
  2016. if (IS_ERR(inode))
  2017. goto out_stop;
  2018. if (l > sizeof(EXT4_I(inode)->i_data)) {
  2019. inode->i_op = &ext4_symlink_inode_operations;
  2020. ext4_set_aops(inode);
  2021. /*
  2022. * page_symlink() calls into ext4_prepare/commit_write.
  2023. * We have a transaction open. All is sweetness. It also sets
  2024. * i_size in generic_commit_write().
  2025. */
  2026. err = __page_symlink(inode, symname, l, 1);
  2027. if (err) {
  2028. clear_nlink(inode);
  2029. unlock_new_inode(inode);
  2030. ext4_mark_inode_dirty(handle, inode);
  2031. iput(inode);
  2032. goto out_stop;
  2033. }
  2034. } else {
  2035. /* clear the extent format for fast symlink */
  2036. EXT4_I(inode)->i_flags &= ~EXT4_EXTENTS_FL;
  2037. inode->i_op = &ext4_fast_symlink_inode_operations;
  2038. memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
  2039. inode->i_size = l-1;
  2040. }
  2041. EXT4_I(inode)->i_disksize = inode->i_size;
  2042. err = ext4_add_nondir(handle, dentry, inode);
  2043. out_stop:
  2044. ext4_journal_stop(handle);
  2045. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2046. goto retry;
  2047. return err;
  2048. }
  2049. static int ext4_link(struct dentry *old_dentry,
  2050. struct inode *dir, struct dentry *dentry)
  2051. {
  2052. handle_t *handle;
  2053. struct inode *inode = old_dentry->d_inode;
  2054. int err, retries = 0;
  2055. if (EXT4_DIR_LINK_MAX(inode))
  2056. return -EMLINK;
  2057. /*
  2058. * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing
  2059. * otherwise has the potential to corrupt the orphan inode list.
  2060. */
  2061. if (inode->i_nlink == 0)
  2062. return -ENOENT;
  2063. retry:
  2064. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  2065. EXT4_INDEX_EXTRA_TRANS_BLOCKS);
  2066. if (IS_ERR(handle))
  2067. return PTR_ERR(handle);
  2068. if (IS_DIRSYNC(dir))
  2069. ext4_handle_sync(handle);
  2070. inode->i_ctime = ext4_current_time(inode);
  2071. ext4_inc_count(handle, inode);
  2072. atomic_inc(&inode->i_count);
  2073. err = ext4_add_entry(handle, dentry, inode);
  2074. if (!err) {
  2075. ext4_mark_inode_dirty(handle, inode);
  2076. d_instantiate(dentry, inode);
  2077. } else {
  2078. drop_nlink(inode);
  2079. iput(inode);
  2080. }
  2081. ext4_journal_stop(handle);
  2082. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  2083. goto retry;
  2084. return err;
  2085. }
  2086. #define PARENT_INO(buffer) \
  2087. (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer))->inode)
  2088. /*
  2089. * Anybody can rename anything with this: the permission checks are left to the
  2090. * higher-level routines.
  2091. */
  2092. static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
  2093. struct inode *new_dir, struct dentry *new_dentry)
  2094. {
  2095. handle_t *handle;
  2096. struct inode *old_inode, *new_inode;
  2097. struct buffer_head *old_bh, *new_bh, *dir_bh;
  2098. struct ext4_dir_entry_2 *old_de, *new_de;
  2099. int retval;
  2100. old_bh = new_bh = dir_bh = NULL;
  2101. /* Initialize quotas before so that eventual writes go
  2102. * in separate transaction */
  2103. if (new_dentry->d_inode)
  2104. DQUOT_INIT(new_dentry->d_inode);
  2105. handle = ext4_journal_start(old_dir, 2 *
  2106. EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
  2107. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
  2108. if (IS_ERR(handle))
  2109. return PTR_ERR(handle);
  2110. if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
  2111. ext4_handle_sync(handle);
  2112. old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
  2113. /*
  2114. * Check for inode number is _not_ due to possible IO errors.
  2115. * We might rmdir the source, keep it as pwd of some process
  2116. * and merrily kill the link to whatever was created under the
  2117. * same name. Goodbye sticky bit ;-<
  2118. */
  2119. old_inode = old_dentry->d_inode;
  2120. retval = -ENOENT;
  2121. if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
  2122. goto end_rename;
  2123. new_inode = new_dentry->d_inode;
  2124. new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
  2125. if (new_bh) {
  2126. if (!new_inode) {
  2127. brelse(new_bh);
  2128. new_bh = NULL;
  2129. }
  2130. }
  2131. if (S_ISDIR(old_inode->i_mode)) {
  2132. if (new_inode) {
  2133. retval = -ENOTEMPTY;
  2134. if (!empty_dir(new_inode))
  2135. goto end_rename;
  2136. }
  2137. retval = -EIO;
  2138. dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
  2139. if (!dir_bh)
  2140. goto end_rename;
  2141. if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino)
  2142. goto end_rename;
  2143. retval = -EMLINK;
  2144. if (!new_inode && new_dir != old_dir &&
  2145. new_dir->i_nlink >= EXT4_LINK_MAX)
  2146. goto end_rename;
  2147. }
  2148. if (!new_bh) {
  2149. retval = ext4_add_entry(handle, new_dentry, old_inode);
  2150. if (retval)
  2151. goto end_rename;
  2152. } else {
  2153. BUFFER_TRACE(new_bh, "get write access");
  2154. ext4_journal_get_write_access(handle, new_bh);
  2155. new_de->inode = cpu_to_le32(old_inode->i_ino);
  2156. if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
  2157. EXT4_FEATURE_INCOMPAT_FILETYPE))
  2158. new_de->file_type = old_de->file_type;
  2159. new_dir->i_version++;
  2160. new_dir->i_ctime = new_dir->i_mtime =
  2161. ext4_current_time(new_dir);
  2162. ext4_mark_inode_dirty(handle, new_dir);
  2163. BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
  2164. ext4_handle_dirty_metadata(handle, new_dir, new_bh);
  2165. brelse(new_bh);
  2166. new_bh = NULL;
  2167. }
  2168. /*
  2169. * Like most other Unix systems, set the ctime for inodes on a
  2170. * rename.
  2171. */
  2172. old_inode->i_ctime = ext4_current_time(old_inode);
  2173. ext4_mark_inode_dirty(handle, old_inode);
  2174. /*
  2175. * ok, that's it
  2176. */
  2177. if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
  2178. old_de->name_len != old_dentry->d_name.len ||
  2179. strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
  2180. (retval = ext4_delete_entry(handle, old_dir,
  2181. old_de, old_bh)) == -ENOENT) {
  2182. /* old_de could have moved from under us during htree split, so
  2183. * make sure that we are deleting the right entry. We might
  2184. * also be pointing to a stale entry in the unused part of
  2185. * old_bh so just checking inum and the name isn't enough. */
  2186. struct buffer_head *old_bh2;
  2187. struct ext4_dir_entry_2 *old_de2;
  2188. old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
  2189. if (old_bh2) {
  2190. retval = ext4_delete_entry(handle, old_dir,
  2191. old_de2, old_bh2);
  2192. brelse(old_bh2);
  2193. }
  2194. }
  2195. if (retval) {
  2196. ext4_warning(old_dir->i_sb, "ext4_rename",
  2197. "Deleting old file (%lu), %d, error=%d",
  2198. old_dir->i_ino, old_dir->i_nlink, retval);
  2199. }
  2200. if (new_inode) {
  2201. ext4_dec_count(handle, new_inode);
  2202. new_inode->i_ctime = ext4_current_time(new_inode);
  2203. }
  2204. old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
  2205. ext4_update_dx_flag(old_dir);
  2206. if (dir_bh) {
  2207. BUFFER_TRACE(dir_bh, "get_write_access");
  2208. ext4_journal_get_write_access(handle, dir_bh);
  2209. PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino);
  2210. BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
  2211. ext4_handle_dirty_metadata(handle, old_dir, dir_bh);
  2212. ext4_dec_count(handle, old_dir);
  2213. if (new_inode) {
  2214. /* checked empty_dir above, can't have another parent,
  2215. * ext4_dec_count() won't work for many-linked dirs */
  2216. new_inode->i_nlink = 0;
  2217. } else {
  2218. ext4_inc_count(handle, new_dir);
  2219. ext4_update_dx_flag(new_dir);
  2220. ext4_mark_inode_dirty(handle, new_dir);
  2221. }
  2222. }
  2223. ext4_mark_inode_dirty(handle, old_dir);
  2224. if (new_inode) {
  2225. ext4_mark_inode_dirty(handle, new_inode);
  2226. if (!new_inode->i_nlink)
  2227. ext4_orphan_add(handle, new_inode);
  2228. }
  2229. retval = 0;
  2230. end_rename:
  2231. brelse(dir_bh);
  2232. brelse(old_bh);
  2233. brelse(new_bh);
  2234. ext4_journal_stop(handle);
  2235. return retval;
  2236. }
  2237. /*
  2238. * directories can handle most operations...
  2239. */
  2240. const struct inode_operations ext4_dir_inode_operations = {
  2241. .create = ext4_create,
  2242. .lookup = ext4_lookup,
  2243. .link = ext4_link,
  2244. .unlink = ext4_unlink,
  2245. .symlink = ext4_symlink,
  2246. .mkdir = ext4_mkdir,
  2247. .rmdir = ext4_rmdir,
  2248. .mknod = ext4_mknod,
  2249. .rename = ext4_rename,
  2250. .setattr = ext4_setattr,
  2251. #ifdef CONFIG_EXT4_FS_XATTR
  2252. .setxattr = generic_setxattr,
  2253. .getxattr = generic_getxattr,
  2254. .listxattr = ext4_listxattr,
  2255. .removexattr = generic_removexattr,
  2256. #endif
  2257. .permission = ext4_permission,
  2258. };
  2259. const struct inode_operations ext4_special_inode_operations = {
  2260. .setattr = ext4_setattr,
  2261. #ifdef CONFIG_EXT4_FS_XATTR
  2262. .setxattr = generic_setxattr,
  2263. .getxattr = generic_getxattr,
  2264. .listxattr = ext4_listxattr,
  2265. .removexattr = generic_removexattr,
  2266. #endif
  2267. .permission = ext4_permission,
  2268. };