xattr.c 43 KB

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