xattr.c 46 KB

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