namei.c 69 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, __func__,
  347. "Unrecognised inode hash code %d",
  348. root->info.hash_version);
  349. brelse(bh);
  350. *err = ERR_BAD_DX_DIR;
  351. goto fail;
  352. }
  353. hinfo->hash_version = root->info.hash_version;
  354. if (hinfo->hash_version <= DX_HASH_TEA)
  355. hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  356. hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  357. if (d_name)
  358. ext4fs_dirhash(d_name->name, d_name->len, hinfo);
  359. hash = hinfo->hash;
  360. if (root->info.unused_flags & 1) {
  361. ext4_warning(dir->i_sb, __func__,
  362. "Unimplemented inode hash flags: %#06x",
  363. root->info.unused_flags);
  364. brelse(bh);
  365. *err = ERR_BAD_DX_DIR;
  366. goto fail;
  367. }
  368. if ((indirect = root->info.indirect_levels) > 1) {
  369. ext4_warning(dir->i_sb, __func__,
  370. "Unimplemented inode hash depth: %#06x",
  371. root->info.indirect_levels);
  372. brelse(bh);
  373. *err = ERR_BAD_DX_DIR;
  374. goto fail;
  375. }
  376. entries = (struct dx_entry *) (((char *)&root->info) +
  377. root->info.info_length);
  378. if (dx_get_limit(entries) != dx_root_limit(dir,
  379. root->info.info_length)) {
  380. ext4_warning(dir->i_sb, __func__,
  381. "dx entry: limit != root limit");
  382. brelse(bh);
  383. *err = ERR_BAD_DX_DIR;
  384. goto fail;
  385. }
  386. dxtrace(printk("Look up %x", hash));
  387. while (1)
  388. {
  389. count = dx_get_count(entries);
  390. if (!count || count > dx_get_limit(entries)) {
  391. ext4_warning(dir->i_sb, __func__,
  392. "dx entry: no count or count > limit");
  393. brelse(bh);
  394. *err = ERR_BAD_DX_DIR;
  395. goto fail2;
  396. }
  397. p = entries + 1;
  398. q = entries + count - 1;
  399. while (p <= q)
  400. {
  401. m = p + (q - p)/2;
  402. dxtrace(printk("."));
  403. if (dx_get_hash(m) > hash)
  404. q = m - 1;
  405. else
  406. p = m + 1;
  407. }
  408. if (0) // linear search cross check
  409. {
  410. unsigned n = count - 1;
  411. at = entries;
  412. while (n--)
  413. {
  414. dxtrace(printk(","));
  415. if (dx_get_hash(++at) > hash)
  416. {
  417. at--;
  418. break;
  419. }
  420. }
  421. assert (at == p - 1);
  422. }
  423. at = p - 1;
  424. dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
  425. frame->bh = bh;
  426. frame->entries = entries;
  427. frame->at = at;
  428. if (!indirect--) return frame;
  429. if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
  430. goto fail2;
  431. at = entries = ((struct dx_node *) bh->b_data)->entries;
  432. if (dx_get_limit(entries) != dx_node_limit (dir)) {
  433. ext4_warning(dir->i_sb, __func__,
  434. "dx entry: limit != node limit");
  435. brelse(bh);
  436. *err = ERR_BAD_DX_DIR;
  437. goto fail2;
  438. }
  439. frame++;
  440. frame->bh = NULL;
  441. }
  442. fail2:
  443. while (frame >= frame_in) {
  444. brelse(frame->bh);
  445. frame--;
  446. }
  447. fail:
  448. if (*err == ERR_BAD_DX_DIR)
  449. ext4_warning(dir->i_sb, __func__,
  450. "Corrupt dir inode %ld, running e2fsck is "
  451. "recommended.", dir->i_ino);
  452. return NULL;
  453. }
  454. static void dx_release (struct dx_frame *frames)
  455. {
  456. if (frames[0].bh == NULL)
  457. return;
  458. if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
  459. brelse(frames[1].bh);
  460. brelse(frames[0].bh);
  461. }
  462. /*
  463. * This function increments the frame pointer to search the next leaf
  464. * block, and reads in the necessary intervening nodes if the search
  465. * should be necessary. Whether or not the search is necessary is
  466. * controlled by the hash parameter. If the hash value is even, then
  467. * the search is only continued if the next block starts with that
  468. * hash value. This is used if we are searching for a specific file.
  469. *
  470. * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
  471. *
  472. * This function returns 1 if the caller should continue to search,
  473. * or 0 if it should not. If there is an error reading one of the
  474. * index blocks, it will a negative error code.
  475. *
  476. * If start_hash is non-null, it will be filled in with the starting
  477. * hash of the next page.
  478. */
  479. static int ext4_htree_next_block(struct inode *dir, __u32 hash,
  480. struct dx_frame *frame,
  481. struct dx_frame *frames,
  482. __u32 *start_hash)
  483. {
  484. struct dx_frame *p;
  485. struct buffer_head *bh;
  486. int err, num_frames = 0;
  487. __u32 bhash;
  488. p = frame;
  489. /*
  490. * Find the next leaf page by incrementing the frame pointer.
  491. * If we run out of entries in the interior node, loop around and
  492. * increment pointer in the parent node. When we break out of
  493. * this loop, num_frames indicates the number of interior
  494. * nodes need to be read.
  495. */
  496. while (1) {
  497. if (++(p->at) < p->entries + dx_get_count(p->entries))
  498. break;
  499. if (p == frames)
  500. return 0;
  501. num_frames++;
  502. p--;
  503. }
  504. /*
  505. * If the hash is 1, then continue only if the next page has a
  506. * continuation hash of any value. This is used for readdir
  507. * handling. Otherwise, check to see if the hash matches the
  508. * desired contiuation hash. If it doesn't, return since
  509. * there's no point to read in the successive index pages.
  510. */
  511. bhash = dx_get_hash(p->at);
  512. if (start_hash)
  513. *start_hash = bhash;
  514. if ((hash & 1) == 0) {
  515. if ((bhash & ~1) != hash)
  516. return 0;
  517. }
  518. /*
  519. * If the hash is HASH_NB_ALWAYS, we always go to the next
  520. * block so no check is necessary
  521. */
  522. while (num_frames--) {
  523. if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
  524. 0, &err)))
  525. return err; /* Failure */
  526. p++;
  527. brelse(p->bh);
  528. p->bh = bh;
  529. p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
  530. }
  531. return 1;
  532. }
  533. /*
  534. * This function fills a red-black tree with information from a
  535. * directory block. It returns the number directory entries loaded
  536. * into the tree. If there is an error it is returned in err.
  537. */
  538. static int htree_dirblock_to_tree(struct file *dir_file,
  539. struct inode *dir, ext4_lblk_t block,
  540. struct dx_hash_info *hinfo,
  541. __u32 start_hash, __u32 start_minor_hash)
  542. {
  543. struct buffer_head *bh;
  544. struct ext4_dir_entry_2 *de, *top;
  545. int err, count = 0;
  546. dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
  547. (unsigned long)block));
  548. if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
  549. return err;
  550. de = (struct ext4_dir_entry_2 *) bh->b_data;
  551. top = (struct ext4_dir_entry_2 *) ((char *) de +
  552. dir->i_sb->s_blocksize -
  553. EXT4_DIR_REC_LEN(0));
  554. for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
  555. if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
  556. (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
  557. +((char *)de - bh->b_data))) {
  558. /* On error, skip the f_pos to the next block. */
  559. dir_file->f_pos = (dir_file->f_pos |
  560. (dir->i_sb->s_blocksize - 1)) + 1;
  561. brelse(bh);
  562. return count;
  563. }
  564. ext4fs_dirhash(de->name, de->name_len, hinfo);
  565. if ((hinfo->hash < start_hash) ||
  566. ((hinfo->hash == start_hash) &&
  567. (hinfo->minor_hash < start_minor_hash)))
  568. continue;
  569. if (de->inode == 0)
  570. continue;
  571. if ((err = ext4_htree_store_dirent(dir_file,
  572. hinfo->hash, hinfo->minor_hash, de)) != 0) {
  573. brelse(bh);
  574. return err;
  575. }
  576. count++;
  577. }
  578. brelse(bh);
  579. return count;
  580. }
  581. /*
  582. * This function fills a red-black tree with information from a
  583. * directory. We start scanning the directory in hash order, starting
  584. * at start_hash and start_minor_hash.
  585. *
  586. * This function returns the number of entries inserted into the tree,
  587. * or a negative error code.
  588. */
  589. int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  590. __u32 start_minor_hash, __u32 *next_hash)
  591. {
  592. struct dx_hash_info hinfo;
  593. struct ext4_dir_entry_2 *de;
  594. struct dx_frame frames[2], *frame;
  595. struct inode *dir;
  596. ext4_lblk_t block;
  597. int count = 0;
  598. int ret, err;
  599. __u32 hashval;
  600. dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
  601. start_hash, start_minor_hash));
  602. dir = dir_file->f_path.dentry->d_inode;
  603. if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) {
  604. hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  605. if (hinfo.hash_version <= DX_HASH_TEA)
  606. hinfo.hash_version +=
  607. EXT4_SB(dir->i_sb)->s_hash_unsigned;
  608. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  609. count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
  610. start_hash, start_minor_hash);
  611. *next_hash = ~0;
  612. return count;
  613. }
  614. hinfo.hash = start_hash;
  615. hinfo.minor_hash = 0;
  616. frame = dx_probe(NULL, dir, &hinfo, frames, &err);
  617. if (!frame)
  618. return err;
  619. /* Add '.' and '..' from the htree header */
  620. if (!start_hash && !start_minor_hash) {
  621. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  622. if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
  623. goto errout;
  624. count++;
  625. }
  626. if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
  627. de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
  628. de = ext4_next_entry(de, dir->i_sb->s_blocksize);
  629. if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
  630. goto errout;
  631. count++;
  632. }
  633. while (1) {
  634. block = dx_get_block(frame->at);
  635. ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
  636. start_hash, start_minor_hash);
  637. if (ret < 0) {
  638. err = ret;
  639. goto errout;
  640. }
  641. count += ret;
  642. hashval = ~0;
  643. ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
  644. frame, frames, &hashval);
  645. *next_hash = hashval;
  646. if (ret < 0) {
  647. err = ret;
  648. goto errout;
  649. }
  650. /*
  651. * Stop if: (a) there are no more entries, or
  652. * (b) we have inserted at least one entry and the
  653. * next hash value is not a continuation
  654. */
  655. if ((ret == 0) ||
  656. (count && ((hashval & 1) == 0)))
  657. break;
  658. }
  659. dx_release(frames);
  660. dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
  661. "next hash: %x\n", count, *next_hash));
  662. return count;
  663. errout:
  664. dx_release(frames);
  665. return (err);
  666. }
  667. /*
  668. * Directory block splitting, compacting
  669. */
  670. /*
  671. * Create map of hash values, offsets, and sizes, stored at end of block.
  672. * Returns number of entries mapped.
  673. */
  674. static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
  675. struct dx_hash_info *hinfo,
  676. struct dx_map_entry *map_tail)
  677. {
  678. int count = 0;
  679. char *base = (char *) de;
  680. struct dx_hash_info h = *hinfo;
  681. while ((char *) de < base + blocksize) {
  682. if (de->name_len && de->inode) {
  683. ext4fs_dirhash(de->name, de->name_len, &h);
  684. map_tail--;
  685. map_tail->hash = h.hash;
  686. map_tail->offs = ((char *) de - base)>>2;
  687. map_tail->size = le16_to_cpu(de->rec_len);
  688. count++;
  689. cond_resched();
  690. }
  691. /* XXX: do we need to check rec_len == 0 case? -Chris */
  692. de = ext4_next_entry(de, blocksize);
  693. }
  694. return count;
  695. }
  696. /* Sort map by hash value */
  697. static void dx_sort_map (struct dx_map_entry *map, unsigned count)
  698. {
  699. struct dx_map_entry *p, *q, *top = map + count - 1;
  700. int more;
  701. /* Combsort until bubble sort doesn't suck */
  702. while (count > 2) {
  703. count = count*10/13;
  704. if (count - 9 < 2) /* 9, 10 -> 11 */
  705. count = 11;
  706. for (p = top, q = p - count; q >= map; p--, q--)
  707. if (p->hash < q->hash)
  708. swap(*p, *q);
  709. }
  710. /* Garden variety bubble sort */
  711. do {
  712. more = 0;
  713. q = top;
  714. while (q-- > map) {
  715. if (q[1].hash >= q[0].hash)
  716. continue;
  717. swap(*(q+1), *q);
  718. more = 1;
  719. }
  720. } while(more);
  721. }
  722. static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
  723. {
  724. struct dx_entry *entries = frame->entries;
  725. struct dx_entry *old = frame->at, *new = old + 1;
  726. int count = dx_get_count(entries);
  727. assert(count < dx_get_limit(entries));
  728. assert(old < entries + count);
  729. memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
  730. dx_set_hash(new, hash);
  731. dx_set_block(new, block);
  732. dx_set_count(entries, count + 1);
  733. }
  734. static void ext4_update_dx_flag(struct inode *inode)
  735. {
  736. if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
  737. EXT4_FEATURE_COMPAT_DIR_INDEX))
  738. EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL;
  739. }
  740. /*
  741. * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
  742. *
  743. * `len <= EXT4_NAME_LEN' is guaranteed by caller.
  744. * `de != NULL' is guaranteed by caller.
  745. */
  746. static inline int ext4_match (int len, const char * const name,
  747. struct ext4_dir_entry_2 * de)
  748. {
  749. if (len != de->name_len)
  750. return 0;
  751. if (!de->inode)
  752. return 0;
  753. return !memcmp(name, de->name, len);
  754. }
  755. /*
  756. * Returns 0 if not found, -1 on failure, and 1 on success
  757. */
  758. static inline int search_dirblock(struct buffer_head *bh,
  759. struct inode *dir,
  760. const struct qstr *d_name,
  761. unsigned int offset,
  762. struct ext4_dir_entry_2 ** res_dir)
  763. {
  764. struct ext4_dir_entry_2 * de;
  765. char * dlimit;
  766. int de_len;
  767. const char *name = d_name->name;
  768. int namelen = d_name->len;
  769. de = (struct ext4_dir_entry_2 *) bh->b_data;
  770. dlimit = bh->b_data + dir->i_sb->s_blocksize;
  771. while ((char *) de < dlimit) {
  772. /* this code is executed quadratically often */
  773. /* do minimal checking `by hand' */
  774. if ((char *) de + namelen <= dlimit &&
  775. ext4_match (namelen, name, de)) {
  776. /* found a match - just to be sure, do a full check */
  777. if (!ext4_check_dir_entry("ext4_find_entry",
  778. dir, de, bh, offset))
  779. return -1;
  780. *res_dir = de;
  781. return 1;
  782. }
  783. /* prevent looping on a bad block */
  784. de_len = ext4_rec_len_from_disk(de->rec_len,
  785. dir->i_sb->s_blocksize);
  786. if (de_len <= 0)
  787. return -1;
  788. offset += de_len;
  789. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  790. }
  791. return 0;
  792. }
  793. /*
  794. * ext4_find_entry()
  795. *
  796. * finds an entry in the specified directory with the wanted name. It
  797. * returns the cache buffer in which the entry was found, and the entry
  798. * itself (as a parameter - res_dir). It does NOT read the inode of the
  799. * entry - you'll have to do that yourself if you want to.
  800. *
  801. * The returned buffer_head has ->b_count elevated. The caller is expected
  802. * to brelse() it when appropriate.
  803. */
  804. static struct buffer_head * ext4_find_entry (struct inode *dir,
  805. const struct qstr *d_name,
  806. struct ext4_dir_entry_2 ** res_dir)
  807. {
  808. struct super_block *sb;
  809. struct buffer_head *bh_use[NAMEI_RA_SIZE];
  810. struct buffer_head *bh, *ret = NULL;
  811. ext4_lblk_t start, block, b;
  812. int ra_max = 0; /* Number of bh's in the readahead
  813. buffer, bh_use[] */
  814. int ra_ptr = 0; /* Current index into readahead
  815. buffer */
  816. int num = 0;
  817. ext4_lblk_t nblocks;
  818. int i, err;
  819. int namelen;
  820. *res_dir = NULL;
  821. sb = dir->i_sb;
  822. namelen = d_name->len;
  823. if (namelen > EXT4_NAME_LEN)
  824. return NULL;
  825. if (is_dx(dir)) {
  826. bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
  827. /*
  828. * On success, or if the error was file not found,
  829. * return. Otherwise, fall back to doing a search the
  830. * old fashioned way.
  831. */
  832. if (bh || (err != ERR_BAD_DX_DIR))
  833. return bh;
  834. dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
  835. "falling back\n"));
  836. }
  837. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  838. start = EXT4_I(dir)->i_dir_start_lookup;
  839. if (start >= nblocks)
  840. start = 0;
  841. block = start;
  842. restart:
  843. do {
  844. /*
  845. * We deal with the read-ahead logic here.
  846. */
  847. if (ra_ptr >= ra_max) {
  848. /* Refill the readahead buffer */
  849. ra_ptr = 0;
  850. b = block;
  851. for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  852. /*
  853. * Terminate if we reach the end of the
  854. * directory and must wrap, or if our
  855. * search has finished at this block.
  856. */
  857. if (b >= nblocks || (num && block == start)) {
  858. bh_use[ra_max] = NULL;
  859. break;
  860. }
  861. num++;
  862. bh = ext4_getblk(NULL, dir, b++, 0, &err);
  863. bh_use[ra_max] = bh;
  864. if (bh)
  865. ll_rw_block(READ_META, 1, &bh);
  866. }
  867. }
  868. if ((bh = bh_use[ra_ptr++]) == NULL)
  869. goto next;
  870. wait_on_buffer(bh);
  871. if (!buffer_uptodate(bh)) {
  872. /* read error, skip block & hope for the best */
  873. ext4_error(sb, __func__, "reading directory #%lu "
  874. "offset %lu", dir->i_ino,
  875. (unsigned long)block);
  876. brelse(bh);
  877. goto next;
  878. }
  879. i = search_dirblock(bh, dir, d_name,
  880. block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
  881. if (i == 1) {
  882. EXT4_I(dir)->i_dir_start_lookup = block;
  883. ret = bh;
  884. goto cleanup_and_exit;
  885. } else {
  886. brelse(bh);
  887. if (i < 0)
  888. goto cleanup_and_exit;
  889. }
  890. next:
  891. if (++block >= nblocks)
  892. block = 0;
  893. } while (block != start);
  894. /*
  895. * If the directory has grown while we were searching, then
  896. * search the last part of the directory before giving up.
  897. */
  898. block = nblocks;
  899. nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
  900. if (block < nblocks) {
  901. start = 0;
  902. goto restart;
  903. }
  904. cleanup_and_exit:
  905. /* Clean up the read-ahead blocks */
  906. for (; ra_ptr < ra_max; ra_ptr++)
  907. brelse(bh_use[ra_ptr]);
  908. return ret;
  909. }
  910. static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
  911. struct ext4_dir_entry_2 **res_dir, int *err)
  912. {
  913. struct super_block * sb;
  914. struct dx_hash_info hinfo;
  915. u32 hash;
  916. struct dx_frame frames[2], *frame;
  917. struct ext4_dir_entry_2 *de, *top;
  918. struct buffer_head *bh;
  919. ext4_lblk_t block;
  920. int retval;
  921. int namelen = d_name->len;
  922. const u8 *name = d_name->name;
  923. sb = dir->i_sb;
  924. /* NFS may look up ".." - look at dx_root directory block */
  925. if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){
  926. if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
  927. return NULL;
  928. } else {
  929. frame = frames;
  930. frame->bh = NULL; /* for dx_release() */
  931. frame->at = (struct dx_entry *)frames; /* hack for zero entry*/
  932. dx_set_block(frame->at, 0); /* dx_root block is 0 */
  933. }
  934. hash = hinfo.hash;
  935. do {
  936. block = dx_get_block(frame->at);
  937. if (!(bh = ext4_bread (NULL,dir, block, 0, err)))
  938. goto errout;
  939. de = (struct ext4_dir_entry_2 *) bh->b_data;
  940. top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize -
  941. EXT4_DIR_REC_LEN(0));
  942. for (; de < top; de = ext4_next_entry(de, sb->s_blocksize)) {
  943. int off = (block << EXT4_BLOCK_SIZE_BITS(sb))
  944. + ((char *) de - bh->b_data);
  945. if (!ext4_check_dir_entry(__func__, dir, de, bh, off)) {
  946. brelse(bh);
  947. *err = ERR_BAD_DX_DIR;
  948. goto errout;
  949. }
  950. if (ext4_match(namelen, name, de)) {
  951. *res_dir = de;
  952. dx_release(frames);
  953. return bh;
  954. }
  955. }
  956. brelse(bh);
  957. /* Check to see if we should continue to search */
  958. retval = ext4_htree_next_block(dir, hash, frame,
  959. frames, NULL);
  960. if (retval < 0) {
  961. ext4_warning(sb, __func__,
  962. "error reading index page in directory #%lu",
  963. dir->i_ino);
  964. *err = retval;
  965. goto errout;
  966. }
  967. } while (retval == 1);
  968. *err = -ENOENT;
  969. errout:
  970. dxtrace(printk(KERN_DEBUG "%s not found\n", name));
  971. dx_release (frames);
  972. return NULL;
  973. }
  974. static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  975. {
  976. struct inode *inode;
  977. struct ext4_dir_entry_2 *de;
  978. struct buffer_head *bh;
  979. if (dentry->d_name.len > EXT4_NAME_LEN)
  980. return ERR_PTR(-ENAMETOOLONG);
  981. bh = ext4_find_entry(dir, &dentry->d_name, &de);
  982. inode = NULL;
  983. if (bh) {
  984. __u32 ino = le32_to_cpu(de->inode);
  985. brelse(bh);
  986. if (!ext4_valid_inum(dir->i_sb, ino)) {
  987. ext4_error(dir->i_sb, "ext4_lookup",
  988. "bad inode number: %u", ino);
  989. return ERR_PTR(-EIO);
  990. }
  991. inode = ext4_iget(dir->i_sb, ino);
  992. if (unlikely(IS_ERR(inode))) {
  993. if (PTR_ERR(inode) == -ESTALE) {
  994. ext4_error(dir->i_sb, __func__,
  995. "deleted inode referenced: %u",
  996. ino);
  997. return ERR_PTR(-EIO);
  998. } else {
  999. return ERR_CAST(inode);
  1000. }
  1001. }
  1002. }
  1003. return d_splice_alias(inode, dentry);
  1004. }
  1005. struct dentry *ext4_get_parent(struct dentry *child)
  1006. {
  1007. __u32 ino;
  1008. struct inode *inode;
  1009. static const struct qstr dotdot = {
  1010. .name = "..",
  1011. .len = 2,
  1012. };
  1013. struct ext4_dir_entry_2 * de;
  1014. struct buffer_head *bh;
  1015. bh = ext4_find_entry(child->d_inode, &dotdot, &de);
  1016. inode = NULL;
  1017. if (!bh)
  1018. return ERR_PTR(-ENOENT);
  1019. ino = le32_to_cpu(de->inode);
  1020. brelse(bh);
  1021. if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
  1022. ext4_error(child->d_inode->i_sb, "ext4_get_parent",
  1023. "bad inode number: %u", ino);
  1024. return ERR_PTR(-EIO);
  1025. }
  1026. return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
  1027. }
  1028. #define S_SHIFT 12
  1029. static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
  1030. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  1031. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  1032. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  1033. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  1034. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  1035. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  1036. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  1037. };
  1038. static inline void ext4_set_de_type(struct super_block *sb,
  1039. struct ext4_dir_entry_2 *de,
  1040. umode_t mode) {
  1041. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
  1042. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  1043. }
  1044. /*
  1045. * Move count entries from end of map between two memory locations.
  1046. * Returns pointer to last entry moved.
  1047. */
  1048. static struct ext4_dir_entry_2 *
  1049. dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
  1050. unsigned blocksize)
  1051. {
  1052. unsigned rec_len = 0;
  1053. while (count--) {
  1054. struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
  1055. (from + (map->offs<<2));
  1056. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1057. memcpy (to, de, rec_len);
  1058. ((struct ext4_dir_entry_2 *) to)->rec_len =
  1059. ext4_rec_len_to_disk(rec_len, blocksize);
  1060. de->inode = 0;
  1061. map++;
  1062. to += rec_len;
  1063. }
  1064. return (struct ext4_dir_entry_2 *) (to - rec_len);
  1065. }
  1066. /*
  1067. * Compact each dir entry in the range to the minimal rec_len.
  1068. * Returns pointer to last entry in range.
  1069. */
  1070. static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
  1071. {
  1072. struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
  1073. unsigned rec_len = 0;
  1074. prev = to = de;
  1075. while ((char*)de < base + blocksize) {
  1076. next = ext4_next_entry(de, blocksize);
  1077. if (de->inode && de->name_len) {
  1078. rec_len = EXT4_DIR_REC_LEN(de->name_len);
  1079. if (de > to)
  1080. memmove(to, de, rec_len);
  1081. to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
  1082. prev = to;
  1083. to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
  1084. }
  1085. de = next;
  1086. }
  1087. return prev;
  1088. }
  1089. /*
  1090. * Split a full leaf block to make room for a new dir entry.
  1091. * Allocate a new block, and move entries so that they are approx. equally full.
  1092. * Returns pointer to de in block into which the new entry will be inserted.
  1093. */
  1094. static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
  1095. struct buffer_head **bh,struct dx_frame *frame,
  1096. struct dx_hash_info *hinfo, int *error)
  1097. {
  1098. unsigned blocksize = dir->i_sb->s_blocksize;
  1099. unsigned count, continued;
  1100. struct buffer_head *bh2;
  1101. ext4_lblk_t newblock;
  1102. u32 hash2;
  1103. struct dx_map_entry *map;
  1104. char *data1 = (*bh)->b_data, *data2;
  1105. unsigned split, move, size;
  1106. struct ext4_dir_entry_2 *de = NULL, *de2;
  1107. int err = 0, i;
  1108. bh2 = ext4_append (handle, dir, &newblock, &err);
  1109. if (!(bh2)) {
  1110. brelse(*bh);
  1111. *bh = NULL;
  1112. goto errout;
  1113. }
  1114. BUFFER_TRACE(*bh, "get_write_access");
  1115. err = ext4_journal_get_write_access(handle, *bh);
  1116. if (err)
  1117. goto journal_error;
  1118. BUFFER_TRACE(frame->bh, "get_write_access");
  1119. err = ext4_journal_get_write_access(handle, frame->bh);
  1120. if (err)
  1121. goto journal_error;
  1122. data2 = bh2->b_data;
  1123. /* create map in the end of data2 block */
  1124. map = (struct dx_map_entry *) (data2 + blocksize);
  1125. count = dx_make_map((struct ext4_dir_entry_2 *) data1,
  1126. blocksize, hinfo, map);
  1127. map -= count;
  1128. dx_sort_map(map, count);
  1129. /* Split the existing block in the middle, size-wise */
  1130. size = 0;
  1131. move = 0;
  1132. for (i = count-1; i >= 0; i--) {
  1133. /* is more than half of this entry in 2nd half of the block? */
  1134. if (size + map[i].size/2 > blocksize/2)
  1135. break;
  1136. size += map[i].size;
  1137. move++;
  1138. }
  1139. /* map index at which we will split */
  1140. split = count - move;
  1141. hash2 = map[split].hash;
  1142. continued = hash2 == map[split - 1].hash;
  1143. dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
  1144. (unsigned long)dx_get_block(frame->at),
  1145. hash2, split, count-split));
  1146. /* Fancy dance to stay within two buffers */
  1147. de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
  1148. de = dx_pack_dirents(data1, blocksize);
  1149. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1150. blocksize);
  1151. de2->rec_len = ext4_rec_len_to_disk(data2 + blocksize - (char *) de2,
  1152. blocksize);
  1153. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
  1154. dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
  1155. /* Which block gets the new entry? */
  1156. if (hinfo->hash >= hash2)
  1157. {
  1158. swap(*bh, bh2);
  1159. de = de2;
  1160. }
  1161. dx_insert_block(frame, hash2 + continued, newblock);
  1162. err = ext4_handle_dirty_metadata(handle, dir, bh2);
  1163. if (err)
  1164. goto journal_error;
  1165. err = ext4_handle_dirty_metadata(handle, dir, frame->bh);
  1166. if (err)
  1167. goto journal_error;
  1168. brelse(bh2);
  1169. dxtrace(dx_show_index("frame", frame->entries));
  1170. return de;
  1171. journal_error:
  1172. brelse(*bh);
  1173. brelse(bh2);
  1174. *bh = NULL;
  1175. ext4_std_error(dir->i_sb, err);
  1176. errout:
  1177. *error = err;
  1178. return NULL;
  1179. }
  1180. /*
  1181. * Add a new entry into a directory (leaf) block. If de is non-NULL,
  1182. * it points to a directory entry which is guaranteed to be large
  1183. * enough for new directory entry. If de is NULL, then
  1184. * add_dirent_to_buf will attempt search the directory block for
  1185. * space. It will return -ENOSPC if no space is available, and -EIO
  1186. * and -EEXIST if directory entry already exists.
  1187. */
  1188. static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
  1189. struct inode *inode, struct ext4_dir_entry_2 *de,
  1190. struct buffer_head *bh)
  1191. {
  1192. struct inode *dir = dentry->d_parent->d_inode;
  1193. const char *name = dentry->d_name.name;
  1194. int namelen = dentry->d_name.len;
  1195. unsigned int offset = 0;
  1196. unsigned int blocksize = dir->i_sb->s_blocksize;
  1197. unsigned short reclen;
  1198. int nlen, rlen, err;
  1199. char *top;
  1200. reclen = EXT4_DIR_REC_LEN(namelen);
  1201. if (!de) {
  1202. de = (struct ext4_dir_entry_2 *)bh->b_data;
  1203. top = bh->b_data + blocksize - reclen;
  1204. while ((char *) de <= top) {
  1205. if (!ext4_check_dir_entry("ext4_add_entry", dir, de,
  1206. bh, offset))
  1207. return -EIO;
  1208. if (ext4_match(namelen, name, de))
  1209. return -EEXIST;
  1210. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1211. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1212. if ((de->inode? rlen - nlen: rlen) >= reclen)
  1213. break;
  1214. de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
  1215. offset += rlen;
  1216. }
  1217. if ((char *) de > top)
  1218. return -ENOSPC;
  1219. }
  1220. BUFFER_TRACE(bh, "get_write_access");
  1221. err = ext4_journal_get_write_access(handle, bh);
  1222. if (err) {
  1223. ext4_std_error(dir->i_sb, err);
  1224. return err;
  1225. }
  1226. /* By now the buffer is marked for journaling */
  1227. nlen = EXT4_DIR_REC_LEN(de->name_len);
  1228. rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
  1229. if (de->inode) {
  1230. struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
  1231. de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
  1232. de->rec_len = ext4_rec_len_to_disk(nlen, blocksize);
  1233. de = de1;
  1234. }
  1235. de->file_type = EXT4_FT_UNKNOWN;
  1236. if (inode) {
  1237. de->inode = cpu_to_le32(inode->i_ino);
  1238. ext4_set_de_type(dir->i_sb, de, inode->i_mode);
  1239. } else
  1240. de->inode = 0;
  1241. de->name_len = namelen;
  1242. memcpy(de->name, name, namelen);
  1243. /*
  1244. * XXX shouldn't update any times until successful
  1245. * completion of syscall, but too many callers depend
  1246. * on this.
  1247. *
  1248. * XXX similarly, too many callers depend on
  1249. * ext4_new_inode() setting the times, but error
  1250. * recovery deletes the inode, so the worst that can
  1251. * happen is that the times are slightly out of date
  1252. * and/or different from the directory change time.
  1253. */
  1254. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  1255. ext4_update_dx_flag(dir);
  1256. dir->i_version++;
  1257. ext4_mark_inode_dirty(handle, dir);
  1258. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1259. err = ext4_handle_dirty_metadata(handle, dir, bh);
  1260. if (err)
  1261. ext4_std_error(dir->i_sb, err);
  1262. return 0;
  1263. }
  1264. /*
  1265. * This converts a one block unindexed directory to a 3 block indexed
  1266. * directory, and adds the dentry to the indexed directory.
  1267. */
  1268. static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
  1269. struct inode *inode, struct buffer_head *bh)
  1270. {
  1271. struct inode *dir = dentry->d_parent->d_inode;
  1272. const char *name = dentry->d_name.name;
  1273. int namelen = dentry->d_name.len;
  1274. struct buffer_head *bh2;
  1275. struct dx_root *root;
  1276. struct dx_frame frames[2], *frame;
  1277. struct dx_entry *entries;
  1278. struct ext4_dir_entry_2 *de, *de2;
  1279. char *data1, *top;
  1280. unsigned len;
  1281. int retval;
  1282. unsigned blocksize;
  1283. struct dx_hash_info hinfo;
  1284. ext4_lblk_t block;
  1285. struct fake_dirent *fde;
  1286. blocksize = dir->i_sb->s_blocksize;
  1287. dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
  1288. retval = ext4_journal_get_write_access(handle, bh);
  1289. if (retval) {
  1290. ext4_std_error(dir->i_sb, retval);
  1291. brelse(bh);
  1292. return retval;
  1293. }
  1294. root = (struct dx_root *) bh->b_data;
  1295. /* The 0th block becomes the root, move the dirents out */
  1296. fde = &root->dotdot;
  1297. de = (struct ext4_dir_entry_2 *)((char *)fde +
  1298. ext4_rec_len_from_disk(fde->rec_len, blocksize));
  1299. if ((char *) de >= (((char *) root) + blocksize)) {
  1300. ext4_error(dir->i_sb, __func__,
  1301. "invalid rec_len for '..' in inode %lu",
  1302. dir->i_ino);
  1303. brelse(bh);
  1304. return -EIO;
  1305. }
  1306. len = ((char *) root) + blocksize - (char *) de;
  1307. /* Allocate new block for the 0th block's dirents */
  1308. bh2 = ext4_append(handle, dir, &block, &retval);
  1309. if (!(bh2)) {
  1310. brelse(bh);
  1311. return retval;
  1312. }
  1313. EXT4_I(dir)->i_flags |= EXT4_INDEX_FL;
  1314. data1 = bh2->b_data;
  1315. memcpy (data1, de, len);
  1316. de = (struct ext4_dir_entry_2 *) data1;
  1317. top = data1 + len;
  1318. while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
  1319. de = de2;
  1320. de->rec_len = ext4_rec_len_to_disk(data1 + blocksize - (char *) de,
  1321. blocksize);
  1322. /* Initialize the root; the dot dirents already exist */
  1323. de = (struct ext4_dir_entry_2 *) (&root->dotdot);
  1324. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
  1325. blocksize);
  1326. memset (&root->info, 0, sizeof(root->info));
  1327. root->info.info_length = sizeof(root->info);
  1328. root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
  1329. entries = root->entries;
  1330. dx_set_block(entries, 1);
  1331. dx_set_count(entries, 1);
  1332. dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
  1333. /* Initialize as for dx_probe */
  1334. hinfo.hash_version = root->info.hash_version;
  1335. if (hinfo.hash_version <= DX_HASH_TEA)
  1336. hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
  1337. hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
  1338. ext4fs_dirhash(name, namelen, &hinfo);
  1339. frame = frames;
  1340. frame->entries = entries;
  1341. frame->at = entries;
  1342. frame->bh = bh;
  1343. bh = bh2;
  1344. de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
  1345. dx_release (frames);
  1346. if (!(de))
  1347. return retval;
  1348. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1349. brelse(bh);
  1350. return retval;
  1351. }
  1352. /*
  1353. * ext4_add_entry()
  1354. *
  1355. * adds a file entry to the specified directory, using the same
  1356. * semantics as ext4_find_entry(). It returns NULL if it failed.
  1357. *
  1358. * NOTE!! The inode part of 'de' is left at 0 - which means you
  1359. * may not sleep between calling this and putting something into
  1360. * the entry, as someone else might have used it while you slept.
  1361. */
  1362. static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
  1363. struct inode *inode)
  1364. {
  1365. struct inode *dir = dentry->d_parent->d_inode;
  1366. struct buffer_head *bh;
  1367. struct ext4_dir_entry_2 *de;
  1368. struct super_block *sb;
  1369. int retval;
  1370. int dx_fallback=0;
  1371. unsigned blocksize;
  1372. ext4_lblk_t block, blocks;
  1373. sb = dir->i_sb;
  1374. blocksize = sb->s_blocksize;
  1375. if (!dentry->d_name.len)
  1376. return -EINVAL;
  1377. if (is_dx(dir)) {
  1378. retval = ext4_dx_add_entry(handle, dentry, inode);
  1379. if (!retval || (retval != ERR_BAD_DX_DIR))
  1380. return retval;
  1381. EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL;
  1382. dx_fallback++;
  1383. ext4_mark_inode_dirty(handle, dir);
  1384. }
  1385. blocks = dir->i_size >> sb->s_blocksize_bits;
  1386. for (block = 0; block < blocks; block++) {
  1387. bh = ext4_bread(handle, dir, block, 0, &retval);
  1388. if(!bh)
  1389. return retval;
  1390. retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1391. if (retval != -ENOSPC) {
  1392. brelse(bh);
  1393. return retval;
  1394. }
  1395. if (blocks == 1 && !dx_fallback &&
  1396. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
  1397. return make_indexed_dir(handle, dentry, inode, bh);
  1398. brelse(bh);
  1399. }
  1400. bh = ext4_append(handle, dir, &block, &retval);
  1401. if (!bh)
  1402. return retval;
  1403. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1404. de->inode = 0;
  1405. de->rec_len = ext4_rec_len_to_disk(blocksize, blocksize);
  1406. retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1407. brelse(bh);
  1408. return retval;
  1409. }
  1410. /*
  1411. * Returns 0 for success, or a negative error value
  1412. */
  1413. static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
  1414. struct inode *inode)
  1415. {
  1416. struct dx_frame frames[2], *frame;
  1417. struct dx_entry *entries, *at;
  1418. struct dx_hash_info hinfo;
  1419. struct buffer_head *bh;
  1420. struct inode *dir = dentry->d_parent->d_inode;
  1421. struct super_block *sb = dir->i_sb;
  1422. struct ext4_dir_entry_2 *de;
  1423. int err;
  1424. frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
  1425. if (!frame)
  1426. return err;
  1427. entries = frame->entries;
  1428. at = frame->at;
  1429. if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
  1430. goto cleanup;
  1431. BUFFER_TRACE(bh, "get_write_access");
  1432. err = ext4_journal_get_write_access(handle, bh);
  1433. if (err)
  1434. goto journal_error;
  1435. err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
  1436. if (err != -ENOSPC)
  1437. goto cleanup;
  1438. /* Block full, should compress but for now just split */
  1439. dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
  1440. dx_get_count(entries), dx_get_limit(entries)));
  1441. /* Need to split index? */
  1442. if (dx_get_count(entries) == dx_get_limit(entries)) {
  1443. ext4_lblk_t newblock;
  1444. unsigned icount = dx_get_count(entries);
  1445. int levels = frame - frames;
  1446. struct dx_entry *entries2;
  1447. struct dx_node *node2;
  1448. struct buffer_head *bh2;
  1449. if (levels && (dx_get_count(frames->entries) ==
  1450. dx_get_limit(frames->entries))) {
  1451. ext4_warning(sb, __func__,
  1452. "Directory index full!");
  1453. err = -ENOSPC;
  1454. goto cleanup;
  1455. }
  1456. bh2 = ext4_append (handle, dir, &newblock, &err);
  1457. if (!(bh2))
  1458. goto cleanup;
  1459. node2 = (struct dx_node *)(bh2->b_data);
  1460. entries2 = node2->entries;
  1461. memset(&node2->fake, 0, sizeof(struct fake_dirent));
  1462. node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
  1463. sb->s_blocksize);
  1464. BUFFER_TRACE(frame->bh, "get_write_access");
  1465. err = ext4_journal_get_write_access(handle, frame->bh);
  1466. if (err)
  1467. goto journal_error;
  1468. if (levels) {
  1469. unsigned icount1 = icount/2, icount2 = icount - icount1;
  1470. unsigned hash2 = dx_get_hash(entries + icount1);
  1471. dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
  1472. icount1, icount2));
  1473. BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
  1474. err = ext4_journal_get_write_access(handle,
  1475. frames[0].bh);
  1476. if (err)
  1477. goto journal_error;
  1478. memcpy((char *) entries2, (char *) (entries + icount1),
  1479. icount2 * sizeof(struct dx_entry));
  1480. dx_set_count(entries, icount1);
  1481. dx_set_count(entries2, icount2);
  1482. dx_set_limit(entries2, dx_node_limit(dir));
  1483. /* Which index block gets the new entry? */
  1484. if (at - entries >= icount1) {
  1485. frame->at = at = at - entries - icount1 + entries2;
  1486. frame->entries = entries = entries2;
  1487. swap(frame->bh, bh2);
  1488. }
  1489. dx_insert_block(frames + 0, hash2, newblock);
  1490. dxtrace(dx_show_index("node", frames[1].entries));
  1491. dxtrace(dx_show_index("node",
  1492. ((struct dx_node *) bh2->b_data)->entries));
  1493. err = ext4_handle_dirty_metadata(handle, inode, bh2);
  1494. if (err)
  1495. goto journal_error;
  1496. brelse (bh2);
  1497. } else {
  1498. dxtrace(printk(KERN_DEBUG
  1499. "Creating second level index...\n"));
  1500. memcpy((char *) entries2, (char *) entries,
  1501. icount * sizeof(struct dx_entry));
  1502. dx_set_limit(entries2, dx_node_limit(dir));
  1503. /* Set up root */
  1504. dx_set_count(entries, 1);
  1505. dx_set_block(entries + 0, newblock);
  1506. ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
  1507. /* Add new access path frame */
  1508. frame = frames + 1;
  1509. frame->at = at = at - entries + entries2;
  1510. frame->entries = entries = entries2;
  1511. frame->bh = bh2;
  1512. err = ext4_journal_get_write_access(handle,
  1513. frame->bh);
  1514. if (err)
  1515. goto journal_error;
  1516. }
  1517. ext4_handle_dirty_metadata(handle, inode, frames[0].bh);
  1518. }
  1519. de = do_split(handle, dir, &bh, frame, &hinfo, &err);
  1520. if (!de)
  1521. goto cleanup;
  1522. err = add_dirent_to_buf(handle, dentry, inode, de, bh);
  1523. goto cleanup;
  1524. journal_error:
  1525. ext4_std_error(dir->i_sb, err);
  1526. cleanup:
  1527. if (bh)
  1528. brelse(bh);
  1529. dx_release(frames);
  1530. return err;
  1531. }
  1532. /*
  1533. * ext4_delete_entry deletes a directory entry by merging it with the
  1534. * previous entry
  1535. */
  1536. static int ext4_delete_entry(handle_t *handle,
  1537. struct inode *dir,
  1538. struct ext4_dir_entry_2 *de_del,
  1539. struct buffer_head *bh)
  1540. {
  1541. struct ext4_dir_entry_2 *de, *pde;
  1542. unsigned int blocksize = dir->i_sb->s_blocksize;
  1543. int i;
  1544. i = 0;
  1545. pde = NULL;
  1546. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1547. while (i < bh->b_size) {
  1548. if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i))
  1549. return -EIO;
  1550. if (de == de_del) {
  1551. BUFFER_TRACE(bh, "get_write_access");
  1552. ext4_journal_get_write_access(handle, bh);
  1553. if (pde)
  1554. pde->rec_len = ext4_rec_len_to_disk(
  1555. ext4_rec_len_from_disk(pde->rec_len,
  1556. blocksize) +
  1557. ext4_rec_len_from_disk(de->rec_len,
  1558. blocksize),
  1559. blocksize);
  1560. else
  1561. de->inode = 0;
  1562. dir->i_version++;
  1563. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1564. ext4_handle_dirty_metadata(handle, dir, bh);
  1565. return 0;
  1566. }
  1567. i += ext4_rec_len_from_disk(de->rec_len, blocksize);
  1568. pde = de;
  1569. de = ext4_next_entry(de, blocksize);
  1570. }
  1571. return -ENOENT;
  1572. }
  1573. /*
  1574. * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
  1575. * since this indicates that nlinks count was previously 1.
  1576. */
  1577. static void ext4_inc_count(handle_t *handle, struct inode *inode)
  1578. {
  1579. inc_nlink(inode);
  1580. if (is_dx(inode) && inode->i_nlink > 1) {
  1581. /* limit is 16-bit i_links_count */
  1582. if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
  1583. inode->i_nlink = 1;
  1584. EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
  1585. EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
  1586. }
  1587. }
  1588. }
  1589. /*
  1590. * If a directory had nlink == 1, then we should let it be 1. This indicates
  1591. * directory has >EXT4_LINK_MAX subdirs.
  1592. */
  1593. static void ext4_dec_count(handle_t *handle, struct inode *inode)
  1594. {
  1595. drop_nlink(inode);
  1596. if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0)
  1597. inc_nlink(inode);
  1598. }
  1599. static int ext4_add_nondir(handle_t *handle,
  1600. struct dentry *dentry, struct inode *inode)
  1601. {
  1602. int err = ext4_add_entry(handle, dentry, inode);
  1603. if (!err) {
  1604. ext4_mark_inode_dirty(handle, inode);
  1605. d_instantiate(dentry, inode);
  1606. unlock_new_inode(inode);
  1607. return 0;
  1608. }
  1609. drop_nlink(inode);
  1610. unlock_new_inode(inode);
  1611. iput(inode);
  1612. return err;
  1613. }
  1614. /*
  1615. * By the time this is called, we already have created
  1616. * the directory cache entry for the new file, but it
  1617. * is so far negative - it has no inode.
  1618. *
  1619. * If the create succeeds, we fill in the inode information
  1620. * with d_instantiate().
  1621. */
  1622. static int ext4_create(struct inode *dir, struct dentry *dentry, int mode,
  1623. struct nameidata *nd)
  1624. {
  1625. handle_t *handle;
  1626. struct inode *inode;
  1627. int err, retries = 0;
  1628. vfs_dq_init(dir);
  1629. retry:
  1630. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1631. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1632. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1633. if (IS_ERR(handle))
  1634. return PTR_ERR(handle);
  1635. if (IS_DIRSYNC(dir))
  1636. ext4_handle_sync(handle);
  1637. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0);
  1638. err = PTR_ERR(inode);
  1639. if (!IS_ERR(inode)) {
  1640. inode->i_op = &ext4_file_inode_operations;
  1641. inode->i_fop = &ext4_file_operations;
  1642. ext4_set_aops(inode);
  1643. err = ext4_add_nondir(handle, dentry, inode);
  1644. }
  1645. ext4_journal_stop(handle);
  1646. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1647. goto retry;
  1648. return err;
  1649. }
  1650. static int ext4_mknod(struct inode *dir, struct dentry *dentry,
  1651. int mode, dev_t rdev)
  1652. {
  1653. handle_t *handle;
  1654. struct inode *inode;
  1655. int err, retries = 0;
  1656. if (!new_valid_dev(rdev))
  1657. return -EINVAL;
  1658. vfs_dq_init(dir);
  1659. retry:
  1660. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1661. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1662. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1663. if (IS_ERR(handle))
  1664. return PTR_ERR(handle);
  1665. if (IS_DIRSYNC(dir))
  1666. ext4_handle_sync(handle);
  1667. inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0);
  1668. err = PTR_ERR(inode);
  1669. if (!IS_ERR(inode)) {
  1670. init_special_inode(inode, inode->i_mode, rdev);
  1671. #ifdef CONFIG_EXT4_FS_XATTR
  1672. inode->i_op = &ext4_special_inode_operations;
  1673. #endif
  1674. err = ext4_add_nondir(handle, dentry, inode);
  1675. }
  1676. ext4_journal_stop(handle);
  1677. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1678. goto retry;
  1679. return err;
  1680. }
  1681. static int ext4_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1682. {
  1683. handle_t *handle;
  1684. struct inode *inode;
  1685. struct buffer_head *dir_block;
  1686. struct ext4_dir_entry_2 *de;
  1687. unsigned int blocksize = dir->i_sb->s_blocksize;
  1688. int err, retries = 0;
  1689. if (EXT4_DIR_LINK_MAX(dir))
  1690. return -EMLINK;
  1691. vfs_dq_init(dir);
  1692. retry:
  1693. handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
  1694. EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
  1695. EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
  1696. if (IS_ERR(handle))
  1697. return PTR_ERR(handle);
  1698. if (IS_DIRSYNC(dir))
  1699. ext4_handle_sync(handle);
  1700. inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
  1701. &dentry->d_name, 0);
  1702. err = PTR_ERR(inode);
  1703. if (IS_ERR(inode))
  1704. goto out_stop;
  1705. inode->i_op = &ext4_dir_inode_operations;
  1706. inode->i_fop = &ext4_dir_operations;
  1707. inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  1708. dir_block = ext4_bread(handle, inode, 0, 1, &err);
  1709. if (!dir_block)
  1710. goto out_clear_inode;
  1711. BUFFER_TRACE(dir_block, "get_write_access");
  1712. ext4_journal_get_write_access(handle, dir_block);
  1713. de = (struct ext4_dir_entry_2 *) dir_block->b_data;
  1714. de->inode = cpu_to_le32(inode->i_ino);
  1715. de->name_len = 1;
  1716. de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
  1717. blocksize);
  1718. strcpy(de->name, ".");
  1719. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1720. de = ext4_next_entry(de, blocksize);
  1721. de->inode = cpu_to_le32(dir->i_ino);
  1722. de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(1),
  1723. blocksize);
  1724. de->name_len = 2;
  1725. strcpy(de->name, "..");
  1726. ext4_set_de_type(dir->i_sb, de, S_IFDIR);
  1727. inode->i_nlink = 2;
  1728. BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
  1729. ext4_handle_dirty_metadata(handle, dir, dir_block);
  1730. brelse(dir_block);
  1731. ext4_mark_inode_dirty(handle, inode);
  1732. err = ext4_add_entry(handle, dentry, inode);
  1733. if (err) {
  1734. out_clear_inode:
  1735. clear_nlink(inode);
  1736. unlock_new_inode(inode);
  1737. ext4_mark_inode_dirty(handle, inode);
  1738. iput(inode);
  1739. goto out_stop;
  1740. }
  1741. ext4_inc_count(handle, dir);
  1742. ext4_update_dx_flag(dir);
  1743. ext4_mark_inode_dirty(handle, dir);
  1744. d_instantiate(dentry, inode);
  1745. unlock_new_inode(inode);
  1746. out_stop:
  1747. ext4_journal_stop(handle);
  1748. if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
  1749. goto retry;
  1750. return err;
  1751. }
  1752. /*
  1753. * routine to check that the specified directory is empty (for rmdir)
  1754. */
  1755. static int empty_dir(struct inode *inode)
  1756. {
  1757. unsigned int offset;
  1758. struct buffer_head *bh;
  1759. struct ext4_dir_entry_2 *de, *de1;
  1760. struct super_block *sb;
  1761. int err = 0;
  1762. sb = inode->i_sb;
  1763. if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
  1764. !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
  1765. if (err)
  1766. ext4_error(inode->i_sb, __func__,
  1767. "error %d reading directory #%lu offset 0",
  1768. err, inode->i_ino);
  1769. else
  1770. ext4_warning(inode->i_sb, __func__,
  1771. "bad directory (dir #%lu) - no data block",
  1772. inode->i_ino);
  1773. return 1;
  1774. }
  1775. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1776. de1 = ext4_next_entry(de, sb->s_blocksize);
  1777. if (le32_to_cpu(de->inode) != inode->i_ino ||
  1778. !le32_to_cpu(de1->inode) ||
  1779. strcmp(".", de->name) ||
  1780. strcmp("..", de1->name)) {
  1781. ext4_warning(inode->i_sb, "empty_dir",
  1782. "bad directory (dir #%lu) - no `.' or `..'",
  1783. inode->i_ino);
  1784. brelse(bh);
  1785. return 1;
  1786. }
  1787. offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
  1788. ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
  1789. de = ext4_next_entry(de1, sb->s_blocksize);
  1790. while (offset < inode->i_size) {
  1791. if (!bh ||
  1792. (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
  1793. err = 0;
  1794. brelse(bh);
  1795. bh = ext4_bread(NULL, inode,
  1796. offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err);
  1797. if (!bh) {
  1798. if (err)
  1799. ext4_error(sb, __func__,
  1800. "error %d reading directory"
  1801. " #%lu offset %u",
  1802. err, inode->i_ino, offset);
  1803. offset += sb->s_blocksize;
  1804. continue;
  1805. }
  1806. de = (struct ext4_dir_entry_2 *) bh->b_data;
  1807. }
  1808. if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) {
  1809. de = (struct ext4_dir_entry_2 *)(bh->b_data +
  1810. sb->s_blocksize);
  1811. offset = (offset | (sb->s_blocksize - 1)) + 1;
  1812. continue;
  1813. }
  1814. if (le32_to_cpu(de->inode)) {
  1815. brelse(bh);
  1816. return 0;
  1817. }
  1818. offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
  1819. de = ext4_next_entry(de, sb->s_blocksize);
  1820. }
  1821. brelse(bh);
  1822. return 1;
  1823. }
  1824. /* ext4_orphan_add() links an unlinked or truncated inode into a list of
  1825. * such inodes, starting at the superblock, in case we crash before the
  1826. * file is closed/deleted, or in case the inode truncate spans multiple
  1827. * transactions and the last transaction is not recovered after a crash.
  1828. *
  1829. * At filesystem recovery time, we walk this list deleting unlinked
  1830. * inodes and truncating linked inodes in ext4_orphan_cleanup().
  1831. */
  1832. int ext4_orphan_add(handle_t *handle, struct inode *inode)
  1833. {
  1834. struct super_block *sb = inode->i_sb;
  1835. struct ext4_iloc iloc;
  1836. int err = 0, rc;
  1837. if (!ext4_handle_valid(handle))
  1838. return 0;
  1839. mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
  1840. if (!list_empty(&EXT4_I(inode)->i_orphan))
  1841. goto out_unlock;
  1842. /* Orphan handling is only valid for files with data blocks
  1843. * being truncated, or files being unlinked. */
  1844. /* @@@ FIXME: Observation from aviro:
  1845. * I think I can trigger J_ASSERT in ext4_orphan_add(). We block
  1846. * here (on s_orphan_lock), so race with ext4_link() which might bump
  1847. * ->i_nlink. For, say it, character device. Not a regular file,
  1848. * not a directory, not a symlink and ->i_nlink > 0.
  1849. *
  1850. * tytso, 4/25/2009: I'm not sure how that could happen;
  1851. * shouldn't the fs core protect us from these sort of
  1852. * unlink()/link() races?
  1853. */
  1854. J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1855. S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
  1856. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  1857. err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
  1858. if (err)
  1859. goto out_unlock;
  1860. err = ext4_reserve_inode_write(handle, inode, &iloc);
  1861. if (err)
  1862. goto out_unlock;
  1863. /* Insert this inode at the head of the on-disk orphan list... */
  1864. NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
  1865. EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
  1866. err = ext4_handle_dirty_metadata(handle, inode, EXT4_SB(sb)->s_sbh);
  1867. rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
  1868. if (!err)
  1869. err = rc;
  1870. /* Only add to the head of the in-memory list if all the
  1871. * previous operations succeeded. If the orphan_add is going to
  1872. * fail (possibly taking the journal offline), we can't risk
  1873. * leaving the inode on the orphan list: stray orphan-list
  1874. * entries can cause panics at unmount time.
  1875. *
  1876. * This is safe: on error we're going to ignore the orphan list
  1877. * anyway on the next recovery. */
  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, inode, 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. vfs_dq_init(dir);
  1963. vfs_dq_init(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, "ext4_rmdir",
  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. vfs_dq_init(dir);
  2014. vfs_dq_init(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, "ext4_unlink",
  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. vfs_dq_init(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_I(inode)->i_flags &= ~EXT4_EXTENTS_FL;
  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. vfs_dq_init(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. vfs_dq_init(old_dir);
  2158. vfs_dq_init(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. vfs_dq_init(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, "ext4_rename",
  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. };