namei.c 92 KB

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