namei.c 101 KB

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