namei.c 85 KB

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