namei.c 70 KB

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