namei.c 69 KB

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