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