xattr.c 44 KB

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