xattr.c 81 KB

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