namei.c 72 KB

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