xattr.c 44 KB

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