namei.c 70 KB

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