namei.c 72 KB

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