xattr.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/xattr.c
  4. *
  5. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  6. *
  7. * Fix by Harrison Xing <harrison@mountainviewdata.com>.
  8. * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
  9. * Extended attributes for symlinks and special files added per
  10. * suggestion of Luka Renko <luka.renko@hermes.si>.
  11. * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
  12. * Red Hat Inc.
  13. * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
  14. * and Andreas Gruenbacher <agruen@suse.de>.
  15. */
  16. /*
  17. * Extended attributes are stored directly in inodes (on file systems with
  18. * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
  19. * field contains the block number if an inode uses an additional block. All
  20. * attributes must fit in the inode and one additional block. Blocks that
  21. * contain the identical set of attributes may be shared among several inodes.
  22. * Identical blocks are detected by keeping a cache of blocks that have
  23. * recently been accessed.
  24. *
  25. * The attributes in inodes and on blocks have a different header; the entries
  26. * are stored in the same format:
  27. *
  28. * +------------------+
  29. * | header |
  30. * | entry 1 | |
  31. * | entry 2 | | growing downwards
  32. * | entry 3 | v
  33. * | four null bytes |
  34. * | . . . |
  35. * | value 1 | ^
  36. * | value 3 | | growing upwards
  37. * | value 2 | |
  38. * +------------------+
  39. *
  40. * The header is followed by multiple entry descriptors. In disk blocks, the
  41. * entry descriptors are kept sorted. In inodes, they are unsorted. The
  42. * attribute values are aligned to the end of the block in no specific order.
  43. *
  44. * Locking strategy
  45. * ----------------
  46. * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
  47. * EA blocks are only changed if they are exclusive to an inode, so
  48. * holding xattr_sem also means that nothing but the EA block's reference
  49. * count can change. Multiple writers to the same block are synchronized
  50. * by the buffer lock.
  51. */
  52. #include <linux/init.h>
  53. #include <linux/fs.h>
  54. #include <linux/slab.h>
  55. #include <linux/mbcache.h>
  56. #include <linux/quotaops.h>
  57. #include <linux/iversion.h>
  58. #include "ext4_jbd2.h"
  59. #include "ext4.h"
  60. #include "xattr.h"
  61. #include "acl.h"
  62. #ifdef EXT4_XATTR_DEBUG
  63. # define ea_idebug(inode, fmt, ...) \
  64. printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
  65. inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
  66. # define ea_bdebug(bh, fmt, ...) \
  67. printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
  68. bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
  69. #else
  70. # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  71. # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  72. #endif
  73. static void ext4_xattr_block_cache_insert(struct mb_cache *,
  74. struct buffer_head *);
  75. static struct buffer_head *
  76. ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
  77. struct mb_cache_entry **);
  78. static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
  79. size_t value_count);
  80. static void ext4_xattr_rehash(struct ext4_xattr_header *);
  81. static const struct xattr_handler * const ext4_xattr_handler_map[] = {
  82. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  83. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  84. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  85. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  86. #endif
  87. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  88. #ifdef CONFIG_EXT4_FS_SECURITY
  89. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  90. #endif
  91. };
  92. const struct xattr_handler *ext4_xattr_handlers[] = {
  93. &ext4_xattr_user_handler,
  94. &ext4_xattr_trusted_handler,
  95. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  96. &posix_acl_access_xattr_handler,
  97. &posix_acl_default_xattr_handler,
  98. #endif
  99. #ifdef CONFIG_EXT4_FS_SECURITY
  100. &ext4_xattr_security_handler,
  101. #endif
  102. NULL
  103. };
  104. #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
  105. inode->i_sb->s_fs_info)->s_ea_block_cache)
  106. #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
  107. inode->i_sb->s_fs_info)->s_ea_inode_cache)
  108. static int
  109. ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
  110. struct inode *inode);
  111. #ifdef CONFIG_LOCKDEP
  112. void ext4_xattr_inode_set_class(struct inode *ea_inode)
  113. {
  114. lockdep_set_subclass(&ea_inode->i_rwsem, 1);
  115. }
  116. #endif
  117. static __le32 ext4_xattr_block_csum(struct inode *inode,
  118. sector_t block_nr,
  119. struct ext4_xattr_header *hdr)
  120. {
  121. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  122. __u32 csum;
  123. __le64 dsk_block_nr = cpu_to_le64(block_nr);
  124. __u32 dummy_csum = 0;
  125. int offset = offsetof(struct ext4_xattr_header, h_checksum);
  126. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
  127. sizeof(dsk_block_nr));
  128. csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
  129. csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
  130. offset += sizeof(dummy_csum);
  131. csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
  132. EXT4_BLOCK_SIZE(inode->i_sb) - offset);
  133. return cpu_to_le32(csum);
  134. }
  135. static int ext4_xattr_block_csum_verify(struct inode *inode,
  136. struct buffer_head *bh)
  137. {
  138. struct ext4_xattr_header *hdr = BHDR(bh);
  139. int ret = 1;
  140. if (ext4_has_metadata_csum(inode->i_sb)) {
  141. lock_buffer(bh);
  142. ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
  143. bh->b_blocknr, hdr));
  144. unlock_buffer(bh);
  145. }
  146. return ret;
  147. }
  148. static void ext4_xattr_block_csum_set(struct inode *inode,
  149. struct buffer_head *bh)
  150. {
  151. if (ext4_has_metadata_csum(inode->i_sb))
  152. BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
  153. bh->b_blocknr, BHDR(bh));
  154. }
  155. static inline const struct xattr_handler *
  156. ext4_xattr_handler(int name_index)
  157. {
  158. const struct xattr_handler *handler = NULL;
  159. if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
  160. handler = ext4_xattr_handler_map[name_index];
  161. return handler;
  162. }
  163. static int
  164. ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
  165. void *value_start)
  166. {
  167. struct ext4_xattr_entry *e = entry;
  168. /* Find the end of the names list */
  169. while (!IS_LAST_ENTRY(e)) {
  170. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
  171. if ((void *)next >= end)
  172. return -EFSCORRUPTED;
  173. e = next;
  174. }
  175. /* Check the values */
  176. while (!IS_LAST_ENTRY(entry)) {
  177. u32 size = le32_to_cpu(entry->e_value_size);
  178. if (size > EXT4_XATTR_SIZE_MAX)
  179. return -EFSCORRUPTED;
  180. if (size != 0 && entry->e_value_inum == 0) {
  181. u16 offs = le16_to_cpu(entry->e_value_offs);
  182. void *value;
  183. /*
  184. * The value cannot overlap the names, and the value
  185. * with padding cannot extend beyond 'end'. Check both
  186. * the padded and unpadded sizes, since the size may
  187. * overflow to 0 when adding padding.
  188. */
  189. if (offs > end - value_start)
  190. return -EFSCORRUPTED;
  191. value = value_start + offs;
  192. if (value < (void *)e + sizeof(u32) ||
  193. size > end - value ||
  194. EXT4_XATTR_SIZE(size) > end - value)
  195. return -EFSCORRUPTED;
  196. }
  197. entry = EXT4_XATTR_NEXT(entry);
  198. }
  199. return 0;
  200. }
  201. static inline int
  202. __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
  203. const char *function, unsigned int line)
  204. {
  205. int error = -EFSCORRUPTED;
  206. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  207. BHDR(bh)->h_blocks != cpu_to_le32(1))
  208. goto errout;
  209. if (buffer_verified(bh))
  210. return 0;
  211. error = -EFSBADCRC;
  212. if (!ext4_xattr_block_csum_verify(inode, bh))
  213. goto errout;
  214. error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
  215. bh->b_data);
  216. errout:
  217. if (error)
  218. __ext4_error_inode(inode, function, line, 0,
  219. "corrupted xattr block %llu",
  220. (unsigned long long) bh->b_blocknr);
  221. else
  222. set_buffer_verified(bh);
  223. return error;
  224. }
  225. #define ext4_xattr_check_block(inode, bh) \
  226. __ext4_xattr_check_block((inode), (bh), __func__, __LINE__)
  227. static int
  228. __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
  229. void *end, const char *function, unsigned int line)
  230. {
  231. int error = -EFSCORRUPTED;
  232. if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
  233. (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
  234. goto errout;
  235. error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
  236. errout:
  237. if (error)
  238. __ext4_error_inode(inode, function, line, 0,
  239. "corrupted in-inode xattr");
  240. return error;
  241. }
  242. #define xattr_check_inode(inode, header, end) \
  243. __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
  244. static int
  245. xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
  246. void *end, int name_index, const char *name, int sorted)
  247. {
  248. struct ext4_xattr_entry *entry, *next;
  249. size_t name_len;
  250. int cmp = 1;
  251. if (name == NULL)
  252. return -EINVAL;
  253. name_len = strlen(name);
  254. for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
  255. next = EXT4_XATTR_NEXT(entry);
  256. if ((void *) next >= end) {
  257. EXT4_ERROR_INODE(inode, "corrupted xattr entries");
  258. return -EFSCORRUPTED;
  259. }
  260. cmp = name_index - entry->e_name_index;
  261. if (!cmp)
  262. cmp = name_len - entry->e_name_len;
  263. if (!cmp)
  264. cmp = memcmp(name, entry->e_name, name_len);
  265. if (cmp <= 0 && (sorted || cmp == 0))
  266. break;
  267. }
  268. *pentry = entry;
  269. return cmp ? -ENODATA : 0;
  270. }
  271. static u32
  272. ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
  273. {
  274. return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
  275. }
  276. static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
  277. {
  278. return ((u64)ea_inode->i_ctime.tv_sec << 32) |
  279. (u32) inode_peek_iversion_raw(ea_inode);
  280. }
  281. static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
  282. {
  283. ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
  284. inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
  285. }
  286. static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
  287. {
  288. return (u32)ea_inode->i_atime.tv_sec;
  289. }
  290. static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
  291. {
  292. ea_inode->i_atime.tv_sec = hash;
  293. }
  294. /*
  295. * Read the EA value from an inode.
  296. */
  297. static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
  298. {
  299. int blocksize = 1 << ea_inode->i_blkbits;
  300. int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
  301. int tail_size = (size % blocksize) ?: blocksize;
  302. struct buffer_head *bhs_inline[8];
  303. struct buffer_head **bhs = bhs_inline;
  304. int i, ret;
  305. if (bh_count > ARRAY_SIZE(bhs_inline)) {
  306. bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
  307. if (!bhs)
  308. return -ENOMEM;
  309. }
  310. ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
  311. true /* wait */, bhs);
  312. if (ret)
  313. goto free_bhs;
  314. for (i = 0; i < bh_count; i++) {
  315. /* There shouldn't be any holes in ea_inode. */
  316. if (!bhs[i]) {
  317. ret = -EFSCORRUPTED;
  318. goto put_bhs;
  319. }
  320. memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
  321. i < bh_count - 1 ? blocksize : tail_size);
  322. }
  323. ret = 0;
  324. put_bhs:
  325. for (i = 0; i < bh_count; i++)
  326. brelse(bhs[i]);
  327. free_bhs:
  328. if (bhs != bhs_inline)
  329. kfree(bhs);
  330. return ret;
  331. }
  332. #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
  333. static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
  334. u32 ea_inode_hash, struct inode **ea_inode)
  335. {
  336. struct inode *inode;
  337. int err;
  338. inode = ext4_iget(parent->i_sb, ea_ino);
  339. if (IS_ERR(inode)) {
  340. err = PTR_ERR(inode);
  341. ext4_error(parent->i_sb,
  342. "error while reading EA inode %lu err=%d", ea_ino,
  343. err);
  344. return err;
  345. }
  346. if (is_bad_inode(inode)) {
  347. ext4_error(parent->i_sb,
  348. "error while reading EA inode %lu is_bad_inode",
  349. ea_ino);
  350. err = -EIO;
  351. goto error;
  352. }
  353. if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
  354. ext4_error(parent->i_sb,
  355. "EA inode %lu does not have EXT4_EA_INODE_FL flag",
  356. ea_ino);
  357. err = -EINVAL;
  358. goto error;
  359. }
  360. ext4_xattr_inode_set_class(inode);
  361. /*
  362. * Check whether this is an old Lustre-style xattr inode. Lustre
  363. * implementation does not have hash validation, rather it has a
  364. * backpointer from ea_inode to the parent inode.
  365. */
  366. if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
  367. EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
  368. inode->i_generation == parent->i_generation) {
  369. ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
  370. ext4_xattr_inode_set_ref(inode, 1);
  371. } else {
  372. inode_lock(inode);
  373. inode->i_flags |= S_NOQUOTA;
  374. inode_unlock(inode);
  375. }
  376. *ea_inode = inode;
  377. return 0;
  378. error:
  379. iput(inode);
  380. return err;
  381. }
  382. static int
  383. ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
  384. struct ext4_xattr_entry *entry, void *buffer,
  385. size_t size)
  386. {
  387. u32 hash;
  388. /* Verify stored hash matches calculated hash. */
  389. hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
  390. if (hash != ext4_xattr_inode_get_hash(ea_inode))
  391. return -EFSCORRUPTED;
  392. if (entry) {
  393. __le32 e_hash, tmp_data;
  394. /* Verify entry hash. */
  395. tmp_data = cpu_to_le32(hash);
  396. e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
  397. &tmp_data, 1);
  398. if (e_hash != entry->e_hash)
  399. return -EFSCORRUPTED;
  400. }
  401. return 0;
  402. }
  403. /*
  404. * Read xattr value from the EA inode.
  405. */
  406. static int
  407. ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
  408. void *buffer, size_t size)
  409. {
  410. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
  411. struct inode *ea_inode;
  412. int err;
  413. err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
  414. le32_to_cpu(entry->e_hash), &ea_inode);
  415. if (err) {
  416. ea_inode = NULL;
  417. goto out;
  418. }
  419. if (i_size_read(ea_inode) != size) {
  420. ext4_warning_inode(ea_inode,
  421. "ea_inode file size=%llu entry size=%zu",
  422. i_size_read(ea_inode), size);
  423. err = -EFSCORRUPTED;
  424. goto out;
  425. }
  426. err = ext4_xattr_inode_read(ea_inode, buffer, size);
  427. if (err)
  428. goto out;
  429. if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
  430. err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
  431. size);
  432. if (err) {
  433. ext4_warning_inode(ea_inode,
  434. "EA inode hash validation failed");
  435. goto out;
  436. }
  437. if (ea_inode_cache)
  438. mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
  439. ext4_xattr_inode_get_hash(ea_inode),
  440. ea_inode->i_ino, true /* reusable */);
  441. }
  442. out:
  443. iput(ea_inode);
  444. return err;
  445. }
  446. static int
  447. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  448. void *buffer, size_t buffer_size)
  449. {
  450. struct buffer_head *bh = NULL;
  451. struct ext4_xattr_entry *entry;
  452. size_t size;
  453. void *end;
  454. int error;
  455. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  456. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  457. name_index, name, buffer, (long)buffer_size);
  458. error = -ENODATA;
  459. if (!EXT4_I(inode)->i_file_acl)
  460. goto cleanup;
  461. ea_idebug(inode, "reading block %llu",
  462. (unsigned long long)EXT4_I(inode)->i_file_acl);
  463. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  464. if (!bh)
  465. goto cleanup;
  466. ea_bdebug(bh, "b_count=%d, refcount=%d",
  467. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  468. error = ext4_xattr_check_block(inode, bh);
  469. if (error)
  470. goto cleanup;
  471. ext4_xattr_block_cache_insert(ea_block_cache, bh);
  472. entry = BFIRST(bh);
  473. end = bh->b_data + bh->b_size;
  474. error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
  475. if (error)
  476. goto cleanup;
  477. size = le32_to_cpu(entry->e_value_size);
  478. error = -ERANGE;
  479. if (unlikely(size > EXT4_XATTR_SIZE_MAX))
  480. goto cleanup;
  481. if (buffer) {
  482. if (size > buffer_size)
  483. goto cleanup;
  484. if (entry->e_value_inum) {
  485. error = ext4_xattr_inode_get(inode, entry, buffer,
  486. size);
  487. if (error)
  488. goto cleanup;
  489. } else {
  490. u16 offset = le16_to_cpu(entry->e_value_offs);
  491. void *p = bh->b_data + offset;
  492. if (unlikely(p + size > end))
  493. goto cleanup;
  494. memcpy(buffer, p, size);
  495. }
  496. }
  497. error = size;
  498. cleanup:
  499. brelse(bh);
  500. return error;
  501. }
  502. int
  503. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  504. void *buffer, size_t buffer_size)
  505. {
  506. struct ext4_xattr_ibody_header *header;
  507. struct ext4_xattr_entry *entry;
  508. struct ext4_inode *raw_inode;
  509. struct ext4_iloc iloc;
  510. size_t size;
  511. void *end;
  512. int error;
  513. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  514. return -ENODATA;
  515. error = ext4_get_inode_loc(inode, &iloc);
  516. if (error)
  517. return error;
  518. raw_inode = ext4_raw_inode(&iloc);
  519. header = IHDR(inode, raw_inode);
  520. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  521. error = xattr_check_inode(inode, header, end);
  522. if (error)
  523. goto cleanup;
  524. entry = IFIRST(header);
  525. error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
  526. if (error)
  527. goto cleanup;
  528. size = le32_to_cpu(entry->e_value_size);
  529. error = -ERANGE;
  530. if (unlikely(size > EXT4_XATTR_SIZE_MAX))
  531. goto cleanup;
  532. if (buffer) {
  533. if (size > buffer_size)
  534. goto cleanup;
  535. if (entry->e_value_inum) {
  536. error = ext4_xattr_inode_get(inode, entry, buffer,
  537. size);
  538. if (error)
  539. goto cleanup;
  540. } else {
  541. u16 offset = le16_to_cpu(entry->e_value_offs);
  542. void *p = (void *)IFIRST(header) + offset;
  543. if (unlikely(p + size > end))
  544. goto cleanup;
  545. memcpy(buffer, p, size);
  546. }
  547. }
  548. error = size;
  549. cleanup:
  550. brelse(iloc.bh);
  551. return error;
  552. }
  553. /*
  554. * ext4_xattr_get()
  555. *
  556. * Copy an extended attribute into the buffer
  557. * provided, or compute the buffer size required.
  558. * Buffer is NULL to compute the size of the buffer required.
  559. *
  560. * Returns a negative error number on failure, or the number of bytes
  561. * used / required on success.
  562. */
  563. int
  564. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  565. void *buffer, size_t buffer_size)
  566. {
  567. int error;
  568. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  569. return -EIO;
  570. if (strlen(name) > 255)
  571. return -ERANGE;
  572. down_read(&EXT4_I(inode)->xattr_sem);
  573. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  574. buffer_size);
  575. if (error == -ENODATA)
  576. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  577. buffer_size);
  578. up_read(&EXT4_I(inode)->xattr_sem);
  579. return error;
  580. }
  581. static int
  582. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  583. char *buffer, size_t buffer_size)
  584. {
  585. size_t rest = buffer_size;
  586. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  587. const struct xattr_handler *handler =
  588. ext4_xattr_handler(entry->e_name_index);
  589. if (handler && (!handler->list || handler->list(dentry))) {
  590. const char *prefix = handler->prefix ?: handler->name;
  591. size_t prefix_len = strlen(prefix);
  592. size_t size = prefix_len + entry->e_name_len + 1;
  593. if (buffer) {
  594. if (size > rest)
  595. return -ERANGE;
  596. memcpy(buffer, prefix, prefix_len);
  597. buffer += prefix_len;
  598. memcpy(buffer, entry->e_name, entry->e_name_len);
  599. buffer += entry->e_name_len;
  600. *buffer++ = 0;
  601. }
  602. rest -= size;
  603. }
  604. }
  605. return buffer_size - rest; /* total size */
  606. }
  607. static int
  608. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  609. {
  610. struct inode *inode = d_inode(dentry);
  611. struct buffer_head *bh = NULL;
  612. int error;
  613. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  614. buffer, (long)buffer_size);
  615. error = 0;
  616. if (!EXT4_I(inode)->i_file_acl)
  617. goto cleanup;
  618. ea_idebug(inode, "reading block %llu",
  619. (unsigned long long)EXT4_I(inode)->i_file_acl);
  620. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  621. error = -EIO;
  622. if (!bh)
  623. goto cleanup;
  624. ea_bdebug(bh, "b_count=%d, refcount=%d",
  625. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  626. error = ext4_xattr_check_block(inode, bh);
  627. if (error)
  628. goto cleanup;
  629. ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
  630. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  631. cleanup:
  632. brelse(bh);
  633. return error;
  634. }
  635. static int
  636. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  637. {
  638. struct inode *inode = d_inode(dentry);
  639. struct ext4_xattr_ibody_header *header;
  640. struct ext4_inode *raw_inode;
  641. struct ext4_iloc iloc;
  642. void *end;
  643. int error;
  644. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  645. return 0;
  646. error = ext4_get_inode_loc(inode, &iloc);
  647. if (error)
  648. return error;
  649. raw_inode = ext4_raw_inode(&iloc);
  650. header = IHDR(inode, raw_inode);
  651. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  652. error = xattr_check_inode(inode, header, end);
  653. if (error)
  654. goto cleanup;
  655. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  656. buffer, buffer_size);
  657. cleanup:
  658. brelse(iloc.bh);
  659. return error;
  660. }
  661. /*
  662. * Inode operation listxattr()
  663. *
  664. * d_inode(dentry)->i_rwsem: don't care
  665. *
  666. * Copy a list of attribute names into the buffer
  667. * provided, or compute the buffer size required.
  668. * Buffer is NULL to compute the size of the buffer required.
  669. *
  670. * Returns a negative error number on failure, or the number of bytes
  671. * used / required on success.
  672. */
  673. ssize_t
  674. ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  675. {
  676. int ret, ret2;
  677. down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  678. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  679. if (ret < 0)
  680. goto errout;
  681. if (buffer) {
  682. buffer += ret;
  683. buffer_size -= ret;
  684. }
  685. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  686. if (ret < 0)
  687. goto errout;
  688. ret += ret2;
  689. errout:
  690. up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  691. return ret;
  692. }
  693. /*
  694. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  695. * not set, set it.
  696. */
  697. static void ext4_xattr_update_super_block(handle_t *handle,
  698. struct super_block *sb)
  699. {
  700. if (ext4_has_feature_xattr(sb))
  701. return;
  702. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  703. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  704. ext4_set_feature_xattr(sb);
  705. ext4_handle_dirty_super(handle, sb);
  706. }
  707. }
  708. int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
  709. {
  710. struct ext4_iloc iloc = { .bh = NULL };
  711. struct buffer_head *bh = NULL;
  712. struct ext4_inode *raw_inode;
  713. struct ext4_xattr_ibody_header *header;
  714. struct ext4_xattr_entry *entry;
  715. qsize_t ea_inode_refs = 0;
  716. void *end;
  717. int ret;
  718. lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
  719. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  720. ret = ext4_get_inode_loc(inode, &iloc);
  721. if (ret)
  722. goto out;
  723. raw_inode = ext4_raw_inode(&iloc);
  724. header = IHDR(inode, raw_inode);
  725. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  726. ret = xattr_check_inode(inode, header, end);
  727. if (ret)
  728. goto out;
  729. for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
  730. entry = EXT4_XATTR_NEXT(entry))
  731. if (entry->e_value_inum)
  732. ea_inode_refs++;
  733. }
  734. if (EXT4_I(inode)->i_file_acl) {
  735. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  736. if (!bh) {
  737. ret = -EIO;
  738. goto out;
  739. }
  740. ret = ext4_xattr_check_block(inode, bh);
  741. if (ret)
  742. goto out;
  743. for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
  744. entry = EXT4_XATTR_NEXT(entry))
  745. if (entry->e_value_inum)
  746. ea_inode_refs++;
  747. }
  748. *usage = ea_inode_refs + 1;
  749. ret = 0;
  750. out:
  751. brelse(iloc.bh);
  752. brelse(bh);
  753. return ret;
  754. }
  755. static inline size_t round_up_cluster(struct inode *inode, size_t length)
  756. {
  757. struct super_block *sb = inode->i_sb;
  758. size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
  759. inode->i_blkbits);
  760. size_t mask = ~(cluster_size - 1);
  761. return (length + cluster_size - 1) & mask;
  762. }
  763. static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
  764. {
  765. int err;
  766. err = dquot_alloc_inode(inode);
  767. if (err)
  768. return err;
  769. err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
  770. if (err)
  771. dquot_free_inode(inode);
  772. return err;
  773. }
  774. static void ext4_xattr_inode_free_quota(struct inode *parent,
  775. struct inode *ea_inode,
  776. size_t len)
  777. {
  778. if (ea_inode &&
  779. ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
  780. return;
  781. dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
  782. dquot_free_inode(parent);
  783. }
  784. int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
  785. struct buffer_head *block_bh, size_t value_len,
  786. bool is_create)
  787. {
  788. int credits;
  789. int blocks;
  790. /*
  791. * 1) Owner inode update
  792. * 2) Ref count update on old xattr block
  793. * 3) new xattr block
  794. * 4) block bitmap update for new xattr block
  795. * 5) group descriptor for new xattr block
  796. * 6) block bitmap update for old xattr block
  797. * 7) group descriptor for old block
  798. *
  799. * 6 & 7 can happen if we have two racing threads T_a and T_b
  800. * which are each trying to set an xattr on inodes I_a and I_b
  801. * which were both initially sharing an xattr block.
  802. */
  803. credits = 7;
  804. /* Quota updates. */
  805. credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
  806. /*
  807. * In case of inline data, we may push out the data to a block,
  808. * so we need to reserve credits for this eventuality
  809. */
  810. if (inode && ext4_has_inline_data(inode))
  811. credits += ext4_writepage_trans_blocks(inode) + 1;
  812. /* We are done if ea_inode feature is not enabled. */
  813. if (!ext4_has_feature_ea_inode(sb))
  814. return credits;
  815. /* New ea_inode, inode map, block bitmap, group descriptor. */
  816. credits += 4;
  817. /* Data blocks. */
  818. blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  819. /* Indirection block or one level of extent tree. */
  820. blocks += 1;
  821. /* Block bitmap and group descriptor updates for each block. */
  822. credits += blocks * 2;
  823. /* Blocks themselves. */
  824. credits += blocks;
  825. if (!is_create) {
  826. /* Dereference ea_inode holding old xattr value.
  827. * Old ea_inode, inode map, block bitmap, group descriptor.
  828. */
  829. credits += 4;
  830. /* Data blocks for old ea_inode. */
  831. blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
  832. /* Indirection block or one level of extent tree for old
  833. * ea_inode.
  834. */
  835. blocks += 1;
  836. /* Block bitmap and group descriptor updates for each block. */
  837. credits += blocks * 2;
  838. }
  839. /* We may need to clone the existing xattr block in which case we need
  840. * to increment ref counts for existing ea_inodes referenced by it.
  841. */
  842. if (block_bh) {
  843. struct ext4_xattr_entry *entry = BFIRST(block_bh);
  844. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
  845. if (entry->e_value_inum)
  846. /* Ref count update on ea_inode. */
  847. credits += 1;
  848. }
  849. return credits;
  850. }
  851. static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode,
  852. int credits, struct buffer_head *bh,
  853. bool dirty, bool block_csum)
  854. {
  855. int error;
  856. if (!ext4_handle_valid(handle))
  857. return 0;
  858. if (handle->h_buffer_credits >= credits)
  859. return 0;
  860. error = ext4_journal_extend(handle, credits - handle->h_buffer_credits);
  861. if (!error)
  862. return 0;
  863. if (error < 0) {
  864. ext4_warning(inode->i_sb, "Extend journal (error %d)", error);
  865. return error;
  866. }
  867. if (bh && dirty) {
  868. if (block_csum)
  869. ext4_xattr_block_csum_set(inode, bh);
  870. error = ext4_handle_dirty_metadata(handle, NULL, bh);
  871. if (error) {
  872. ext4_warning(inode->i_sb, "Handle metadata (error %d)",
  873. error);
  874. return error;
  875. }
  876. }
  877. error = ext4_journal_restart(handle, credits);
  878. if (error) {
  879. ext4_warning(inode->i_sb, "Restart journal (error %d)", error);
  880. return error;
  881. }
  882. if (bh) {
  883. error = ext4_journal_get_write_access(handle, bh);
  884. if (error) {
  885. ext4_warning(inode->i_sb,
  886. "Get write access failed (error %d)",
  887. error);
  888. return error;
  889. }
  890. }
  891. return 0;
  892. }
  893. static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
  894. int ref_change)
  895. {
  896. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode);
  897. struct ext4_iloc iloc;
  898. s64 ref_count;
  899. u32 hash;
  900. int ret;
  901. inode_lock(ea_inode);
  902. ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
  903. if (ret) {
  904. iloc.bh = NULL;
  905. goto out;
  906. }
  907. ref_count = ext4_xattr_inode_get_ref(ea_inode);
  908. ref_count += ref_change;
  909. ext4_xattr_inode_set_ref(ea_inode, ref_count);
  910. if (ref_change > 0) {
  911. WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
  912. ea_inode->i_ino, ref_count);
  913. if (ref_count == 1) {
  914. WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
  915. ea_inode->i_ino, ea_inode->i_nlink);
  916. set_nlink(ea_inode, 1);
  917. ext4_orphan_del(handle, ea_inode);
  918. if (ea_inode_cache) {
  919. hash = ext4_xattr_inode_get_hash(ea_inode);
  920. mb_cache_entry_create(ea_inode_cache,
  921. GFP_NOFS, hash,
  922. ea_inode->i_ino,
  923. true /* reusable */);
  924. }
  925. }
  926. } else {
  927. WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
  928. ea_inode->i_ino, ref_count);
  929. if (ref_count == 0) {
  930. WARN_ONCE(ea_inode->i_nlink != 1,
  931. "EA inode %lu i_nlink=%u",
  932. ea_inode->i_ino, ea_inode->i_nlink);
  933. clear_nlink(ea_inode);
  934. ext4_orphan_add(handle, ea_inode);
  935. if (ea_inode_cache) {
  936. hash = ext4_xattr_inode_get_hash(ea_inode);
  937. mb_cache_entry_delete(ea_inode_cache, hash,
  938. ea_inode->i_ino);
  939. }
  940. }
  941. }
  942. ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
  943. iloc.bh = NULL;
  944. if (ret)
  945. ext4_warning_inode(ea_inode,
  946. "ext4_mark_iloc_dirty() failed ret=%d", ret);
  947. out:
  948. brelse(iloc.bh);
  949. inode_unlock(ea_inode);
  950. return ret;
  951. }
  952. static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
  953. {
  954. return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
  955. }
  956. static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
  957. {
  958. return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
  959. }
  960. static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
  961. struct ext4_xattr_entry *first)
  962. {
  963. struct inode *ea_inode;
  964. struct ext4_xattr_entry *entry;
  965. struct ext4_xattr_entry *failed_entry;
  966. unsigned int ea_ino;
  967. int err, saved_err;
  968. for (entry = first; !IS_LAST_ENTRY(entry);
  969. entry = EXT4_XATTR_NEXT(entry)) {
  970. if (!entry->e_value_inum)
  971. continue;
  972. ea_ino = le32_to_cpu(entry->e_value_inum);
  973. err = ext4_xattr_inode_iget(parent, ea_ino,
  974. le32_to_cpu(entry->e_hash),
  975. &ea_inode);
  976. if (err)
  977. goto cleanup;
  978. err = ext4_xattr_inode_inc_ref(handle, ea_inode);
  979. if (err) {
  980. ext4_warning_inode(ea_inode, "inc ref error %d", err);
  981. iput(ea_inode);
  982. goto cleanup;
  983. }
  984. iput(ea_inode);
  985. }
  986. return 0;
  987. cleanup:
  988. saved_err = err;
  989. failed_entry = entry;
  990. for (entry = first; entry != failed_entry;
  991. entry = EXT4_XATTR_NEXT(entry)) {
  992. if (!entry->e_value_inum)
  993. continue;
  994. ea_ino = le32_to_cpu(entry->e_value_inum);
  995. err = ext4_xattr_inode_iget(parent, ea_ino,
  996. le32_to_cpu(entry->e_hash),
  997. &ea_inode);
  998. if (err) {
  999. ext4_warning(parent->i_sb,
  1000. "cleanup ea_ino %u iget error %d", ea_ino,
  1001. err);
  1002. continue;
  1003. }
  1004. err = ext4_xattr_inode_dec_ref(handle, ea_inode);
  1005. if (err)
  1006. ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
  1007. err);
  1008. iput(ea_inode);
  1009. }
  1010. return saved_err;
  1011. }
  1012. static void
  1013. ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
  1014. struct buffer_head *bh,
  1015. struct ext4_xattr_entry *first, bool block_csum,
  1016. struct ext4_xattr_inode_array **ea_inode_array,
  1017. int extra_credits, bool skip_quota)
  1018. {
  1019. struct inode *ea_inode;
  1020. struct ext4_xattr_entry *entry;
  1021. bool dirty = false;
  1022. unsigned int ea_ino;
  1023. int err;
  1024. int credits;
  1025. /* One credit for dec ref on ea_inode, one for orphan list addition, */
  1026. credits = 2 + extra_credits;
  1027. for (entry = first; !IS_LAST_ENTRY(entry);
  1028. entry = EXT4_XATTR_NEXT(entry)) {
  1029. if (!entry->e_value_inum)
  1030. continue;
  1031. ea_ino = le32_to_cpu(entry->e_value_inum);
  1032. err = ext4_xattr_inode_iget(parent, ea_ino,
  1033. le32_to_cpu(entry->e_hash),
  1034. &ea_inode);
  1035. if (err)
  1036. continue;
  1037. err = ext4_expand_inode_array(ea_inode_array, ea_inode);
  1038. if (err) {
  1039. ext4_warning_inode(ea_inode,
  1040. "Expand inode array err=%d", err);
  1041. iput(ea_inode);
  1042. continue;
  1043. }
  1044. err = ext4_xattr_ensure_credits(handle, parent, credits, bh,
  1045. dirty, block_csum);
  1046. if (err) {
  1047. ext4_warning_inode(ea_inode, "Ensure credits err=%d",
  1048. err);
  1049. continue;
  1050. }
  1051. err = ext4_xattr_inode_dec_ref(handle, ea_inode);
  1052. if (err) {
  1053. ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
  1054. err);
  1055. continue;
  1056. }
  1057. if (!skip_quota)
  1058. ext4_xattr_inode_free_quota(parent, ea_inode,
  1059. le32_to_cpu(entry->e_value_size));
  1060. /*
  1061. * Forget about ea_inode within the same transaction that
  1062. * decrements the ref count. This avoids duplicate decrements in
  1063. * case the rest of the work spills over to subsequent
  1064. * transactions.
  1065. */
  1066. entry->e_value_inum = 0;
  1067. entry->e_value_size = 0;
  1068. dirty = true;
  1069. }
  1070. if (dirty) {
  1071. /*
  1072. * Note that we are deliberately skipping csum calculation for
  1073. * the final update because we do not expect any journal
  1074. * restarts until xattr block is freed.
  1075. */
  1076. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  1077. if (err)
  1078. ext4_warning_inode(parent,
  1079. "handle dirty metadata err=%d", err);
  1080. }
  1081. }
  1082. /*
  1083. * Release the xattr block BH: If the reference count is > 1, decrement it;
  1084. * otherwise free the block.
  1085. */
  1086. static void
  1087. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  1088. struct buffer_head *bh,
  1089. struct ext4_xattr_inode_array **ea_inode_array,
  1090. int extra_credits)
  1091. {
  1092. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  1093. u32 hash, ref;
  1094. int error = 0;
  1095. BUFFER_TRACE(bh, "get_write_access");
  1096. error = ext4_journal_get_write_access(handle, bh);
  1097. if (error)
  1098. goto out;
  1099. lock_buffer(bh);
  1100. hash = le32_to_cpu(BHDR(bh)->h_hash);
  1101. ref = le32_to_cpu(BHDR(bh)->h_refcount);
  1102. if (ref == 1) {
  1103. ea_bdebug(bh, "refcount now=0; freeing");
  1104. /*
  1105. * This must happen under buffer lock for
  1106. * ext4_xattr_block_set() to reliably detect freed block
  1107. */
  1108. if (ea_block_cache)
  1109. mb_cache_entry_delete(ea_block_cache, hash,
  1110. bh->b_blocknr);
  1111. get_bh(bh);
  1112. unlock_buffer(bh);
  1113. if (ext4_has_feature_ea_inode(inode->i_sb))
  1114. ext4_xattr_inode_dec_ref_all(handle, inode, bh,
  1115. BFIRST(bh),
  1116. true /* block_csum */,
  1117. ea_inode_array,
  1118. extra_credits,
  1119. true /* skip_quota */);
  1120. ext4_free_blocks(handle, inode, bh, 0, 1,
  1121. EXT4_FREE_BLOCKS_METADATA |
  1122. EXT4_FREE_BLOCKS_FORGET);
  1123. } else {
  1124. ref--;
  1125. BHDR(bh)->h_refcount = cpu_to_le32(ref);
  1126. if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
  1127. struct mb_cache_entry *ce;
  1128. if (ea_block_cache) {
  1129. ce = mb_cache_entry_get(ea_block_cache, hash,
  1130. bh->b_blocknr);
  1131. if (ce) {
  1132. ce->e_reusable = 1;
  1133. mb_cache_entry_put(ea_block_cache, ce);
  1134. }
  1135. }
  1136. }
  1137. ext4_xattr_block_csum_set(inode, bh);
  1138. /*
  1139. * Beware of this ugliness: Releasing of xattr block references
  1140. * from different inodes can race and so we have to protect
  1141. * from a race where someone else frees the block (and releases
  1142. * its journal_head) before we are done dirtying the buffer. In
  1143. * nojournal mode this race is harmless and we actually cannot
  1144. * call ext4_handle_dirty_metadata() with locked buffer as
  1145. * that function can call sync_dirty_buffer() so for that case
  1146. * we handle the dirtying after unlocking the buffer.
  1147. */
  1148. if (ext4_handle_valid(handle))
  1149. error = ext4_handle_dirty_metadata(handle, inode, bh);
  1150. unlock_buffer(bh);
  1151. if (!ext4_handle_valid(handle))
  1152. error = ext4_handle_dirty_metadata(handle, inode, bh);
  1153. if (IS_SYNC(inode))
  1154. ext4_handle_sync(handle);
  1155. dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
  1156. ea_bdebug(bh, "refcount now=%d; releasing",
  1157. le32_to_cpu(BHDR(bh)->h_refcount));
  1158. }
  1159. out:
  1160. ext4_std_error(inode->i_sb, error);
  1161. return;
  1162. }
  1163. /*
  1164. * Find the available free space for EAs. This also returns the total number of
  1165. * bytes used by EA entries.
  1166. */
  1167. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  1168. size_t *min_offs, void *base, int *total)
  1169. {
  1170. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1171. if (!last->e_value_inum && last->e_value_size) {
  1172. size_t offs = le16_to_cpu(last->e_value_offs);
  1173. if (offs < *min_offs)
  1174. *min_offs = offs;
  1175. }
  1176. if (total)
  1177. *total += EXT4_XATTR_LEN(last->e_name_len);
  1178. }
  1179. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  1180. }
  1181. /*
  1182. * Write the value of the EA in an inode.
  1183. */
  1184. static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
  1185. const void *buf, int bufsize)
  1186. {
  1187. struct buffer_head *bh = NULL;
  1188. unsigned long block = 0;
  1189. int blocksize = ea_inode->i_sb->s_blocksize;
  1190. int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
  1191. int csize, wsize = 0;
  1192. int ret = 0;
  1193. int retries = 0;
  1194. retry:
  1195. while (ret >= 0 && ret < max_blocks) {
  1196. struct ext4_map_blocks map;
  1197. map.m_lblk = block += ret;
  1198. map.m_len = max_blocks -= ret;
  1199. ret = ext4_map_blocks(handle, ea_inode, &map,
  1200. EXT4_GET_BLOCKS_CREATE);
  1201. if (ret <= 0) {
  1202. ext4_mark_inode_dirty(handle, ea_inode);
  1203. if (ret == -ENOSPC &&
  1204. ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
  1205. ret = 0;
  1206. goto retry;
  1207. }
  1208. break;
  1209. }
  1210. }
  1211. if (ret < 0)
  1212. return ret;
  1213. block = 0;
  1214. while (wsize < bufsize) {
  1215. if (bh != NULL)
  1216. brelse(bh);
  1217. csize = (bufsize - wsize) > blocksize ? blocksize :
  1218. bufsize - wsize;
  1219. bh = ext4_getblk(handle, ea_inode, block, 0);
  1220. if (IS_ERR(bh))
  1221. return PTR_ERR(bh);
  1222. ret = ext4_journal_get_write_access(handle, bh);
  1223. if (ret)
  1224. goto out;
  1225. memcpy(bh->b_data, buf, csize);
  1226. set_buffer_uptodate(bh);
  1227. ext4_handle_dirty_metadata(handle, ea_inode, bh);
  1228. buf += csize;
  1229. wsize += csize;
  1230. block += 1;
  1231. }
  1232. inode_lock(ea_inode);
  1233. i_size_write(ea_inode, wsize);
  1234. ext4_update_i_disksize(ea_inode, wsize);
  1235. inode_unlock(ea_inode);
  1236. ext4_mark_inode_dirty(handle, ea_inode);
  1237. out:
  1238. brelse(bh);
  1239. return ret;
  1240. }
  1241. /*
  1242. * Create an inode to store the value of a large EA.
  1243. */
  1244. static struct inode *ext4_xattr_inode_create(handle_t *handle,
  1245. struct inode *inode, u32 hash)
  1246. {
  1247. struct inode *ea_inode = NULL;
  1248. uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
  1249. int err;
  1250. /*
  1251. * Let the next inode be the goal, so we try and allocate the EA inode
  1252. * in the same group, or nearby one.
  1253. */
  1254. ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
  1255. S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
  1256. EXT4_EA_INODE_FL);
  1257. if (!IS_ERR(ea_inode)) {
  1258. ea_inode->i_op = &ext4_file_inode_operations;
  1259. ea_inode->i_fop = &ext4_file_operations;
  1260. ext4_set_aops(ea_inode);
  1261. ext4_xattr_inode_set_class(ea_inode);
  1262. unlock_new_inode(ea_inode);
  1263. ext4_xattr_inode_set_ref(ea_inode, 1);
  1264. ext4_xattr_inode_set_hash(ea_inode, hash);
  1265. err = ext4_mark_inode_dirty(handle, ea_inode);
  1266. if (!err)
  1267. err = ext4_inode_attach_jinode(ea_inode);
  1268. if (err) {
  1269. iput(ea_inode);
  1270. return ERR_PTR(err);
  1271. }
  1272. /*
  1273. * Xattr inodes are shared therefore quota charging is performed
  1274. * at a higher level.
  1275. */
  1276. dquot_free_inode(ea_inode);
  1277. dquot_drop(ea_inode);
  1278. inode_lock(ea_inode);
  1279. ea_inode->i_flags |= S_NOQUOTA;
  1280. inode_unlock(ea_inode);
  1281. }
  1282. return ea_inode;
  1283. }
  1284. static struct inode *
  1285. ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
  1286. size_t value_len, u32 hash)
  1287. {
  1288. struct inode *ea_inode;
  1289. struct mb_cache_entry *ce;
  1290. struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
  1291. void *ea_data;
  1292. if (!ea_inode_cache)
  1293. return NULL;
  1294. ce = mb_cache_entry_find_first(ea_inode_cache, hash);
  1295. if (!ce)
  1296. return NULL;
  1297. ea_data = ext4_kvmalloc(value_len, GFP_NOFS);
  1298. if (!ea_data) {
  1299. mb_cache_entry_put(ea_inode_cache, ce);
  1300. return NULL;
  1301. }
  1302. while (ce) {
  1303. ea_inode = ext4_iget(inode->i_sb, ce->e_value);
  1304. if (!IS_ERR(ea_inode) &&
  1305. !is_bad_inode(ea_inode) &&
  1306. (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) &&
  1307. i_size_read(ea_inode) == value_len &&
  1308. !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
  1309. !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
  1310. value_len) &&
  1311. !memcmp(value, ea_data, value_len)) {
  1312. mb_cache_entry_touch(ea_inode_cache, ce);
  1313. mb_cache_entry_put(ea_inode_cache, ce);
  1314. kvfree(ea_data);
  1315. return ea_inode;
  1316. }
  1317. if (!IS_ERR(ea_inode))
  1318. iput(ea_inode);
  1319. ce = mb_cache_entry_find_next(ea_inode_cache, ce);
  1320. }
  1321. kvfree(ea_data);
  1322. return NULL;
  1323. }
  1324. /*
  1325. * Add value of the EA in an inode.
  1326. */
  1327. static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
  1328. const void *value, size_t value_len,
  1329. struct inode **ret_inode)
  1330. {
  1331. struct inode *ea_inode;
  1332. u32 hash;
  1333. int err;
  1334. hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
  1335. ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
  1336. if (ea_inode) {
  1337. err = ext4_xattr_inode_inc_ref(handle, ea_inode);
  1338. if (err) {
  1339. iput(ea_inode);
  1340. return err;
  1341. }
  1342. *ret_inode = ea_inode;
  1343. return 0;
  1344. }
  1345. /* Create an inode for the EA value */
  1346. ea_inode = ext4_xattr_inode_create(handle, inode, hash);
  1347. if (IS_ERR(ea_inode))
  1348. return PTR_ERR(ea_inode);
  1349. err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
  1350. if (err) {
  1351. ext4_xattr_inode_dec_ref(handle, ea_inode);
  1352. iput(ea_inode);
  1353. return err;
  1354. }
  1355. if (EA_INODE_CACHE(inode))
  1356. mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
  1357. ea_inode->i_ino, true /* reusable */);
  1358. *ret_inode = ea_inode;
  1359. return 0;
  1360. }
  1361. /*
  1362. * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
  1363. * feature is enabled.
  1364. */
  1365. #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
  1366. static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
  1367. struct ext4_xattr_search *s,
  1368. handle_t *handle, struct inode *inode,
  1369. bool is_block)
  1370. {
  1371. struct ext4_xattr_entry *last, *next;
  1372. struct ext4_xattr_entry *here = s->here;
  1373. size_t min_offs = s->end - s->base, name_len = strlen(i->name);
  1374. int in_inode = i->in_inode;
  1375. struct inode *old_ea_inode = NULL;
  1376. struct inode *new_ea_inode = NULL;
  1377. size_t old_size, new_size;
  1378. int ret;
  1379. /* Space used by old and new values. */
  1380. old_size = (!s->not_found && !here->e_value_inum) ?
  1381. EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
  1382. new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
  1383. /*
  1384. * Optimization for the simple case when old and new values have the
  1385. * same padded sizes. Not applicable if external inodes are involved.
  1386. */
  1387. if (new_size && new_size == old_size) {
  1388. size_t offs = le16_to_cpu(here->e_value_offs);
  1389. void *val = s->base + offs;
  1390. here->e_value_size = cpu_to_le32(i->value_len);
  1391. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  1392. memset(val, 0, new_size);
  1393. } else {
  1394. memcpy(val, i->value, i->value_len);
  1395. /* Clear padding bytes. */
  1396. memset(val + i->value_len, 0, new_size - i->value_len);
  1397. }
  1398. goto update_hash;
  1399. }
  1400. /* Compute min_offs and last. */
  1401. last = s->first;
  1402. for (; !IS_LAST_ENTRY(last); last = next) {
  1403. next = EXT4_XATTR_NEXT(last);
  1404. if ((void *)next >= s->end) {
  1405. EXT4_ERROR_INODE(inode, "corrupted xattr entries");
  1406. ret = -EFSCORRUPTED;
  1407. goto out;
  1408. }
  1409. if (!last->e_value_inum && last->e_value_size) {
  1410. size_t offs = le16_to_cpu(last->e_value_offs);
  1411. if (offs < min_offs)
  1412. min_offs = offs;
  1413. }
  1414. }
  1415. /* Check whether we have enough space. */
  1416. if (i->value) {
  1417. size_t free;
  1418. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  1419. if (!s->not_found)
  1420. free += EXT4_XATTR_LEN(name_len) + old_size;
  1421. if (free < EXT4_XATTR_LEN(name_len) + new_size) {
  1422. ret = -ENOSPC;
  1423. goto out;
  1424. }
  1425. /*
  1426. * If storing the value in an external inode is an option,
  1427. * reserve space for xattr entries/names in the external
  1428. * attribute block so that a long value does not occupy the
  1429. * whole space and prevent futher entries being added.
  1430. */
  1431. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  1432. new_size && is_block &&
  1433. (min_offs + old_size - new_size) <
  1434. EXT4_XATTR_BLOCK_RESERVE(inode)) {
  1435. ret = -ENOSPC;
  1436. goto out;
  1437. }
  1438. }
  1439. /*
  1440. * Getting access to old and new ea inodes is subject to failures.
  1441. * Finish that work before doing any modifications to the xattr data.
  1442. */
  1443. if (!s->not_found && here->e_value_inum) {
  1444. ret = ext4_xattr_inode_iget(inode,
  1445. le32_to_cpu(here->e_value_inum),
  1446. le32_to_cpu(here->e_hash),
  1447. &old_ea_inode);
  1448. if (ret) {
  1449. old_ea_inode = NULL;
  1450. goto out;
  1451. }
  1452. }
  1453. if (i->value && in_inode) {
  1454. WARN_ON_ONCE(!i->value_len);
  1455. ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
  1456. if (ret)
  1457. goto out;
  1458. ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
  1459. i->value_len,
  1460. &new_ea_inode);
  1461. if (ret) {
  1462. new_ea_inode = NULL;
  1463. ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
  1464. goto out;
  1465. }
  1466. }
  1467. if (old_ea_inode) {
  1468. /* We are ready to release ref count on the old_ea_inode. */
  1469. ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
  1470. if (ret) {
  1471. /* Release newly required ref count on new_ea_inode. */
  1472. if (new_ea_inode) {
  1473. int err;
  1474. err = ext4_xattr_inode_dec_ref(handle,
  1475. new_ea_inode);
  1476. if (err)
  1477. ext4_warning_inode(new_ea_inode,
  1478. "dec ref new_ea_inode err=%d",
  1479. err);
  1480. ext4_xattr_inode_free_quota(inode, new_ea_inode,
  1481. i->value_len);
  1482. }
  1483. goto out;
  1484. }
  1485. ext4_xattr_inode_free_quota(inode, old_ea_inode,
  1486. le32_to_cpu(here->e_value_size));
  1487. }
  1488. /* No failures allowed past this point. */
  1489. if (!s->not_found && here->e_value_size && here->e_value_offs) {
  1490. /* Remove the old value. */
  1491. void *first_val = s->base + min_offs;
  1492. size_t offs = le16_to_cpu(here->e_value_offs);
  1493. void *val = s->base + offs;
  1494. memmove(first_val + old_size, first_val, val - first_val);
  1495. memset(first_val, 0, old_size);
  1496. min_offs += old_size;
  1497. /* Adjust all value offsets. */
  1498. last = s->first;
  1499. while (!IS_LAST_ENTRY(last)) {
  1500. size_t o = le16_to_cpu(last->e_value_offs);
  1501. if (!last->e_value_inum &&
  1502. last->e_value_size && o < offs)
  1503. last->e_value_offs = cpu_to_le16(o + old_size);
  1504. last = EXT4_XATTR_NEXT(last);
  1505. }
  1506. }
  1507. if (!i->value) {
  1508. /* Remove old name. */
  1509. size_t size = EXT4_XATTR_LEN(name_len);
  1510. last = ENTRY((void *)last - size);
  1511. memmove(here, (void *)here + size,
  1512. (void *)last - (void *)here + sizeof(__u32));
  1513. memset(last, 0, size);
  1514. } else if (s->not_found) {
  1515. /* Insert new name. */
  1516. size_t size = EXT4_XATTR_LEN(name_len);
  1517. size_t rest = (void *)last - (void *)here + sizeof(__u32);
  1518. memmove((void *)here + size, here, rest);
  1519. memset(here, 0, size);
  1520. here->e_name_index = i->name_index;
  1521. here->e_name_len = name_len;
  1522. memcpy(here->e_name, i->name, name_len);
  1523. } else {
  1524. /* This is an update, reset value info. */
  1525. here->e_value_inum = 0;
  1526. here->e_value_offs = 0;
  1527. here->e_value_size = 0;
  1528. }
  1529. if (i->value) {
  1530. /* Insert new value. */
  1531. if (in_inode) {
  1532. here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
  1533. } else if (i->value_len) {
  1534. void *val = s->base + min_offs - new_size;
  1535. here->e_value_offs = cpu_to_le16(min_offs - new_size);
  1536. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  1537. memset(val, 0, new_size);
  1538. } else {
  1539. memcpy(val, i->value, i->value_len);
  1540. /* Clear padding bytes. */
  1541. memset(val + i->value_len, 0,
  1542. new_size - i->value_len);
  1543. }
  1544. }
  1545. here->e_value_size = cpu_to_le32(i->value_len);
  1546. }
  1547. update_hash:
  1548. if (i->value) {
  1549. __le32 hash = 0;
  1550. /* Entry hash calculation. */
  1551. if (in_inode) {
  1552. __le32 crc32c_hash;
  1553. /*
  1554. * Feed crc32c hash instead of the raw value for entry
  1555. * hash calculation. This is to avoid walking
  1556. * potentially long value buffer again.
  1557. */
  1558. crc32c_hash = cpu_to_le32(
  1559. ext4_xattr_inode_get_hash(new_ea_inode));
  1560. hash = ext4_xattr_hash_entry(here->e_name,
  1561. here->e_name_len,
  1562. &crc32c_hash, 1);
  1563. } else if (is_block) {
  1564. __le32 *value = s->base + le16_to_cpu(
  1565. here->e_value_offs);
  1566. hash = ext4_xattr_hash_entry(here->e_name,
  1567. here->e_name_len, value,
  1568. new_size >> 2);
  1569. }
  1570. here->e_hash = hash;
  1571. }
  1572. if (is_block)
  1573. ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
  1574. ret = 0;
  1575. out:
  1576. iput(old_ea_inode);
  1577. iput(new_ea_inode);
  1578. return ret;
  1579. }
  1580. struct ext4_xattr_block_find {
  1581. struct ext4_xattr_search s;
  1582. struct buffer_head *bh;
  1583. };
  1584. static int
  1585. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  1586. struct ext4_xattr_block_find *bs)
  1587. {
  1588. struct super_block *sb = inode->i_sb;
  1589. int error;
  1590. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  1591. i->name_index, i->name, i->value, (long)i->value_len);
  1592. if (EXT4_I(inode)->i_file_acl) {
  1593. /* The inode already has an extended attribute block. */
  1594. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  1595. error = -EIO;
  1596. if (!bs->bh)
  1597. goto cleanup;
  1598. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  1599. atomic_read(&(bs->bh->b_count)),
  1600. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  1601. error = ext4_xattr_check_block(inode, bs->bh);
  1602. if (error)
  1603. goto cleanup;
  1604. /* Find the named attribute. */
  1605. bs->s.base = BHDR(bs->bh);
  1606. bs->s.first = BFIRST(bs->bh);
  1607. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  1608. bs->s.here = bs->s.first;
  1609. error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
  1610. i->name_index, i->name, 1);
  1611. if (error && error != -ENODATA)
  1612. goto cleanup;
  1613. bs->s.not_found = error;
  1614. }
  1615. error = 0;
  1616. cleanup:
  1617. return error;
  1618. }
  1619. static int
  1620. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  1621. struct ext4_xattr_info *i,
  1622. struct ext4_xattr_block_find *bs)
  1623. {
  1624. struct super_block *sb = inode->i_sb;
  1625. struct buffer_head *new_bh = NULL;
  1626. struct ext4_xattr_search s_copy = bs->s;
  1627. struct ext4_xattr_search *s = &s_copy;
  1628. struct mb_cache_entry *ce = NULL;
  1629. int error = 0;
  1630. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  1631. struct inode *ea_inode = NULL, *tmp_inode;
  1632. size_t old_ea_inode_quota = 0;
  1633. unsigned int ea_ino;
  1634. #define header(x) ((struct ext4_xattr_header *)(x))
  1635. if (s->base) {
  1636. BUFFER_TRACE(bs->bh, "get_write_access");
  1637. error = ext4_journal_get_write_access(handle, bs->bh);
  1638. if (error)
  1639. goto cleanup;
  1640. lock_buffer(bs->bh);
  1641. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  1642. __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
  1643. /*
  1644. * This must happen under buffer lock for
  1645. * ext4_xattr_block_set() to reliably detect modified
  1646. * block
  1647. */
  1648. if (ea_block_cache)
  1649. mb_cache_entry_delete(ea_block_cache, hash,
  1650. bs->bh->b_blocknr);
  1651. ea_bdebug(bs->bh, "modifying in-place");
  1652. error = ext4_xattr_set_entry(i, s, handle, inode,
  1653. true /* is_block */);
  1654. ext4_xattr_block_csum_set(inode, bs->bh);
  1655. unlock_buffer(bs->bh);
  1656. if (error == -EFSCORRUPTED)
  1657. goto bad_block;
  1658. if (!error)
  1659. error = ext4_handle_dirty_metadata(handle,
  1660. inode,
  1661. bs->bh);
  1662. if (error)
  1663. goto cleanup;
  1664. goto inserted;
  1665. } else {
  1666. int offset = (char *)s->here - bs->bh->b_data;
  1667. unlock_buffer(bs->bh);
  1668. ea_bdebug(bs->bh, "cloning");
  1669. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  1670. error = -ENOMEM;
  1671. if (s->base == NULL)
  1672. goto cleanup;
  1673. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  1674. s->first = ENTRY(header(s->base)+1);
  1675. header(s->base)->h_refcount = cpu_to_le32(1);
  1676. s->here = ENTRY(s->base + offset);
  1677. s->end = s->base + bs->bh->b_size;
  1678. /*
  1679. * If existing entry points to an xattr inode, we need
  1680. * to prevent ext4_xattr_set_entry() from decrementing
  1681. * ref count on it because the reference belongs to the
  1682. * original block. In this case, make the entry look
  1683. * like it has an empty value.
  1684. */
  1685. if (!s->not_found && s->here->e_value_inum) {
  1686. ea_ino = le32_to_cpu(s->here->e_value_inum);
  1687. error = ext4_xattr_inode_iget(inode, ea_ino,
  1688. le32_to_cpu(s->here->e_hash),
  1689. &tmp_inode);
  1690. if (error)
  1691. goto cleanup;
  1692. if (!ext4_test_inode_state(tmp_inode,
  1693. EXT4_STATE_LUSTRE_EA_INODE)) {
  1694. /*
  1695. * Defer quota free call for previous
  1696. * inode until success is guaranteed.
  1697. */
  1698. old_ea_inode_quota = le32_to_cpu(
  1699. s->here->e_value_size);
  1700. }
  1701. iput(tmp_inode);
  1702. s->here->e_value_inum = 0;
  1703. s->here->e_value_size = 0;
  1704. }
  1705. }
  1706. } else {
  1707. /* Allocate a buffer where we construct the new block. */
  1708. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  1709. /* assert(header == s->base) */
  1710. error = -ENOMEM;
  1711. if (s->base == NULL)
  1712. goto cleanup;
  1713. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1714. header(s->base)->h_blocks = cpu_to_le32(1);
  1715. header(s->base)->h_refcount = cpu_to_le32(1);
  1716. s->first = ENTRY(header(s->base)+1);
  1717. s->here = ENTRY(header(s->base)+1);
  1718. s->end = s->base + sb->s_blocksize;
  1719. }
  1720. error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
  1721. if (error == -EFSCORRUPTED)
  1722. goto bad_block;
  1723. if (error)
  1724. goto cleanup;
  1725. if (i->value && s->here->e_value_inum) {
  1726. /*
  1727. * A ref count on ea_inode has been taken as part of the call to
  1728. * ext4_xattr_set_entry() above. We would like to drop this
  1729. * extra ref but we have to wait until the xattr block is
  1730. * initialized and has its own ref count on the ea_inode.
  1731. */
  1732. ea_ino = le32_to_cpu(s->here->e_value_inum);
  1733. error = ext4_xattr_inode_iget(inode, ea_ino,
  1734. le32_to_cpu(s->here->e_hash),
  1735. &ea_inode);
  1736. if (error) {
  1737. ea_inode = NULL;
  1738. goto cleanup;
  1739. }
  1740. }
  1741. inserted:
  1742. if (!IS_LAST_ENTRY(s->first)) {
  1743. new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
  1744. &ce);
  1745. if (new_bh) {
  1746. /* We found an identical block in the cache. */
  1747. if (new_bh == bs->bh)
  1748. ea_bdebug(new_bh, "keeping");
  1749. else {
  1750. u32 ref;
  1751. WARN_ON_ONCE(dquot_initialize_needed(inode));
  1752. /* The old block is released after updating
  1753. the inode. */
  1754. error = dquot_alloc_block(inode,
  1755. EXT4_C2B(EXT4_SB(sb), 1));
  1756. if (error)
  1757. goto cleanup;
  1758. BUFFER_TRACE(new_bh, "get_write_access");
  1759. error = ext4_journal_get_write_access(handle,
  1760. new_bh);
  1761. if (error)
  1762. goto cleanup_dquot;
  1763. lock_buffer(new_bh);
  1764. /*
  1765. * We have to be careful about races with
  1766. * freeing, rehashing or adding references to
  1767. * xattr block. Once we hold buffer lock xattr
  1768. * block's state is stable so we can check
  1769. * whether the block got freed / rehashed or
  1770. * not. Since we unhash mbcache entry under
  1771. * buffer lock when freeing / rehashing xattr
  1772. * block, checking whether entry is still
  1773. * hashed is reliable. Same rules hold for
  1774. * e_reusable handling.
  1775. */
  1776. if (hlist_bl_unhashed(&ce->e_hash_list) ||
  1777. !ce->e_reusable) {
  1778. /*
  1779. * Undo everything and check mbcache
  1780. * again.
  1781. */
  1782. unlock_buffer(new_bh);
  1783. dquot_free_block(inode,
  1784. EXT4_C2B(EXT4_SB(sb),
  1785. 1));
  1786. brelse(new_bh);
  1787. mb_cache_entry_put(ea_block_cache, ce);
  1788. ce = NULL;
  1789. new_bh = NULL;
  1790. goto inserted;
  1791. }
  1792. ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
  1793. BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
  1794. if (ref >= EXT4_XATTR_REFCOUNT_MAX)
  1795. ce->e_reusable = 0;
  1796. ea_bdebug(new_bh, "reusing; refcount now=%d",
  1797. ref);
  1798. ext4_xattr_block_csum_set(inode, new_bh);
  1799. unlock_buffer(new_bh);
  1800. error = ext4_handle_dirty_metadata(handle,
  1801. inode,
  1802. new_bh);
  1803. if (error)
  1804. goto cleanup_dquot;
  1805. }
  1806. mb_cache_entry_touch(ea_block_cache, ce);
  1807. mb_cache_entry_put(ea_block_cache, ce);
  1808. ce = NULL;
  1809. } else if (bs->bh && s->base == bs->bh->b_data) {
  1810. /* We were modifying this block in-place. */
  1811. ea_bdebug(bs->bh, "keeping this block");
  1812. ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
  1813. new_bh = bs->bh;
  1814. get_bh(new_bh);
  1815. } else {
  1816. /* We need to allocate a new block */
  1817. ext4_fsblk_t goal, block;
  1818. WARN_ON_ONCE(dquot_initialize_needed(inode));
  1819. goal = ext4_group_first_block_no(sb,
  1820. EXT4_I(inode)->i_block_group);
  1821. /* non-extent files can't have physical blocks past 2^32 */
  1822. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  1823. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  1824. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  1825. NULL, &error);
  1826. if (error)
  1827. goto cleanup;
  1828. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  1829. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  1830. ea_idebug(inode, "creating block %llu",
  1831. (unsigned long long)block);
  1832. new_bh = sb_getblk(sb, block);
  1833. if (unlikely(!new_bh)) {
  1834. error = -ENOMEM;
  1835. getblk_failed:
  1836. ext4_free_blocks(handle, inode, NULL, block, 1,
  1837. EXT4_FREE_BLOCKS_METADATA);
  1838. goto cleanup;
  1839. }
  1840. error = ext4_xattr_inode_inc_ref_all(handle, inode,
  1841. ENTRY(header(s->base)+1));
  1842. if (error)
  1843. goto getblk_failed;
  1844. if (ea_inode) {
  1845. /* Drop the extra ref on ea_inode. */
  1846. error = ext4_xattr_inode_dec_ref(handle,
  1847. ea_inode);
  1848. if (error)
  1849. ext4_warning_inode(ea_inode,
  1850. "dec ref error=%d",
  1851. error);
  1852. iput(ea_inode);
  1853. ea_inode = NULL;
  1854. }
  1855. lock_buffer(new_bh);
  1856. error = ext4_journal_get_create_access(handle, new_bh);
  1857. if (error) {
  1858. unlock_buffer(new_bh);
  1859. error = -EIO;
  1860. goto getblk_failed;
  1861. }
  1862. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  1863. ext4_xattr_block_csum_set(inode, new_bh);
  1864. set_buffer_uptodate(new_bh);
  1865. unlock_buffer(new_bh);
  1866. ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
  1867. error = ext4_handle_dirty_metadata(handle, inode,
  1868. new_bh);
  1869. if (error)
  1870. goto cleanup;
  1871. }
  1872. }
  1873. if (old_ea_inode_quota)
  1874. ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
  1875. /* Update the inode. */
  1876. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  1877. /* Drop the previous xattr block. */
  1878. if (bs->bh && bs->bh != new_bh) {
  1879. struct ext4_xattr_inode_array *ea_inode_array = NULL;
  1880. ext4_xattr_release_block(handle, inode, bs->bh,
  1881. &ea_inode_array,
  1882. 0 /* extra_credits */);
  1883. ext4_xattr_inode_array_free(ea_inode_array);
  1884. }
  1885. error = 0;
  1886. cleanup:
  1887. if (ea_inode) {
  1888. int error2;
  1889. error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
  1890. if (error2)
  1891. ext4_warning_inode(ea_inode, "dec ref error=%d",
  1892. error2);
  1893. /* If there was an error, revert the quota charge. */
  1894. if (error)
  1895. ext4_xattr_inode_free_quota(inode, ea_inode,
  1896. i_size_read(ea_inode));
  1897. iput(ea_inode);
  1898. }
  1899. if (ce)
  1900. mb_cache_entry_put(ea_block_cache, ce);
  1901. brelse(new_bh);
  1902. if (!(bs->bh && s->base == bs->bh->b_data))
  1903. kfree(s->base);
  1904. return error;
  1905. cleanup_dquot:
  1906. dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
  1907. goto cleanup;
  1908. bad_block:
  1909. EXT4_ERROR_INODE(inode, "bad block %llu",
  1910. EXT4_I(inode)->i_file_acl);
  1911. goto cleanup;
  1912. #undef header
  1913. }
  1914. int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  1915. struct ext4_xattr_ibody_find *is)
  1916. {
  1917. struct ext4_xattr_ibody_header *header;
  1918. struct ext4_inode *raw_inode;
  1919. int error;
  1920. if (EXT4_I(inode)->i_extra_isize == 0)
  1921. return 0;
  1922. raw_inode = ext4_raw_inode(&is->iloc);
  1923. header = IHDR(inode, raw_inode);
  1924. is->s.base = is->s.first = IFIRST(header);
  1925. is->s.here = is->s.first;
  1926. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1927. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  1928. error = xattr_check_inode(inode, header, is->s.end);
  1929. if (error)
  1930. return error;
  1931. /* Find the named attribute. */
  1932. error = xattr_find_entry(inode, &is->s.here, is->s.end,
  1933. i->name_index, i->name, 0);
  1934. if (error && error != -ENODATA)
  1935. return error;
  1936. is->s.not_found = error;
  1937. }
  1938. return 0;
  1939. }
  1940. int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
  1941. struct ext4_xattr_info *i,
  1942. struct ext4_xattr_ibody_find *is)
  1943. {
  1944. struct ext4_xattr_ibody_header *header;
  1945. struct ext4_xattr_search *s = &is->s;
  1946. int error;
  1947. if (EXT4_I(inode)->i_extra_isize == 0)
  1948. return -ENOSPC;
  1949. error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
  1950. if (error)
  1951. return error;
  1952. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  1953. if (!IS_LAST_ENTRY(s->first)) {
  1954. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1955. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  1956. } else {
  1957. header->h_magic = cpu_to_le32(0);
  1958. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  1959. }
  1960. return 0;
  1961. }
  1962. static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  1963. struct ext4_xattr_info *i,
  1964. struct ext4_xattr_ibody_find *is)
  1965. {
  1966. struct ext4_xattr_ibody_header *header;
  1967. struct ext4_xattr_search *s = &is->s;
  1968. int error;
  1969. if (EXT4_I(inode)->i_extra_isize == 0)
  1970. return -ENOSPC;
  1971. error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
  1972. if (error)
  1973. return error;
  1974. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  1975. if (!IS_LAST_ENTRY(s->first)) {
  1976. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  1977. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  1978. } else {
  1979. header->h_magic = cpu_to_le32(0);
  1980. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  1981. }
  1982. return 0;
  1983. }
  1984. static int ext4_xattr_value_same(struct ext4_xattr_search *s,
  1985. struct ext4_xattr_info *i)
  1986. {
  1987. void *value;
  1988. /* When e_value_inum is set the value is stored externally. */
  1989. if (s->here->e_value_inum)
  1990. return 0;
  1991. if (le32_to_cpu(s->here->e_value_size) != i->value_len)
  1992. return 0;
  1993. value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
  1994. return !memcmp(value, i->value, i->value_len);
  1995. }
  1996. static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
  1997. {
  1998. struct buffer_head *bh;
  1999. int error;
  2000. if (!EXT4_I(inode)->i_file_acl)
  2001. return NULL;
  2002. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  2003. if (!bh)
  2004. return ERR_PTR(-EIO);
  2005. error = ext4_xattr_check_block(inode, bh);
  2006. if (error)
  2007. return ERR_PTR(error);
  2008. return bh;
  2009. }
  2010. /*
  2011. * ext4_xattr_set_handle()
  2012. *
  2013. * Create, replace or remove an extended attribute for this inode. Value
  2014. * is NULL to remove an existing extended attribute, and non-NULL to
  2015. * either replace an existing extended attribute, or create a new extended
  2016. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  2017. * specify that an extended attribute must exist and must not exist
  2018. * previous to the call, respectively.
  2019. *
  2020. * Returns 0, or a negative error number on failure.
  2021. */
  2022. int
  2023. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  2024. const char *name, const void *value, size_t value_len,
  2025. int flags)
  2026. {
  2027. struct ext4_xattr_info i = {
  2028. .name_index = name_index,
  2029. .name = name,
  2030. .value = value,
  2031. .value_len = value_len,
  2032. .in_inode = 0,
  2033. };
  2034. struct ext4_xattr_ibody_find is = {
  2035. .s = { .not_found = -ENODATA, },
  2036. };
  2037. struct ext4_xattr_block_find bs = {
  2038. .s = { .not_found = -ENODATA, },
  2039. };
  2040. int no_expand;
  2041. int error;
  2042. if (!name)
  2043. return -EINVAL;
  2044. if (strlen(name) > 255)
  2045. return -ERANGE;
  2046. ext4_write_lock_xattr(inode, &no_expand);
  2047. /* Check journal credits under write lock. */
  2048. if (ext4_handle_valid(handle)) {
  2049. struct buffer_head *bh;
  2050. int credits;
  2051. bh = ext4_xattr_get_block(inode);
  2052. if (IS_ERR(bh)) {
  2053. error = PTR_ERR(bh);
  2054. goto cleanup;
  2055. }
  2056. credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
  2057. value_len,
  2058. flags & XATTR_CREATE);
  2059. brelse(bh);
  2060. if (!ext4_handle_has_enough_credits(handle, credits)) {
  2061. error = -ENOSPC;
  2062. goto cleanup;
  2063. }
  2064. }
  2065. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  2066. if (error)
  2067. goto cleanup;
  2068. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  2069. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  2070. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  2071. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  2072. }
  2073. error = ext4_xattr_ibody_find(inode, &i, &is);
  2074. if (error)
  2075. goto cleanup;
  2076. if (is.s.not_found)
  2077. error = ext4_xattr_block_find(inode, &i, &bs);
  2078. if (error)
  2079. goto cleanup;
  2080. if (is.s.not_found && bs.s.not_found) {
  2081. error = -ENODATA;
  2082. if (flags & XATTR_REPLACE)
  2083. goto cleanup;
  2084. error = 0;
  2085. if (!value)
  2086. goto cleanup;
  2087. } else {
  2088. error = -EEXIST;
  2089. if (flags & XATTR_CREATE)
  2090. goto cleanup;
  2091. }
  2092. if (!value) {
  2093. if (!is.s.not_found)
  2094. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  2095. else if (!bs.s.not_found)
  2096. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2097. } else {
  2098. error = 0;
  2099. /* Xattr value did not change? Save us some work and bail out */
  2100. if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
  2101. goto cleanup;
  2102. if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
  2103. goto cleanup;
  2104. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2105. (EXT4_XATTR_SIZE(i.value_len) >
  2106. EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
  2107. i.in_inode = 1;
  2108. retry_inode:
  2109. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  2110. if (!error && !bs.s.not_found) {
  2111. i.value = NULL;
  2112. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2113. } else if (error == -ENOSPC) {
  2114. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  2115. error = ext4_xattr_block_find(inode, &i, &bs);
  2116. if (error)
  2117. goto cleanup;
  2118. }
  2119. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  2120. if (!error && !is.s.not_found) {
  2121. i.value = NULL;
  2122. error = ext4_xattr_ibody_set(handle, inode, &i,
  2123. &is);
  2124. } else if (error == -ENOSPC) {
  2125. /*
  2126. * Xattr does not fit in the block, store at
  2127. * external inode if possible.
  2128. */
  2129. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2130. !i.in_inode) {
  2131. i.in_inode = 1;
  2132. goto retry_inode;
  2133. }
  2134. }
  2135. }
  2136. }
  2137. if (!error) {
  2138. ext4_xattr_update_super_block(handle, inode->i_sb);
  2139. inode->i_ctime = current_time(inode);
  2140. if (!value)
  2141. no_expand = 0;
  2142. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  2143. /*
  2144. * The bh is consumed by ext4_mark_iloc_dirty, even with
  2145. * error != 0.
  2146. */
  2147. is.iloc.bh = NULL;
  2148. if (IS_SYNC(inode))
  2149. ext4_handle_sync(handle);
  2150. }
  2151. cleanup:
  2152. brelse(is.iloc.bh);
  2153. brelse(bs.bh);
  2154. ext4_write_unlock_xattr(inode, &no_expand);
  2155. return error;
  2156. }
  2157. int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
  2158. bool is_create, int *credits)
  2159. {
  2160. struct buffer_head *bh;
  2161. int err;
  2162. *credits = 0;
  2163. if (!EXT4_SB(inode->i_sb)->s_journal)
  2164. return 0;
  2165. down_read(&EXT4_I(inode)->xattr_sem);
  2166. bh = ext4_xattr_get_block(inode);
  2167. if (IS_ERR(bh)) {
  2168. err = PTR_ERR(bh);
  2169. } else {
  2170. *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
  2171. value_len, is_create);
  2172. brelse(bh);
  2173. err = 0;
  2174. }
  2175. up_read(&EXT4_I(inode)->xattr_sem);
  2176. return err;
  2177. }
  2178. /*
  2179. * ext4_xattr_set()
  2180. *
  2181. * Like ext4_xattr_set_handle, but start from an inode. This extended
  2182. * attribute modification is a filesystem transaction by itself.
  2183. *
  2184. * Returns 0, or a negative error number on failure.
  2185. */
  2186. int
  2187. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  2188. const void *value, size_t value_len, int flags)
  2189. {
  2190. handle_t *handle;
  2191. struct super_block *sb = inode->i_sb;
  2192. int error, retries = 0;
  2193. int credits;
  2194. error = dquot_initialize(inode);
  2195. if (error)
  2196. return error;
  2197. retry:
  2198. error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
  2199. &credits);
  2200. if (error)
  2201. return error;
  2202. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  2203. if (IS_ERR(handle)) {
  2204. error = PTR_ERR(handle);
  2205. } else {
  2206. int error2;
  2207. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  2208. value, value_len, flags);
  2209. error2 = ext4_journal_stop(handle);
  2210. if (error == -ENOSPC &&
  2211. ext4_should_retry_alloc(sb, &retries))
  2212. goto retry;
  2213. if (error == 0)
  2214. error = error2;
  2215. }
  2216. return error;
  2217. }
  2218. /*
  2219. * Shift the EA entries in the inode to create space for the increased
  2220. * i_extra_isize.
  2221. */
  2222. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  2223. int value_offs_shift, void *to,
  2224. void *from, size_t n)
  2225. {
  2226. struct ext4_xattr_entry *last = entry;
  2227. int new_offs;
  2228. /* We always shift xattr headers further thus offsets get lower */
  2229. BUG_ON(value_offs_shift > 0);
  2230. /* Adjust the value offsets of the entries */
  2231. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  2232. if (!last->e_value_inum && last->e_value_size) {
  2233. new_offs = le16_to_cpu(last->e_value_offs) +
  2234. value_offs_shift;
  2235. last->e_value_offs = cpu_to_le16(new_offs);
  2236. }
  2237. }
  2238. /* Shift the entries by n bytes */
  2239. memmove(to, from, n);
  2240. }
  2241. /*
  2242. * Move xattr pointed to by 'entry' from inode into external xattr block
  2243. */
  2244. static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
  2245. struct ext4_inode *raw_inode,
  2246. struct ext4_xattr_entry *entry)
  2247. {
  2248. struct ext4_xattr_ibody_find *is = NULL;
  2249. struct ext4_xattr_block_find *bs = NULL;
  2250. char *buffer = NULL, *b_entry_name = NULL;
  2251. size_t value_size = le32_to_cpu(entry->e_value_size);
  2252. struct ext4_xattr_info i = {
  2253. .value = NULL,
  2254. .value_len = 0,
  2255. .name_index = entry->e_name_index,
  2256. .in_inode = !!entry->e_value_inum,
  2257. };
  2258. struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
  2259. int error;
  2260. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  2261. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  2262. buffer = kmalloc(value_size, GFP_NOFS);
  2263. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  2264. if (!is || !bs || !buffer || !b_entry_name) {
  2265. error = -ENOMEM;
  2266. goto out;
  2267. }
  2268. is->s.not_found = -ENODATA;
  2269. bs->s.not_found = -ENODATA;
  2270. is->iloc.bh = NULL;
  2271. bs->bh = NULL;
  2272. /* Save the entry name and the entry value */
  2273. if (entry->e_value_inum) {
  2274. error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
  2275. if (error)
  2276. goto out;
  2277. } else {
  2278. size_t value_offs = le16_to_cpu(entry->e_value_offs);
  2279. memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
  2280. }
  2281. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  2282. b_entry_name[entry->e_name_len] = '\0';
  2283. i.name = b_entry_name;
  2284. error = ext4_get_inode_loc(inode, &is->iloc);
  2285. if (error)
  2286. goto out;
  2287. error = ext4_xattr_ibody_find(inode, &i, is);
  2288. if (error)
  2289. goto out;
  2290. /* Remove the chosen entry from the inode */
  2291. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  2292. if (error)
  2293. goto out;
  2294. i.value = buffer;
  2295. i.value_len = value_size;
  2296. error = ext4_xattr_block_find(inode, &i, bs);
  2297. if (error)
  2298. goto out;
  2299. /* Add entry which was removed from the inode into the block */
  2300. error = ext4_xattr_block_set(handle, inode, &i, bs);
  2301. if (error)
  2302. goto out;
  2303. error = 0;
  2304. out:
  2305. kfree(b_entry_name);
  2306. kfree(buffer);
  2307. if (is)
  2308. brelse(is->iloc.bh);
  2309. kfree(is);
  2310. kfree(bs);
  2311. return error;
  2312. }
  2313. static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
  2314. struct ext4_inode *raw_inode,
  2315. int isize_diff, size_t ifree,
  2316. size_t bfree, int *total_ino)
  2317. {
  2318. struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
  2319. struct ext4_xattr_entry *small_entry;
  2320. struct ext4_xattr_entry *entry;
  2321. struct ext4_xattr_entry *last;
  2322. unsigned int entry_size; /* EA entry size */
  2323. unsigned int total_size; /* EA entry size + value size */
  2324. unsigned int min_total_size;
  2325. int error;
  2326. while (isize_diff > ifree) {
  2327. entry = NULL;
  2328. small_entry = NULL;
  2329. min_total_size = ~0U;
  2330. last = IFIRST(header);
  2331. /* Find the entry best suited to be pushed into EA block */
  2332. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  2333. /* never move system.data out of the inode */
  2334. if ((last->e_name_len == 4) &&
  2335. (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
  2336. !memcmp(last->e_name, "data", 4))
  2337. continue;
  2338. total_size = EXT4_XATTR_LEN(last->e_name_len);
  2339. if (!last->e_value_inum)
  2340. total_size += EXT4_XATTR_SIZE(
  2341. le32_to_cpu(last->e_value_size));
  2342. if (total_size <= bfree &&
  2343. total_size < min_total_size) {
  2344. if (total_size + ifree < isize_diff) {
  2345. small_entry = last;
  2346. } else {
  2347. entry = last;
  2348. min_total_size = total_size;
  2349. }
  2350. }
  2351. }
  2352. if (entry == NULL) {
  2353. if (small_entry == NULL)
  2354. return -ENOSPC;
  2355. entry = small_entry;
  2356. }
  2357. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  2358. total_size = entry_size;
  2359. if (!entry->e_value_inum)
  2360. total_size += EXT4_XATTR_SIZE(
  2361. le32_to_cpu(entry->e_value_size));
  2362. error = ext4_xattr_move_to_block(handle, inode, raw_inode,
  2363. entry);
  2364. if (error)
  2365. return error;
  2366. *total_ino -= entry_size;
  2367. ifree += total_size;
  2368. bfree -= total_size;
  2369. }
  2370. return 0;
  2371. }
  2372. /*
  2373. * Expand an inode by new_extra_isize bytes when EAs are present.
  2374. * Returns 0 on success or negative error number on failure.
  2375. */
  2376. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  2377. struct ext4_inode *raw_inode, handle_t *handle)
  2378. {
  2379. struct ext4_xattr_ibody_header *header;
  2380. struct buffer_head *bh;
  2381. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2382. static unsigned int mnt_count;
  2383. size_t min_offs;
  2384. size_t ifree, bfree;
  2385. int total_ino;
  2386. void *base, *end;
  2387. int error = 0, tried_min_extra_isize = 0;
  2388. int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
  2389. int isize_diff; /* How much do we need to grow i_extra_isize */
  2390. retry:
  2391. isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
  2392. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
  2393. return 0;
  2394. header = IHDR(inode, raw_inode);
  2395. /*
  2396. * Check if enough free space is available in the inode to shift the
  2397. * entries ahead by new_extra_isize.
  2398. */
  2399. base = IFIRST(header);
  2400. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  2401. min_offs = end - base;
  2402. total_ino = sizeof(struct ext4_xattr_ibody_header);
  2403. error = xattr_check_inode(inode, header, end);
  2404. if (error)
  2405. goto cleanup;
  2406. ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
  2407. if (ifree >= isize_diff)
  2408. goto shift;
  2409. /*
  2410. * Enough free space isn't available in the inode, check if
  2411. * EA block can hold new_extra_isize bytes.
  2412. */
  2413. if (EXT4_I(inode)->i_file_acl) {
  2414. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  2415. error = -EIO;
  2416. if (!bh)
  2417. goto cleanup;
  2418. error = ext4_xattr_check_block(inode, bh);
  2419. if (error)
  2420. goto cleanup;
  2421. base = BHDR(bh);
  2422. end = bh->b_data + bh->b_size;
  2423. min_offs = end - base;
  2424. bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
  2425. NULL);
  2426. brelse(bh);
  2427. if (bfree + ifree < isize_diff) {
  2428. if (!tried_min_extra_isize && s_min_extra_isize) {
  2429. tried_min_extra_isize++;
  2430. new_extra_isize = s_min_extra_isize;
  2431. goto retry;
  2432. }
  2433. error = -ENOSPC;
  2434. goto cleanup;
  2435. }
  2436. } else {
  2437. bfree = inode->i_sb->s_blocksize;
  2438. }
  2439. error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
  2440. isize_diff, ifree, bfree,
  2441. &total_ino);
  2442. if (error) {
  2443. if (error == -ENOSPC && !tried_min_extra_isize &&
  2444. s_min_extra_isize) {
  2445. tried_min_extra_isize++;
  2446. new_extra_isize = s_min_extra_isize;
  2447. goto retry;
  2448. }
  2449. goto cleanup;
  2450. }
  2451. shift:
  2452. /* Adjust the offsets and shift the remaining entries ahead */
  2453. ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
  2454. - new_extra_isize, (void *)raw_inode +
  2455. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  2456. (void *)header, total_ino);
  2457. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  2458. cleanup:
  2459. if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
  2460. ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
  2461. inode->i_ino);
  2462. mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
  2463. }
  2464. return error;
  2465. }
  2466. #define EIA_INCR 16 /* must be 2^n */
  2467. #define EIA_MASK (EIA_INCR - 1)
  2468. /* Add the large xattr @inode into @ea_inode_array for deferred iput().
  2469. * If @ea_inode_array is new or full it will be grown and the old
  2470. * contents copied over.
  2471. */
  2472. static int
  2473. ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
  2474. struct inode *inode)
  2475. {
  2476. if (*ea_inode_array == NULL) {
  2477. /*
  2478. * Start with 15 inodes, so it fits into a power-of-two size.
  2479. * If *ea_inode_array is NULL, this is essentially offsetof()
  2480. */
  2481. (*ea_inode_array) =
  2482. kmalloc(offsetof(struct ext4_xattr_inode_array,
  2483. inodes[EIA_MASK]),
  2484. GFP_NOFS);
  2485. if (*ea_inode_array == NULL)
  2486. return -ENOMEM;
  2487. (*ea_inode_array)->count = 0;
  2488. } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
  2489. /* expand the array once all 15 + n * 16 slots are full */
  2490. struct ext4_xattr_inode_array *new_array = NULL;
  2491. int count = (*ea_inode_array)->count;
  2492. /* if new_array is NULL, this is essentially offsetof() */
  2493. new_array = kmalloc(
  2494. offsetof(struct ext4_xattr_inode_array,
  2495. inodes[count + EIA_INCR]),
  2496. GFP_NOFS);
  2497. if (new_array == NULL)
  2498. return -ENOMEM;
  2499. memcpy(new_array, *ea_inode_array,
  2500. offsetof(struct ext4_xattr_inode_array, inodes[count]));
  2501. kfree(*ea_inode_array);
  2502. *ea_inode_array = new_array;
  2503. }
  2504. (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
  2505. return 0;
  2506. }
  2507. /*
  2508. * ext4_xattr_delete_inode()
  2509. *
  2510. * Free extended attribute resources associated with this inode. Traverse
  2511. * all entries and decrement reference on any xattr inodes associated with this
  2512. * inode. This is called immediately before an inode is freed. We have exclusive
  2513. * access to the inode. If an orphan inode is deleted it will also release its
  2514. * references on xattr block and xattr inodes.
  2515. */
  2516. int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
  2517. struct ext4_xattr_inode_array **ea_inode_array,
  2518. int extra_credits)
  2519. {
  2520. struct buffer_head *bh = NULL;
  2521. struct ext4_xattr_ibody_header *header;
  2522. struct ext4_iloc iloc = { .bh = NULL };
  2523. struct ext4_xattr_entry *entry;
  2524. struct inode *ea_inode;
  2525. int error;
  2526. error = ext4_xattr_ensure_credits(handle, inode, extra_credits,
  2527. NULL /* bh */,
  2528. false /* dirty */,
  2529. false /* block_csum */);
  2530. if (error) {
  2531. EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
  2532. goto cleanup;
  2533. }
  2534. if (ext4_has_feature_ea_inode(inode->i_sb) &&
  2535. ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  2536. error = ext4_get_inode_loc(inode, &iloc);
  2537. if (error) {
  2538. EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
  2539. goto cleanup;
  2540. }
  2541. error = ext4_journal_get_write_access(handle, iloc.bh);
  2542. if (error) {
  2543. EXT4_ERROR_INODE(inode, "write access (error %d)",
  2544. error);
  2545. goto cleanup;
  2546. }
  2547. header = IHDR(inode, ext4_raw_inode(&iloc));
  2548. if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
  2549. ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
  2550. IFIRST(header),
  2551. false /* block_csum */,
  2552. ea_inode_array,
  2553. extra_credits,
  2554. false /* skip_quota */);
  2555. }
  2556. if (EXT4_I(inode)->i_file_acl) {
  2557. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  2558. if (!bh) {
  2559. EXT4_ERROR_INODE(inode, "block %llu read error",
  2560. EXT4_I(inode)->i_file_acl);
  2561. error = -EIO;
  2562. goto cleanup;
  2563. }
  2564. error = ext4_xattr_check_block(inode, bh);
  2565. if (error)
  2566. goto cleanup;
  2567. if (ext4_has_feature_ea_inode(inode->i_sb)) {
  2568. for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
  2569. entry = EXT4_XATTR_NEXT(entry)) {
  2570. if (!entry->e_value_inum)
  2571. continue;
  2572. error = ext4_xattr_inode_iget(inode,
  2573. le32_to_cpu(entry->e_value_inum),
  2574. le32_to_cpu(entry->e_hash),
  2575. &ea_inode);
  2576. if (error)
  2577. continue;
  2578. ext4_xattr_inode_free_quota(inode, ea_inode,
  2579. le32_to_cpu(entry->e_value_size));
  2580. iput(ea_inode);
  2581. }
  2582. }
  2583. ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
  2584. extra_credits);
  2585. /*
  2586. * Update i_file_acl value in the same transaction that releases
  2587. * block.
  2588. */
  2589. EXT4_I(inode)->i_file_acl = 0;
  2590. error = ext4_mark_inode_dirty(handle, inode);
  2591. if (error) {
  2592. EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
  2593. error);
  2594. goto cleanup;
  2595. }
  2596. }
  2597. error = 0;
  2598. cleanup:
  2599. brelse(iloc.bh);
  2600. brelse(bh);
  2601. return error;
  2602. }
  2603. void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
  2604. {
  2605. int idx;
  2606. if (ea_inode_array == NULL)
  2607. return;
  2608. for (idx = 0; idx < ea_inode_array->count; ++idx)
  2609. iput(ea_inode_array->inodes[idx]);
  2610. kfree(ea_inode_array);
  2611. }
  2612. /*
  2613. * ext4_xattr_block_cache_insert()
  2614. *
  2615. * Create a new entry in the extended attribute block cache, and insert
  2616. * it unless such an entry is already in the cache.
  2617. *
  2618. * Returns 0, or a negative error number on failure.
  2619. */
  2620. static void
  2621. ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
  2622. struct buffer_head *bh)
  2623. {
  2624. struct ext4_xattr_header *header = BHDR(bh);
  2625. __u32 hash = le32_to_cpu(header->h_hash);
  2626. int reusable = le32_to_cpu(header->h_refcount) <
  2627. EXT4_XATTR_REFCOUNT_MAX;
  2628. int error;
  2629. if (!ea_block_cache)
  2630. return;
  2631. error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
  2632. bh->b_blocknr, reusable);
  2633. if (error) {
  2634. if (error == -EBUSY)
  2635. ea_bdebug(bh, "already in cache");
  2636. } else
  2637. ea_bdebug(bh, "inserting [%x]", (int)hash);
  2638. }
  2639. /*
  2640. * ext4_xattr_cmp()
  2641. *
  2642. * Compare two extended attribute blocks for equality.
  2643. *
  2644. * Returns 0 if the blocks are equal, 1 if they differ, and
  2645. * a negative error number on errors.
  2646. */
  2647. static int
  2648. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  2649. struct ext4_xattr_header *header2)
  2650. {
  2651. struct ext4_xattr_entry *entry1, *entry2;
  2652. entry1 = ENTRY(header1+1);
  2653. entry2 = ENTRY(header2+1);
  2654. while (!IS_LAST_ENTRY(entry1)) {
  2655. if (IS_LAST_ENTRY(entry2))
  2656. return 1;
  2657. if (entry1->e_hash != entry2->e_hash ||
  2658. entry1->e_name_index != entry2->e_name_index ||
  2659. entry1->e_name_len != entry2->e_name_len ||
  2660. entry1->e_value_size != entry2->e_value_size ||
  2661. entry1->e_value_inum != entry2->e_value_inum ||
  2662. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  2663. return 1;
  2664. if (!entry1->e_value_inum &&
  2665. memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  2666. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  2667. le32_to_cpu(entry1->e_value_size)))
  2668. return 1;
  2669. entry1 = EXT4_XATTR_NEXT(entry1);
  2670. entry2 = EXT4_XATTR_NEXT(entry2);
  2671. }
  2672. if (!IS_LAST_ENTRY(entry2))
  2673. return 1;
  2674. return 0;
  2675. }
  2676. /*
  2677. * ext4_xattr_block_cache_find()
  2678. *
  2679. * Find an identical extended attribute block.
  2680. *
  2681. * Returns a pointer to the block found, or NULL if such a block was
  2682. * not found or an error occurred.
  2683. */
  2684. static struct buffer_head *
  2685. ext4_xattr_block_cache_find(struct inode *inode,
  2686. struct ext4_xattr_header *header,
  2687. struct mb_cache_entry **pce)
  2688. {
  2689. __u32 hash = le32_to_cpu(header->h_hash);
  2690. struct mb_cache_entry *ce;
  2691. struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
  2692. if (!ea_block_cache)
  2693. return NULL;
  2694. if (!header->h_hash)
  2695. return NULL; /* never share */
  2696. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  2697. ce = mb_cache_entry_find_first(ea_block_cache, hash);
  2698. while (ce) {
  2699. struct buffer_head *bh;
  2700. bh = sb_bread(inode->i_sb, ce->e_value);
  2701. if (!bh) {
  2702. EXT4_ERROR_INODE(inode, "block %lu read error",
  2703. (unsigned long)ce->e_value);
  2704. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  2705. *pce = ce;
  2706. return bh;
  2707. }
  2708. brelse(bh);
  2709. ce = mb_cache_entry_find_next(ea_block_cache, ce);
  2710. }
  2711. return NULL;
  2712. }
  2713. #define NAME_HASH_SHIFT 5
  2714. #define VALUE_HASH_SHIFT 16
  2715. /*
  2716. * ext4_xattr_hash_entry()
  2717. *
  2718. * Compute the hash of an extended attribute.
  2719. */
  2720. static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
  2721. size_t value_count)
  2722. {
  2723. __u32 hash = 0;
  2724. while (name_len--) {
  2725. hash = (hash << NAME_HASH_SHIFT) ^
  2726. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  2727. *name++;
  2728. }
  2729. while (value_count--) {
  2730. hash = (hash << VALUE_HASH_SHIFT) ^
  2731. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  2732. le32_to_cpu(*value++);
  2733. }
  2734. return cpu_to_le32(hash);
  2735. }
  2736. #undef NAME_HASH_SHIFT
  2737. #undef VALUE_HASH_SHIFT
  2738. #define BLOCK_HASH_SHIFT 16
  2739. /*
  2740. * ext4_xattr_rehash()
  2741. *
  2742. * Re-compute the extended attribute hash value after an entry has changed.
  2743. */
  2744. static void ext4_xattr_rehash(struct ext4_xattr_header *header)
  2745. {
  2746. struct ext4_xattr_entry *here;
  2747. __u32 hash = 0;
  2748. here = ENTRY(header+1);
  2749. while (!IS_LAST_ENTRY(here)) {
  2750. if (!here->e_hash) {
  2751. /* Block is not shared if an entry's hash value == 0 */
  2752. hash = 0;
  2753. break;
  2754. }
  2755. hash = (hash << BLOCK_HASH_SHIFT) ^
  2756. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  2757. le32_to_cpu(here->e_hash);
  2758. here = EXT4_XATTR_NEXT(here);
  2759. }
  2760. header->h_hash = cpu_to_le32(hash);
  2761. }
  2762. #undef BLOCK_HASH_SHIFT
  2763. #define HASH_BUCKET_BITS 10
  2764. struct mb_cache *
  2765. ext4_xattr_create_cache(void)
  2766. {
  2767. return mb_cache_create(HASH_BUCKET_BITS);
  2768. }
  2769. void ext4_xattr_destroy_cache(struct mb_cache *cache)
  2770. {
  2771. if (cache)
  2772. mb_cache_destroy(cache);
  2773. }