xattr.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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, f...) do { \
  62. printk(KERN_DEBUG "inode %s:%lu: ", \
  63. inode->i_sb->s_id, inode->i_ino); \
  64. printk(f); \
  65. printk("\n"); \
  66. } while (0)
  67. # define ea_bdebug(bh, f...) do { \
  68. char b[BDEVNAME_SIZE]; \
  69. printk(KERN_DEBUG "block %s:%lu: ", \
  70. bdevname(bh->b_bdev, b), \
  71. (unsigned long) bh->b_blocknr); \
  72. printk(f); \
  73. printk("\n"); \
  74. } while (0)
  75. #else
  76. # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  77. # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  78. #endif
  79. static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
  80. static struct buffer_head *ext4_xattr_cache_find(struct inode *,
  81. struct ext4_xattr_header *,
  82. struct mb_cache_entry **);
  83. static void ext4_xattr_rehash(struct ext4_xattr_header *,
  84. struct ext4_xattr_entry *);
  85. static int ext4_xattr_list(struct dentry *dentry, char *buffer,
  86. size_t buffer_size);
  87. static const struct xattr_handler *ext4_xattr_handler_map[] = {
  88. [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
  89. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  90. [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
  91. [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
  92. #endif
  93. [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
  94. #ifdef CONFIG_EXT4_FS_SECURITY
  95. [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
  96. #endif
  97. };
  98. const struct xattr_handler *ext4_xattr_handlers[] = {
  99. &ext4_xattr_user_handler,
  100. &ext4_xattr_trusted_handler,
  101. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  102. &posix_acl_access_xattr_handler,
  103. &posix_acl_default_xattr_handler,
  104. #endif
  105. #ifdef CONFIG_EXT4_FS_SECURITY
  106. &ext4_xattr_security_handler,
  107. #endif
  108. NULL
  109. };
  110. #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
  111. inode->i_sb->s_fs_info)->s_mb_cache)
  112. static __le32 ext4_xattr_block_csum(struct inode *inode,
  113. sector_t block_nr,
  114. struct ext4_xattr_header *hdr)
  115. {
  116. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  117. __u32 csum;
  118. __le32 save_csum;
  119. __le64 dsk_block_nr = cpu_to_le64(block_nr);
  120. save_csum = hdr->h_checksum;
  121. hdr->h_checksum = 0;
  122. csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
  123. sizeof(dsk_block_nr));
  124. csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
  125. EXT4_BLOCK_SIZE(inode->i_sb));
  126. hdr->h_checksum = save_csum;
  127. return cpu_to_le32(csum);
  128. }
  129. static int ext4_xattr_block_csum_verify(struct inode *inode,
  130. sector_t block_nr,
  131. struct ext4_xattr_header *hdr)
  132. {
  133. if (ext4_has_metadata_csum(inode->i_sb) &&
  134. (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
  135. return 0;
  136. return 1;
  137. }
  138. static void ext4_xattr_block_csum_set(struct inode *inode,
  139. sector_t block_nr,
  140. struct ext4_xattr_header *hdr)
  141. {
  142. if (!ext4_has_metadata_csum(inode->i_sb))
  143. return;
  144. hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
  145. }
  146. static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
  147. struct inode *inode,
  148. struct buffer_head *bh)
  149. {
  150. ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
  151. return ext4_handle_dirty_metadata(handle, inode, 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. /*
  162. * Inode operation listxattr()
  163. *
  164. * d_inode(dentry)->i_mutex: don't care
  165. */
  166. ssize_t
  167. ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
  168. {
  169. return ext4_xattr_list(dentry, buffer, size);
  170. }
  171. static int
  172. ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
  173. void *value_start)
  174. {
  175. struct ext4_xattr_entry *e = entry;
  176. while (!IS_LAST_ENTRY(e)) {
  177. struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
  178. if ((void *)next >= end)
  179. return -EIO;
  180. e = next;
  181. }
  182. while (!IS_LAST_ENTRY(entry)) {
  183. if (entry->e_value_size != 0 &&
  184. (value_start + le16_to_cpu(entry->e_value_offs) <
  185. (void *)e + sizeof(__u32) ||
  186. value_start + le16_to_cpu(entry->e_value_offs) +
  187. le32_to_cpu(entry->e_value_size) > end))
  188. return -EIO;
  189. entry = EXT4_XATTR_NEXT(entry);
  190. }
  191. return 0;
  192. }
  193. static inline int
  194. ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
  195. {
  196. int error;
  197. if (buffer_verified(bh))
  198. return 0;
  199. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  200. BHDR(bh)->h_blocks != cpu_to_le32(1))
  201. return -EIO;
  202. if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
  203. return -EIO;
  204. error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
  205. bh->b_data);
  206. if (!error)
  207. set_buffer_verified(bh);
  208. return error;
  209. }
  210. static inline int
  211. ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
  212. {
  213. size_t value_size = le32_to_cpu(entry->e_value_size);
  214. if (entry->e_value_block != 0 || value_size > size ||
  215. le16_to_cpu(entry->e_value_offs) + value_size > size)
  216. return -EIO;
  217. return 0;
  218. }
  219. static int
  220. ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
  221. const char *name, size_t size, int sorted)
  222. {
  223. struct ext4_xattr_entry *entry;
  224. size_t name_len;
  225. int cmp = 1;
  226. if (name == NULL)
  227. return -EINVAL;
  228. name_len = strlen(name);
  229. entry = *pentry;
  230. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  231. cmp = name_index - entry->e_name_index;
  232. if (!cmp)
  233. cmp = name_len - entry->e_name_len;
  234. if (!cmp)
  235. cmp = memcmp(name, entry->e_name, name_len);
  236. if (cmp <= 0 && (sorted || cmp == 0))
  237. break;
  238. }
  239. *pentry = entry;
  240. if (!cmp && ext4_xattr_check_entry(entry, size))
  241. return -EIO;
  242. return cmp ? -ENODATA : 0;
  243. }
  244. static int
  245. ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
  246. void *buffer, size_t buffer_size)
  247. {
  248. struct buffer_head *bh = NULL;
  249. struct ext4_xattr_entry *entry;
  250. size_t size;
  251. int error;
  252. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  253. ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
  254. name_index, name, buffer, (long)buffer_size);
  255. error = -ENODATA;
  256. if (!EXT4_I(inode)->i_file_acl)
  257. goto cleanup;
  258. ea_idebug(inode, "reading block %llu",
  259. (unsigned long long)EXT4_I(inode)->i_file_acl);
  260. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  261. if (!bh)
  262. goto cleanup;
  263. ea_bdebug(bh, "b_count=%d, refcount=%d",
  264. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  265. if (ext4_xattr_check_block(inode, bh)) {
  266. bad_block:
  267. EXT4_ERROR_INODE(inode, "bad block %llu",
  268. EXT4_I(inode)->i_file_acl);
  269. error = -EIO;
  270. goto cleanup;
  271. }
  272. ext4_xattr_cache_insert(ext4_mb_cache, bh);
  273. entry = BFIRST(bh);
  274. error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
  275. if (error == -EIO)
  276. goto bad_block;
  277. if (error)
  278. goto cleanup;
  279. size = le32_to_cpu(entry->e_value_size);
  280. if (buffer) {
  281. error = -ERANGE;
  282. if (size > buffer_size)
  283. goto cleanup;
  284. memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
  285. size);
  286. }
  287. error = size;
  288. cleanup:
  289. brelse(bh);
  290. return error;
  291. }
  292. int
  293. ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
  294. void *buffer, size_t buffer_size)
  295. {
  296. struct ext4_xattr_ibody_header *header;
  297. struct ext4_xattr_entry *entry;
  298. struct ext4_inode *raw_inode;
  299. struct ext4_iloc iloc;
  300. size_t size;
  301. void *end;
  302. int error;
  303. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  304. return -ENODATA;
  305. error = ext4_get_inode_loc(inode, &iloc);
  306. if (error)
  307. return error;
  308. raw_inode = ext4_raw_inode(&iloc);
  309. header = IHDR(inode, raw_inode);
  310. entry = IFIRST(header);
  311. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  312. error = ext4_xattr_check_names(entry, end, entry);
  313. if (error)
  314. goto cleanup;
  315. error = ext4_xattr_find_entry(&entry, name_index, name,
  316. end - (void *)entry, 0);
  317. if (error)
  318. goto cleanup;
  319. size = le32_to_cpu(entry->e_value_size);
  320. if (buffer) {
  321. error = -ERANGE;
  322. if (size > buffer_size)
  323. goto cleanup;
  324. memcpy(buffer, (void *)IFIRST(header) +
  325. le16_to_cpu(entry->e_value_offs), size);
  326. }
  327. error = size;
  328. cleanup:
  329. brelse(iloc.bh);
  330. return error;
  331. }
  332. /*
  333. * ext4_xattr_get()
  334. *
  335. * Copy an extended attribute into the buffer
  336. * provided, or compute the buffer size required.
  337. * Buffer is NULL to compute the size of the buffer required.
  338. *
  339. * Returns a negative error number on failure, or the number of bytes
  340. * used / required on success.
  341. */
  342. int
  343. ext4_xattr_get(struct inode *inode, int name_index, const char *name,
  344. void *buffer, size_t buffer_size)
  345. {
  346. int error;
  347. if (strlen(name) > 255)
  348. return -ERANGE;
  349. down_read(&EXT4_I(inode)->xattr_sem);
  350. error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
  351. buffer_size);
  352. if (error == -ENODATA)
  353. error = ext4_xattr_block_get(inode, name_index, name, buffer,
  354. buffer_size);
  355. up_read(&EXT4_I(inode)->xattr_sem);
  356. return error;
  357. }
  358. static int
  359. ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
  360. char *buffer, size_t buffer_size)
  361. {
  362. size_t rest = buffer_size;
  363. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  364. const struct xattr_handler *handler =
  365. ext4_xattr_handler(entry->e_name_index);
  366. if (handler) {
  367. size_t size = handler->list(dentry, buffer, rest,
  368. entry->e_name,
  369. entry->e_name_len,
  370. handler->flags);
  371. if (buffer) {
  372. if (size > rest)
  373. return -ERANGE;
  374. buffer += size;
  375. }
  376. rest -= size;
  377. }
  378. }
  379. return buffer_size - rest;
  380. }
  381. static int
  382. ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  383. {
  384. struct inode *inode = d_inode(dentry);
  385. struct buffer_head *bh = NULL;
  386. int error;
  387. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  388. ea_idebug(inode, "buffer=%p, buffer_size=%ld",
  389. buffer, (long)buffer_size);
  390. error = 0;
  391. if (!EXT4_I(inode)->i_file_acl)
  392. goto cleanup;
  393. ea_idebug(inode, "reading block %llu",
  394. (unsigned long long)EXT4_I(inode)->i_file_acl);
  395. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  396. error = -EIO;
  397. if (!bh)
  398. goto cleanup;
  399. ea_bdebug(bh, "b_count=%d, refcount=%d",
  400. atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
  401. if (ext4_xattr_check_block(inode, bh)) {
  402. EXT4_ERROR_INODE(inode, "bad block %llu",
  403. EXT4_I(inode)->i_file_acl);
  404. error = -EIO;
  405. goto cleanup;
  406. }
  407. ext4_xattr_cache_insert(ext4_mb_cache, bh);
  408. error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
  409. cleanup:
  410. brelse(bh);
  411. return error;
  412. }
  413. static int
  414. ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  415. {
  416. struct inode *inode = d_inode(dentry);
  417. struct ext4_xattr_ibody_header *header;
  418. struct ext4_inode *raw_inode;
  419. struct ext4_iloc iloc;
  420. void *end;
  421. int error;
  422. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  423. return 0;
  424. error = ext4_get_inode_loc(inode, &iloc);
  425. if (error)
  426. return error;
  427. raw_inode = ext4_raw_inode(&iloc);
  428. header = IHDR(inode, raw_inode);
  429. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  430. error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
  431. if (error)
  432. goto cleanup;
  433. error = ext4_xattr_list_entries(dentry, IFIRST(header),
  434. buffer, buffer_size);
  435. cleanup:
  436. brelse(iloc.bh);
  437. return error;
  438. }
  439. /*
  440. * ext4_xattr_list()
  441. *
  442. * Copy a list of attribute names into the buffer
  443. * provided, or compute the buffer size required.
  444. * Buffer is NULL to compute the size of the buffer required.
  445. *
  446. * Returns a negative error number on failure, or the number of bytes
  447. * used / required on success.
  448. */
  449. static int
  450. ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
  451. {
  452. int ret, ret2;
  453. down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  454. ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
  455. if (ret < 0)
  456. goto errout;
  457. if (buffer) {
  458. buffer += ret;
  459. buffer_size -= ret;
  460. }
  461. ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
  462. if (ret < 0)
  463. goto errout;
  464. ret += ret2;
  465. errout:
  466. up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
  467. return ret;
  468. }
  469. /*
  470. * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
  471. * not set, set it.
  472. */
  473. static void ext4_xattr_update_super_block(handle_t *handle,
  474. struct super_block *sb)
  475. {
  476. if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
  477. return;
  478. BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
  479. if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
  480. EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
  481. ext4_handle_dirty_super(handle, sb);
  482. }
  483. }
  484. /*
  485. * Release the xattr block BH: If the reference count is > 1, decrement it;
  486. * otherwise free the block.
  487. */
  488. static void
  489. ext4_xattr_release_block(handle_t *handle, struct inode *inode,
  490. struct buffer_head *bh)
  491. {
  492. struct mb_cache_entry *ce = NULL;
  493. int error = 0;
  494. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  495. ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
  496. BUFFER_TRACE(bh, "get_write_access");
  497. error = ext4_journal_get_write_access(handle, bh);
  498. if (error)
  499. goto out;
  500. lock_buffer(bh);
  501. if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
  502. ea_bdebug(bh, "refcount now=0; freeing");
  503. if (ce)
  504. mb_cache_entry_free(ce);
  505. get_bh(bh);
  506. unlock_buffer(bh);
  507. ext4_free_blocks(handle, inode, bh, 0, 1,
  508. EXT4_FREE_BLOCKS_METADATA |
  509. EXT4_FREE_BLOCKS_FORGET);
  510. } else {
  511. le32_add_cpu(&BHDR(bh)->h_refcount, -1);
  512. if (ce)
  513. mb_cache_entry_release(ce);
  514. /*
  515. * Beware of this ugliness: Releasing of xattr block references
  516. * from different inodes can race and so we have to protect
  517. * from a race where someone else frees the block (and releases
  518. * its journal_head) before we are done dirtying the buffer. In
  519. * nojournal mode this race is harmless and we actually cannot
  520. * call ext4_handle_dirty_xattr_block() with locked buffer as
  521. * that function can call sync_dirty_buffer() so for that case
  522. * we handle the dirtying after unlocking the buffer.
  523. */
  524. if (ext4_handle_valid(handle))
  525. error = ext4_handle_dirty_xattr_block(handle, inode,
  526. bh);
  527. unlock_buffer(bh);
  528. if (!ext4_handle_valid(handle))
  529. error = ext4_handle_dirty_xattr_block(handle, inode,
  530. bh);
  531. if (IS_SYNC(inode))
  532. ext4_handle_sync(handle);
  533. dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
  534. ea_bdebug(bh, "refcount now=%d; releasing",
  535. le32_to_cpu(BHDR(bh)->h_refcount));
  536. }
  537. out:
  538. ext4_std_error(inode->i_sb, error);
  539. return;
  540. }
  541. /*
  542. * Find the available free space for EAs. This also returns the total number of
  543. * bytes used by EA entries.
  544. */
  545. static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
  546. size_t *min_offs, void *base, int *total)
  547. {
  548. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  549. if (!last->e_value_block && last->e_value_size) {
  550. size_t offs = le16_to_cpu(last->e_value_offs);
  551. if (offs < *min_offs)
  552. *min_offs = offs;
  553. }
  554. if (total)
  555. *total += EXT4_XATTR_LEN(last->e_name_len);
  556. }
  557. return (*min_offs - ((void *)last - base) - sizeof(__u32));
  558. }
  559. static int
  560. ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
  561. {
  562. struct ext4_xattr_entry *last;
  563. size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
  564. /* Compute min_offs and last. */
  565. last = s->first;
  566. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  567. if (!last->e_value_block && last->e_value_size) {
  568. size_t offs = le16_to_cpu(last->e_value_offs);
  569. if (offs < min_offs)
  570. min_offs = offs;
  571. }
  572. }
  573. free = min_offs - ((void *)last - s->base) - sizeof(__u32);
  574. if (!s->not_found) {
  575. if (!s->here->e_value_block && s->here->e_value_size) {
  576. size_t size = le32_to_cpu(s->here->e_value_size);
  577. free += EXT4_XATTR_SIZE(size);
  578. }
  579. free += EXT4_XATTR_LEN(name_len);
  580. }
  581. if (i->value) {
  582. if (free < EXT4_XATTR_LEN(name_len) +
  583. EXT4_XATTR_SIZE(i->value_len))
  584. return -ENOSPC;
  585. }
  586. if (i->value && s->not_found) {
  587. /* Insert the new name. */
  588. size_t size = EXT4_XATTR_LEN(name_len);
  589. size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
  590. memmove((void *)s->here + size, s->here, rest);
  591. memset(s->here, 0, size);
  592. s->here->e_name_index = i->name_index;
  593. s->here->e_name_len = name_len;
  594. memcpy(s->here->e_name, i->name, name_len);
  595. } else {
  596. if (!s->here->e_value_block && s->here->e_value_size) {
  597. void *first_val = s->base + min_offs;
  598. size_t offs = le16_to_cpu(s->here->e_value_offs);
  599. void *val = s->base + offs;
  600. size_t size = EXT4_XATTR_SIZE(
  601. le32_to_cpu(s->here->e_value_size));
  602. if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
  603. /* The old and the new value have the same
  604. size. Just replace. */
  605. s->here->e_value_size =
  606. cpu_to_le32(i->value_len);
  607. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  608. memset(val, 0, size);
  609. } else {
  610. /* Clear pad bytes first. */
  611. memset(val + size - EXT4_XATTR_PAD, 0,
  612. EXT4_XATTR_PAD);
  613. memcpy(val, i->value, i->value_len);
  614. }
  615. return 0;
  616. }
  617. /* Remove the old value. */
  618. memmove(first_val + size, first_val, val - first_val);
  619. memset(first_val, 0, size);
  620. s->here->e_value_size = 0;
  621. s->here->e_value_offs = 0;
  622. min_offs += size;
  623. /* Adjust all value offsets. */
  624. last = s->first;
  625. while (!IS_LAST_ENTRY(last)) {
  626. size_t o = le16_to_cpu(last->e_value_offs);
  627. if (!last->e_value_block &&
  628. last->e_value_size && o < offs)
  629. last->e_value_offs =
  630. cpu_to_le16(o + size);
  631. last = EXT4_XATTR_NEXT(last);
  632. }
  633. }
  634. if (!i->value) {
  635. /* Remove the old name. */
  636. size_t size = EXT4_XATTR_LEN(name_len);
  637. last = ENTRY((void *)last - size);
  638. memmove(s->here, (void *)s->here + size,
  639. (void *)last - (void *)s->here + sizeof(__u32));
  640. memset(last, 0, size);
  641. }
  642. }
  643. if (i->value) {
  644. /* Insert the new value. */
  645. s->here->e_value_size = cpu_to_le32(i->value_len);
  646. if (i->value_len) {
  647. size_t size = EXT4_XATTR_SIZE(i->value_len);
  648. void *val = s->base + min_offs - size;
  649. s->here->e_value_offs = cpu_to_le16(min_offs - size);
  650. if (i->value == EXT4_ZERO_XATTR_VALUE) {
  651. memset(val, 0, size);
  652. } else {
  653. /* Clear the pad bytes first. */
  654. memset(val + size - EXT4_XATTR_PAD, 0,
  655. EXT4_XATTR_PAD);
  656. memcpy(val, i->value, i->value_len);
  657. }
  658. }
  659. }
  660. return 0;
  661. }
  662. struct ext4_xattr_block_find {
  663. struct ext4_xattr_search s;
  664. struct buffer_head *bh;
  665. };
  666. static int
  667. ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
  668. struct ext4_xattr_block_find *bs)
  669. {
  670. struct super_block *sb = inode->i_sb;
  671. int error;
  672. ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
  673. i->name_index, i->name, i->value, (long)i->value_len);
  674. if (EXT4_I(inode)->i_file_acl) {
  675. /* The inode already has an extended attribute block. */
  676. bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
  677. error = -EIO;
  678. if (!bs->bh)
  679. goto cleanup;
  680. ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
  681. atomic_read(&(bs->bh->b_count)),
  682. le32_to_cpu(BHDR(bs->bh)->h_refcount));
  683. if (ext4_xattr_check_block(inode, bs->bh)) {
  684. EXT4_ERROR_INODE(inode, "bad block %llu",
  685. EXT4_I(inode)->i_file_acl);
  686. error = -EIO;
  687. goto cleanup;
  688. }
  689. /* Find the named attribute. */
  690. bs->s.base = BHDR(bs->bh);
  691. bs->s.first = BFIRST(bs->bh);
  692. bs->s.end = bs->bh->b_data + bs->bh->b_size;
  693. bs->s.here = bs->s.first;
  694. error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
  695. i->name, bs->bh->b_size, 1);
  696. if (error && error != -ENODATA)
  697. goto cleanup;
  698. bs->s.not_found = error;
  699. }
  700. error = 0;
  701. cleanup:
  702. return error;
  703. }
  704. static int
  705. ext4_xattr_block_set(handle_t *handle, struct inode *inode,
  706. struct ext4_xattr_info *i,
  707. struct ext4_xattr_block_find *bs)
  708. {
  709. struct super_block *sb = inode->i_sb;
  710. struct buffer_head *new_bh = NULL;
  711. struct ext4_xattr_search *s = &bs->s;
  712. struct mb_cache_entry *ce = NULL;
  713. int error = 0;
  714. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  715. #define header(x) ((struct ext4_xattr_header *)(x))
  716. if (i->value && i->value_len > sb->s_blocksize)
  717. return -ENOSPC;
  718. if (s->base) {
  719. ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
  720. bs->bh->b_blocknr);
  721. BUFFER_TRACE(bs->bh, "get_write_access");
  722. error = ext4_journal_get_write_access(handle, bs->bh);
  723. if (error)
  724. goto cleanup;
  725. lock_buffer(bs->bh);
  726. if (header(s->base)->h_refcount == cpu_to_le32(1)) {
  727. if (ce) {
  728. mb_cache_entry_free(ce);
  729. ce = NULL;
  730. }
  731. ea_bdebug(bs->bh, "modifying in-place");
  732. error = ext4_xattr_set_entry(i, s);
  733. if (!error) {
  734. if (!IS_LAST_ENTRY(s->first))
  735. ext4_xattr_rehash(header(s->base),
  736. s->here);
  737. ext4_xattr_cache_insert(ext4_mb_cache,
  738. bs->bh);
  739. }
  740. unlock_buffer(bs->bh);
  741. if (error == -EIO)
  742. goto bad_block;
  743. if (!error)
  744. error = ext4_handle_dirty_xattr_block(handle,
  745. inode,
  746. bs->bh);
  747. if (error)
  748. goto cleanup;
  749. goto inserted;
  750. } else {
  751. int offset = (char *)s->here - bs->bh->b_data;
  752. unlock_buffer(bs->bh);
  753. if (ce) {
  754. mb_cache_entry_release(ce);
  755. ce = NULL;
  756. }
  757. ea_bdebug(bs->bh, "cloning");
  758. s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
  759. error = -ENOMEM;
  760. if (s->base == NULL)
  761. goto cleanup;
  762. memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
  763. s->first = ENTRY(header(s->base)+1);
  764. header(s->base)->h_refcount = cpu_to_le32(1);
  765. s->here = ENTRY(s->base + offset);
  766. s->end = s->base + bs->bh->b_size;
  767. }
  768. } else {
  769. /* Allocate a buffer where we construct the new block. */
  770. s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
  771. /* assert(header == s->base) */
  772. error = -ENOMEM;
  773. if (s->base == NULL)
  774. goto cleanup;
  775. header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  776. header(s->base)->h_blocks = cpu_to_le32(1);
  777. header(s->base)->h_refcount = cpu_to_le32(1);
  778. s->first = ENTRY(header(s->base)+1);
  779. s->here = ENTRY(header(s->base)+1);
  780. s->end = s->base + sb->s_blocksize;
  781. }
  782. error = ext4_xattr_set_entry(i, s);
  783. if (error == -EIO)
  784. goto bad_block;
  785. if (error)
  786. goto cleanup;
  787. if (!IS_LAST_ENTRY(s->first))
  788. ext4_xattr_rehash(header(s->base), s->here);
  789. inserted:
  790. if (!IS_LAST_ENTRY(s->first)) {
  791. new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
  792. if (new_bh) {
  793. /* We found an identical block in the cache. */
  794. if (new_bh == bs->bh)
  795. ea_bdebug(new_bh, "keeping");
  796. else {
  797. /* The old block is released after updating
  798. the inode. */
  799. error = dquot_alloc_block(inode,
  800. EXT4_C2B(EXT4_SB(sb), 1));
  801. if (error)
  802. goto cleanup;
  803. BUFFER_TRACE(new_bh, "get_write_access");
  804. error = ext4_journal_get_write_access(handle,
  805. new_bh);
  806. if (error)
  807. goto cleanup_dquot;
  808. lock_buffer(new_bh);
  809. le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
  810. ea_bdebug(new_bh, "reusing; refcount now=%d",
  811. le32_to_cpu(BHDR(new_bh)->h_refcount));
  812. unlock_buffer(new_bh);
  813. error = ext4_handle_dirty_xattr_block(handle,
  814. inode,
  815. new_bh);
  816. if (error)
  817. goto cleanup_dquot;
  818. }
  819. mb_cache_entry_release(ce);
  820. ce = NULL;
  821. } else if (bs->bh && s->base == bs->bh->b_data) {
  822. /* We were modifying this block in-place. */
  823. ea_bdebug(bs->bh, "keeping this block");
  824. new_bh = bs->bh;
  825. get_bh(new_bh);
  826. } else {
  827. /* We need to allocate a new block */
  828. ext4_fsblk_t goal, block;
  829. goal = ext4_group_first_block_no(sb,
  830. EXT4_I(inode)->i_block_group);
  831. /* non-extent files can't have physical blocks past 2^32 */
  832. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  833. goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
  834. block = ext4_new_meta_blocks(handle, inode, goal, 0,
  835. NULL, &error);
  836. if (error)
  837. goto cleanup;
  838. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  839. BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
  840. ea_idebug(inode, "creating block %llu",
  841. (unsigned long long)block);
  842. new_bh = sb_getblk(sb, block);
  843. if (unlikely(!new_bh)) {
  844. error = -ENOMEM;
  845. getblk_failed:
  846. ext4_free_blocks(handle, inode, NULL, block, 1,
  847. EXT4_FREE_BLOCKS_METADATA);
  848. goto cleanup;
  849. }
  850. lock_buffer(new_bh);
  851. error = ext4_journal_get_create_access(handle, new_bh);
  852. if (error) {
  853. unlock_buffer(new_bh);
  854. error = -EIO;
  855. goto getblk_failed;
  856. }
  857. memcpy(new_bh->b_data, s->base, new_bh->b_size);
  858. set_buffer_uptodate(new_bh);
  859. unlock_buffer(new_bh);
  860. ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
  861. error = ext4_handle_dirty_xattr_block(handle,
  862. inode, new_bh);
  863. if (error)
  864. goto cleanup;
  865. }
  866. }
  867. /* Update the inode. */
  868. EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
  869. /* Drop the previous xattr block. */
  870. if (bs->bh && bs->bh != new_bh)
  871. ext4_xattr_release_block(handle, inode, bs->bh);
  872. error = 0;
  873. cleanup:
  874. if (ce)
  875. mb_cache_entry_release(ce);
  876. brelse(new_bh);
  877. if (!(bs->bh && s->base == bs->bh->b_data))
  878. kfree(s->base);
  879. return error;
  880. cleanup_dquot:
  881. dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
  882. goto cleanup;
  883. bad_block:
  884. EXT4_ERROR_INODE(inode, "bad block %llu",
  885. EXT4_I(inode)->i_file_acl);
  886. goto cleanup;
  887. #undef header
  888. }
  889. int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
  890. struct ext4_xattr_ibody_find *is)
  891. {
  892. struct ext4_xattr_ibody_header *header;
  893. struct ext4_inode *raw_inode;
  894. int error;
  895. if (EXT4_I(inode)->i_extra_isize == 0)
  896. return 0;
  897. raw_inode = ext4_raw_inode(&is->iloc);
  898. header = IHDR(inode, raw_inode);
  899. is->s.base = is->s.first = IFIRST(header);
  900. is->s.here = is->s.first;
  901. is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  902. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  903. error = ext4_xattr_check_names(IFIRST(header), is->s.end,
  904. IFIRST(header));
  905. if (error)
  906. return error;
  907. /* Find the named attribute. */
  908. error = ext4_xattr_find_entry(&is->s.here, i->name_index,
  909. i->name, is->s.end -
  910. (void *)is->s.base, 0);
  911. if (error && error != -ENODATA)
  912. return error;
  913. is->s.not_found = error;
  914. }
  915. return 0;
  916. }
  917. int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
  918. struct ext4_xattr_info *i,
  919. struct ext4_xattr_ibody_find *is)
  920. {
  921. struct ext4_xattr_ibody_header *header;
  922. struct ext4_xattr_search *s = &is->s;
  923. int error;
  924. if (EXT4_I(inode)->i_extra_isize == 0)
  925. return -ENOSPC;
  926. error = ext4_xattr_set_entry(i, s);
  927. if (error) {
  928. if (error == -ENOSPC &&
  929. ext4_has_inline_data(inode)) {
  930. error = ext4_try_to_evict_inline_data(handle, inode,
  931. EXT4_XATTR_LEN(strlen(i->name) +
  932. EXT4_XATTR_SIZE(i->value_len)));
  933. if (error)
  934. return error;
  935. error = ext4_xattr_ibody_find(inode, i, is);
  936. if (error)
  937. return error;
  938. error = ext4_xattr_set_entry(i, s);
  939. }
  940. if (error)
  941. return error;
  942. }
  943. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  944. if (!IS_LAST_ENTRY(s->first)) {
  945. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  946. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  947. } else {
  948. header->h_magic = cpu_to_le32(0);
  949. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  950. }
  951. return 0;
  952. }
  953. static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
  954. struct ext4_xattr_info *i,
  955. struct ext4_xattr_ibody_find *is)
  956. {
  957. struct ext4_xattr_ibody_header *header;
  958. struct ext4_xattr_search *s = &is->s;
  959. int error;
  960. if (EXT4_I(inode)->i_extra_isize == 0)
  961. return -ENOSPC;
  962. error = ext4_xattr_set_entry(i, s);
  963. if (error)
  964. return error;
  965. header = IHDR(inode, ext4_raw_inode(&is->iloc));
  966. if (!IS_LAST_ENTRY(s->first)) {
  967. header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
  968. ext4_set_inode_state(inode, EXT4_STATE_XATTR);
  969. } else {
  970. header->h_magic = cpu_to_le32(0);
  971. ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
  972. }
  973. return 0;
  974. }
  975. /*
  976. * ext4_xattr_set_handle()
  977. *
  978. * Create, replace or remove an extended attribute for this inode. Value
  979. * is NULL to remove an existing extended attribute, and non-NULL to
  980. * either replace an existing extended attribute, or create a new extended
  981. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  982. * specify that an extended attribute must exist and must not exist
  983. * previous to the call, respectively.
  984. *
  985. * Returns 0, or a negative error number on failure.
  986. */
  987. int
  988. ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
  989. const char *name, const void *value, size_t value_len,
  990. int flags)
  991. {
  992. struct ext4_xattr_info i = {
  993. .name_index = name_index,
  994. .name = name,
  995. .value = value,
  996. .value_len = value_len,
  997. };
  998. struct ext4_xattr_ibody_find is = {
  999. .s = { .not_found = -ENODATA, },
  1000. };
  1001. struct ext4_xattr_block_find bs = {
  1002. .s = { .not_found = -ENODATA, },
  1003. };
  1004. unsigned long no_expand;
  1005. int error;
  1006. if (!name)
  1007. return -EINVAL;
  1008. if (strlen(name) > 255)
  1009. return -ERANGE;
  1010. down_write(&EXT4_I(inode)->xattr_sem);
  1011. no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1012. ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1013. error = ext4_reserve_inode_write(handle, inode, &is.iloc);
  1014. if (error)
  1015. goto cleanup;
  1016. if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
  1017. struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
  1018. memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
  1019. ext4_clear_inode_state(inode, EXT4_STATE_NEW);
  1020. }
  1021. error = ext4_xattr_ibody_find(inode, &i, &is);
  1022. if (error)
  1023. goto cleanup;
  1024. if (is.s.not_found)
  1025. error = ext4_xattr_block_find(inode, &i, &bs);
  1026. if (error)
  1027. goto cleanup;
  1028. if (is.s.not_found && bs.s.not_found) {
  1029. error = -ENODATA;
  1030. if (flags & XATTR_REPLACE)
  1031. goto cleanup;
  1032. error = 0;
  1033. if (!value)
  1034. goto cleanup;
  1035. } else {
  1036. error = -EEXIST;
  1037. if (flags & XATTR_CREATE)
  1038. goto cleanup;
  1039. }
  1040. if (!value) {
  1041. if (!is.s.not_found)
  1042. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1043. else if (!bs.s.not_found)
  1044. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1045. } else {
  1046. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1047. if (!error && !bs.s.not_found) {
  1048. i.value = NULL;
  1049. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1050. } else if (error == -ENOSPC) {
  1051. if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
  1052. error = ext4_xattr_block_find(inode, &i, &bs);
  1053. if (error)
  1054. goto cleanup;
  1055. }
  1056. error = ext4_xattr_block_set(handle, inode, &i, &bs);
  1057. if (error)
  1058. goto cleanup;
  1059. if (!is.s.not_found) {
  1060. i.value = NULL;
  1061. error = ext4_xattr_ibody_set(handle, inode, &i,
  1062. &is);
  1063. }
  1064. }
  1065. }
  1066. if (!error) {
  1067. ext4_xattr_update_super_block(handle, inode->i_sb);
  1068. inode->i_ctime = ext4_current_time(inode);
  1069. if (!value)
  1070. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1071. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  1072. /*
  1073. * The bh is consumed by ext4_mark_iloc_dirty, even with
  1074. * error != 0.
  1075. */
  1076. is.iloc.bh = NULL;
  1077. if (IS_SYNC(inode))
  1078. ext4_handle_sync(handle);
  1079. }
  1080. cleanup:
  1081. brelse(is.iloc.bh);
  1082. brelse(bs.bh);
  1083. if (no_expand == 0)
  1084. ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
  1085. up_write(&EXT4_I(inode)->xattr_sem);
  1086. return error;
  1087. }
  1088. /*
  1089. * ext4_xattr_set()
  1090. *
  1091. * Like ext4_xattr_set_handle, but start from an inode. This extended
  1092. * attribute modification is a filesystem transaction by itself.
  1093. *
  1094. * Returns 0, or a negative error number on failure.
  1095. */
  1096. int
  1097. ext4_xattr_set(struct inode *inode, int name_index, const char *name,
  1098. const void *value, size_t value_len, int flags)
  1099. {
  1100. handle_t *handle;
  1101. int error, retries = 0;
  1102. int credits = ext4_jbd2_credits_xattr(inode);
  1103. retry:
  1104. handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
  1105. if (IS_ERR(handle)) {
  1106. error = PTR_ERR(handle);
  1107. } else {
  1108. int error2;
  1109. error = ext4_xattr_set_handle(handle, inode, name_index, name,
  1110. value, value_len, flags);
  1111. error2 = ext4_journal_stop(handle);
  1112. if (error == -ENOSPC &&
  1113. ext4_should_retry_alloc(inode->i_sb, &retries))
  1114. goto retry;
  1115. if (error == 0)
  1116. error = error2;
  1117. }
  1118. return error;
  1119. }
  1120. /*
  1121. * Shift the EA entries in the inode to create space for the increased
  1122. * i_extra_isize.
  1123. */
  1124. static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
  1125. int value_offs_shift, void *to,
  1126. void *from, size_t n, int blocksize)
  1127. {
  1128. struct ext4_xattr_entry *last = entry;
  1129. int new_offs;
  1130. /* Adjust the value offsets of the entries */
  1131. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1132. if (!last->e_value_block && last->e_value_size) {
  1133. new_offs = le16_to_cpu(last->e_value_offs) +
  1134. value_offs_shift;
  1135. BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
  1136. > blocksize);
  1137. last->e_value_offs = cpu_to_le16(new_offs);
  1138. }
  1139. }
  1140. /* Shift the entries by n bytes */
  1141. memmove(to, from, n);
  1142. }
  1143. /*
  1144. * Expand an inode by new_extra_isize bytes when EAs are present.
  1145. * Returns 0 on success or negative error number on failure.
  1146. */
  1147. int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
  1148. struct ext4_inode *raw_inode, handle_t *handle)
  1149. {
  1150. struct ext4_xattr_ibody_header *header;
  1151. struct ext4_xattr_entry *entry, *last, *first;
  1152. struct buffer_head *bh = NULL;
  1153. struct ext4_xattr_ibody_find *is = NULL;
  1154. struct ext4_xattr_block_find *bs = NULL;
  1155. char *buffer = NULL, *b_entry_name = NULL;
  1156. size_t min_offs, free;
  1157. int total_ino;
  1158. void *base, *start, *end;
  1159. int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
  1160. int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
  1161. down_write(&EXT4_I(inode)->xattr_sem);
  1162. retry:
  1163. if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
  1164. up_write(&EXT4_I(inode)->xattr_sem);
  1165. return 0;
  1166. }
  1167. header = IHDR(inode, raw_inode);
  1168. entry = IFIRST(header);
  1169. /*
  1170. * Check if enough free space is available in the inode to shift the
  1171. * entries ahead by new_extra_isize.
  1172. */
  1173. base = start = entry;
  1174. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  1175. min_offs = end - base;
  1176. last = entry;
  1177. total_ino = sizeof(struct ext4_xattr_ibody_header);
  1178. free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
  1179. if (free >= new_extra_isize) {
  1180. entry = IFIRST(header);
  1181. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
  1182. - new_extra_isize, (void *)raw_inode +
  1183. EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
  1184. (void *)header, total_ino,
  1185. inode->i_sb->s_blocksize);
  1186. EXT4_I(inode)->i_extra_isize = new_extra_isize;
  1187. error = 0;
  1188. goto cleanup;
  1189. }
  1190. /*
  1191. * Enough free space isn't available in the inode, check if
  1192. * EA block can hold new_extra_isize bytes.
  1193. */
  1194. if (EXT4_I(inode)->i_file_acl) {
  1195. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1196. error = -EIO;
  1197. if (!bh)
  1198. goto cleanup;
  1199. if (ext4_xattr_check_block(inode, bh)) {
  1200. EXT4_ERROR_INODE(inode, "bad block %llu",
  1201. EXT4_I(inode)->i_file_acl);
  1202. error = -EIO;
  1203. goto cleanup;
  1204. }
  1205. base = BHDR(bh);
  1206. first = BFIRST(bh);
  1207. end = bh->b_data + bh->b_size;
  1208. min_offs = end - base;
  1209. free = ext4_xattr_free_space(first, &min_offs, base, NULL);
  1210. if (free < new_extra_isize) {
  1211. if (!tried_min_extra_isize && s_min_extra_isize) {
  1212. tried_min_extra_isize++;
  1213. new_extra_isize = s_min_extra_isize;
  1214. brelse(bh);
  1215. goto retry;
  1216. }
  1217. error = -1;
  1218. goto cleanup;
  1219. }
  1220. } else {
  1221. free = inode->i_sb->s_blocksize;
  1222. }
  1223. while (new_extra_isize > 0) {
  1224. size_t offs, size, entry_size;
  1225. struct ext4_xattr_entry *small_entry = NULL;
  1226. struct ext4_xattr_info i = {
  1227. .value = NULL,
  1228. .value_len = 0,
  1229. };
  1230. unsigned int total_size; /* EA entry size + value size */
  1231. unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
  1232. unsigned int min_total_size = ~0U;
  1233. is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
  1234. bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
  1235. if (!is || !bs) {
  1236. error = -ENOMEM;
  1237. goto cleanup;
  1238. }
  1239. is->s.not_found = -ENODATA;
  1240. bs->s.not_found = -ENODATA;
  1241. is->iloc.bh = NULL;
  1242. bs->bh = NULL;
  1243. last = IFIRST(header);
  1244. /* Find the entry best suited to be pushed into EA block */
  1245. entry = NULL;
  1246. for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
  1247. total_size =
  1248. EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
  1249. EXT4_XATTR_LEN(last->e_name_len);
  1250. if (total_size <= free && total_size < min_total_size) {
  1251. if (total_size < new_extra_isize) {
  1252. small_entry = last;
  1253. } else {
  1254. entry = last;
  1255. min_total_size = total_size;
  1256. }
  1257. }
  1258. }
  1259. if (entry == NULL) {
  1260. if (small_entry) {
  1261. entry = small_entry;
  1262. } else {
  1263. if (!tried_min_extra_isize &&
  1264. s_min_extra_isize) {
  1265. tried_min_extra_isize++;
  1266. new_extra_isize = s_min_extra_isize;
  1267. kfree(is); is = NULL;
  1268. kfree(bs); bs = NULL;
  1269. brelse(bh);
  1270. goto retry;
  1271. }
  1272. error = -1;
  1273. goto cleanup;
  1274. }
  1275. }
  1276. offs = le16_to_cpu(entry->e_value_offs);
  1277. size = le32_to_cpu(entry->e_value_size);
  1278. entry_size = EXT4_XATTR_LEN(entry->e_name_len);
  1279. i.name_index = entry->e_name_index,
  1280. buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
  1281. b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
  1282. if (!buffer || !b_entry_name) {
  1283. error = -ENOMEM;
  1284. goto cleanup;
  1285. }
  1286. /* Save the entry name and the entry value */
  1287. memcpy(buffer, (void *)IFIRST(header) + offs,
  1288. EXT4_XATTR_SIZE(size));
  1289. memcpy(b_entry_name, entry->e_name, entry->e_name_len);
  1290. b_entry_name[entry->e_name_len] = '\0';
  1291. i.name = b_entry_name;
  1292. error = ext4_get_inode_loc(inode, &is->iloc);
  1293. if (error)
  1294. goto cleanup;
  1295. error = ext4_xattr_ibody_find(inode, &i, is);
  1296. if (error)
  1297. goto cleanup;
  1298. /* Remove the chosen entry from the inode */
  1299. error = ext4_xattr_ibody_set(handle, inode, &i, is);
  1300. if (error)
  1301. goto cleanup;
  1302. entry = IFIRST(header);
  1303. if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
  1304. shift_bytes = new_extra_isize;
  1305. else
  1306. shift_bytes = entry_size + size;
  1307. /* Adjust the offsets and shift the remaining entries ahead */
  1308. ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
  1309. shift_bytes, (void *)raw_inode +
  1310. EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
  1311. (void *)header, total_ino - entry_size,
  1312. inode->i_sb->s_blocksize);
  1313. extra_isize += shift_bytes;
  1314. new_extra_isize -= shift_bytes;
  1315. EXT4_I(inode)->i_extra_isize = extra_isize;
  1316. i.name = b_entry_name;
  1317. i.value = buffer;
  1318. i.value_len = size;
  1319. error = ext4_xattr_block_find(inode, &i, bs);
  1320. if (error)
  1321. goto cleanup;
  1322. /* Add entry which was removed from the inode into the block */
  1323. error = ext4_xattr_block_set(handle, inode, &i, bs);
  1324. if (error)
  1325. goto cleanup;
  1326. kfree(b_entry_name);
  1327. kfree(buffer);
  1328. b_entry_name = NULL;
  1329. buffer = NULL;
  1330. brelse(is->iloc.bh);
  1331. kfree(is);
  1332. kfree(bs);
  1333. }
  1334. brelse(bh);
  1335. up_write(&EXT4_I(inode)->xattr_sem);
  1336. return 0;
  1337. cleanup:
  1338. kfree(b_entry_name);
  1339. kfree(buffer);
  1340. if (is)
  1341. brelse(is->iloc.bh);
  1342. kfree(is);
  1343. kfree(bs);
  1344. brelse(bh);
  1345. up_write(&EXT4_I(inode)->xattr_sem);
  1346. return error;
  1347. }
  1348. /*
  1349. * ext4_xattr_delete_inode()
  1350. *
  1351. * Free extended attribute resources associated with this inode. This
  1352. * is called immediately before an inode is freed. We have exclusive
  1353. * access to the inode.
  1354. */
  1355. void
  1356. ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
  1357. {
  1358. struct buffer_head *bh = NULL;
  1359. if (!EXT4_I(inode)->i_file_acl)
  1360. goto cleanup;
  1361. bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
  1362. if (!bh) {
  1363. EXT4_ERROR_INODE(inode, "block %llu read error",
  1364. EXT4_I(inode)->i_file_acl);
  1365. goto cleanup;
  1366. }
  1367. if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
  1368. BHDR(bh)->h_blocks != cpu_to_le32(1)) {
  1369. EXT4_ERROR_INODE(inode, "bad block %llu",
  1370. EXT4_I(inode)->i_file_acl);
  1371. goto cleanup;
  1372. }
  1373. ext4_xattr_release_block(handle, inode, bh);
  1374. EXT4_I(inode)->i_file_acl = 0;
  1375. cleanup:
  1376. brelse(bh);
  1377. }
  1378. /*
  1379. * ext4_xattr_put_super()
  1380. *
  1381. * This is called when a file system is unmounted.
  1382. */
  1383. void
  1384. ext4_xattr_put_super(struct super_block *sb)
  1385. {
  1386. mb_cache_shrink(sb->s_bdev);
  1387. }
  1388. /*
  1389. * ext4_xattr_cache_insert()
  1390. *
  1391. * Create a new entry in the extended attribute cache, and insert
  1392. * it unless such an entry is already in the cache.
  1393. *
  1394. * Returns 0, or a negative error number on failure.
  1395. */
  1396. static void
  1397. ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
  1398. {
  1399. __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
  1400. struct mb_cache_entry *ce;
  1401. int error;
  1402. ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
  1403. if (!ce) {
  1404. ea_bdebug(bh, "out of memory");
  1405. return;
  1406. }
  1407. error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
  1408. if (error) {
  1409. mb_cache_entry_free(ce);
  1410. if (error == -EBUSY) {
  1411. ea_bdebug(bh, "already in cache");
  1412. error = 0;
  1413. }
  1414. } else {
  1415. ea_bdebug(bh, "inserting [%x]", (int)hash);
  1416. mb_cache_entry_release(ce);
  1417. }
  1418. }
  1419. /*
  1420. * ext4_xattr_cmp()
  1421. *
  1422. * Compare two extended attribute blocks for equality.
  1423. *
  1424. * Returns 0 if the blocks are equal, 1 if they differ, and
  1425. * a negative error number on errors.
  1426. */
  1427. static int
  1428. ext4_xattr_cmp(struct ext4_xattr_header *header1,
  1429. struct ext4_xattr_header *header2)
  1430. {
  1431. struct ext4_xattr_entry *entry1, *entry2;
  1432. entry1 = ENTRY(header1+1);
  1433. entry2 = ENTRY(header2+1);
  1434. while (!IS_LAST_ENTRY(entry1)) {
  1435. if (IS_LAST_ENTRY(entry2))
  1436. return 1;
  1437. if (entry1->e_hash != entry2->e_hash ||
  1438. entry1->e_name_index != entry2->e_name_index ||
  1439. entry1->e_name_len != entry2->e_name_len ||
  1440. entry1->e_value_size != entry2->e_value_size ||
  1441. memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
  1442. return 1;
  1443. if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
  1444. return -EIO;
  1445. if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
  1446. (char *)header2 + le16_to_cpu(entry2->e_value_offs),
  1447. le32_to_cpu(entry1->e_value_size)))
  1448. return 1;
  1449. entry1 = EXT4_XATTR_NEXT(entry1);
  1450. entry2 = EXT4_XATTR_NEXT(entry2);
  1451. }
  1452. if (!IS_LAST_ENTRY(entry2))
  1453. return 1;
  1454. return 0;
  1455. }
  1456. /*
  1457. * ext4_xattr_cache_find()
  1458. *
  1459. * Find an identical extended attribute block.
  1460. *
  1461. * Returns a pointer to the block found, or NULL if such a block was
  1462. * not found or an error occurred.
  1463. */
  1464. static struct buffer_head *
  1465. ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
  1466. struct mb_cache_entry **pce)
  1467. {
  1468. __u32 hash = le32_to_cpu(header->h_hash);
  1469. struct mb_cache_entry *ce;
  1470. struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
  1471. if (!header->h_hash)
  1472. return NULL; /* never share */
  1473. ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
  1474. again:
  1475. ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
  1476. hash);
  1477. while (ce) {
  1478. struct buffer_head *bh;
  1479. if (IS_ERR(ce)) {
  1480. if (PTR_ERR(ce) == -EAGAIN)
  1481. goto again;
  1482. break;
  1483. }
  1484. bh = sb_bread(inode->i_sb, ce->e_block);
  1485. if (!bh) {
  1486. EXT4_ERROR_INODE(inode, "block %lu read error",
  1487. (unsigned long) ce->e_block);
  1488. } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
  1489. EXT4_XATTR_REFCOUNT_MAX) {
  1490. ea_idebug(inode, "block %lu refcount %d>=%d",
  1491. (unsigned long) ce->e_block,
  1492. le32_to_cpu(BHDR(bh)->h_refcount),
  1493. EXT4_XATTR_REFCOUNT_MAX);
  1494. } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
  1495. *pce = ce;
  1496. return bh;
  1497. }
  1498. brelse(bh);
  1499. ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
  1500. }
  1501. return NULL;
  1502. }
  1503. #define NAME_HASH_SHIFT 5
  1504. #define VALUE_HASH_SHIFT 16
  1505. /*
  1506. * ext4_xattr_hash_entry()
  1507. *
  1508. * Compute the hash of an extended attribute.
  1509. */
  1510. static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
  1511. struct ext4_xattr_entry *entry)
  1512. {
  1513. __u32 hash = 0;
  1514. char *name = entry->e_name;
  1515. int n;
  1516. for (n = 0; n < entry->e_name_len; n++) {
  1517. hash = (hash << NAME_HASH_SHIFT) ^
  1518. (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
  1519. *name++;
  1520. }
  1521. if (entry->e_value_block == 0 && entry->e_value_size != 0) {
  1522. __le32 *value = (__le32 *)((char *)header +
  1523. le16_to_cpu(entry->e_value_offs));
  1524. for (n = (le32_to_cpu(entry->e_value_size) +
  1525. EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
  1526. hash = (hash << VALUE_HASH_SHIFT) ^
  1527. (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
  1528. le32_to_cpu(*value++);
  1529. }
  1530. }
  1531. entry->e_hash = cpu_to_le32(hash);
  1532. }
  1533. #undef NAME_HASH_SHIFT
  1534. #undef VALUE_HASH_SHIFT
  1535. #define BLOCK_HASH_SHIFT 16
  1536. /*
  1537. * ext4_xattr_rehash()
  1538. *
  1539. * Re-compute the extended attribute hash value after an entry has changed.
  1540. */
  1541. static void ext4_xattr_rehash(struct ext4_xattr_header *header,
  1542. struct ext4_xattr_entry *entry)
  1543. {
  1544. struct ext4_xattr_entry *here;
  1545. __u32 hash = 0;
  1546. ext4_xattr_hash_entry(header, entry);
  1547. here = ENTRY(header+1);
  1548. while (!IS_LAST_ENTRY(here)) {
  1549. if (!here->e_hash) {
  1550. /* Block is not shared if an entry's hash value == 0 */
  1551. hash = 0;
  1552. break;
  1553. }
  1554. hash = (hash << BLOCK_HASH_SHIFT) ^
  1555. (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
  1556. le32_to_cpu(here->e_hash);
  1557. here = EXT4_XATTR_NEXT(here);
  1558. }
  1559. header->h_hash = cpu_to_le32(hash);
  1560. }
  1561. #undef BLOCK_HASH_SHIFT
  1562. #define HASH_BUCKET_BITS 10
  1563. struct mb_cache *
  1564. ext4_xattr_create_cache(char *name)
  1565. {
  1566. return mb_cache_create(name, HASH_BUCKET_BITS);
  1567. }
  1568. void ext4_xattr_destroy_cache(struct mb_cache *cache)
  1569. {
  1570. if (cache)
  1571. mb_cache_destroy(cache);
  1572. }