xattr.c 82 KB

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