xattr.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * linux/fs/reiserfs/xattr.c
  3. *
  4. * Copyright (c) 2002 by Jeff Mahoney, <jeffm@suse.com>
  5. *
  6. */
  7. /*
  8. * In order to implement EA/ACLs in a clean, backwards compatible manner,
  9. * they are implemented as files in a "private" directory.
  10. * Each EA is in it's own file, with the directory layout like so (/ is assumed
  11. * to be relative to fs root). Inside the /.reiserfs_priv/xattrs directory,
  12. * directories named using the capital-hex form of the objectid and
  13. * generation number are used. Inside each directory are individual files
  14. * named with the name of the extended attribute.
  15. *
  16. * So, for objectid 12648430, we could have:
  17. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_access
  18. * /.reiserfs_priv/xattrs/C0FFEE.0/system.posix_acl_default
  19. * /.reiserfs_priv/xattrs/C0FFEE.0/user.Content-Type
  20. * .. or similar.
  21. *
  22. * The file contents are the text of the EA. The size is known based on the
  23. * stat data describing the file.
  24. *
  25. * In the case of system.posix_acl_access and system.posix_acl_default, since
  26. * these are special cases for filesystem ACLs, they are interpreted by the
  27. * kernel, in addition, they are negatively and positively cached and attached
  28. * to the inode so that unnecessary lookups are avoided.
  29. *
  30. * Locking works like so:
  31. * Directory components (xattr root, xattr dir) are protectd by their i_mutex.
  32. * The xattrs themselves are protected by the xattr_sem.
  33. */
  34. #include "reiserfs.h"
  35. #include <linux/capability.h>
  36. #include <linux/dcache.h>
  37. #include <linux/namei.h>
  38. #include <linux/errno.h>
  39. #include <linux/gfp.h>
  40. #include <linux/fs.h>
  41. #include <linux/file.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/xattr.h>
  44. #include "xattr.h"
  45. #include "acl.h"
  46. #include <asm/uaccess.h>
  47. #include <net/checksum.h>
  48. #include <linux/stat.h>
  49. #include <linux/quotaops.h>
  50. #include <linux/security.h>
  51. #define PRIVROOT_NAME ".reiserfs_priv"
  52. #define XAROOT_NAME "xattrs"
  53. /* Helpers for inode ops. We do this so that we don't have all the VFS
  54. * overhead and also for proper i_mutex annotation.
  55. * dir->i_mutex must be held for all of them. */
  56. #ifdef CONFIG_REISERFS_FS_XATTR
  57. static int xattr_create(struct inode *dir, struct dentry *dentry, int mode)
  58. {
  59. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  60. return dir->i_op->create(dir, dentry, mode, NULL);
  61. }
  62. #endif
  63. static int xattr_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  64. {
  65. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  66. return dir->i_op->mkdir(dir, dentry, mode);
  67. }
  68. /* We use I_MUTEX_CHILD here to silence lockdep. It's safe because xattr
  69. * mutation ops aren't called during rename or splace, which are the
  70. * only other users of I_MUTEX_CHILD. It violates the ordering, but that's
  71. * better than allocating another subclass just for this code. */
  72. static int xattr_unlink(struct inode *dir, struct dentry *dentry)
  73. {
  74. int error;
  75. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  76. reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
  77. I_MUTEX_CHILD, dir->i_sb);
  78. error = dir->i_op->unlink(dir, dentry);
  79. mutex_unlock(&dentry->d_inode->i_mutex);
  80. if (!error)
  81. d_delete(dentry);
  82. return error;
  83. }
  84. static int xattr_rmdir(struct inode *dir, struct dentry *dentry)
  85. {
  86. int error;
  87. BUG_ON(!mutex_is_locked(&dir->i_mutex));
  88. reiserfs_mutex_lock_nested_safe(&dentry->d_inode->i_mutex,
  89. I_MUTEX_CHILD, dir->i_sb);
  90. error = dir->i_op->rmdir(dir, dentry);
  91. if (!error)
  92. dentry->d_inode->i_flags |= S_DEAD;
  93. mutex_unlock(&dentry->d_inode->i_mutex);
  94. if (!error)
  95. d_delete(dentry);
  96. return error;
  97. }
  98. #define xattr_may_create(flags) (!flags || flags & XATTR_CREATE)
  99. static struct dentry *open_xa_root(struct super_block *sb, int flags)
  100. {
  101. struct dentry *privroot = REISERFS_SB(sb)->priv_root;
  102. struct dentry *xaroot;
  103. if (!privroot->d_inode)
  104. return ERR_PTR(-ENODATA);
  105. mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
  106. xaroot = dget(REISERFS_SB(sb)->xattr_root);
  107. if (!xaroot)
  108. xaroot = ERR_PTR(-ENODATA);
  109. else if (!xaroot->d_inode) {
  110. int err = -ENODATA;
  111. if (xattr_may_create(flags))
  112. err = xattr_mkdir(privroot->d_inode, xaroot, 0700);
  113. if (err) {
  114. dput(xaroot);
  115. xaroot = ERR_PTR(err);
  116. }
  117. }
  118. mutex_unlock(&privroot->d_inode->i_mutex);
  119. return xaroot;
  120. }
  121. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  122. {
  123. struct dentry *xaroot, *xadir;
  124. char namebuf[17];
  125. xaroot = open_xa_root(inode->i_sb, flags);
  126. if (IS_ERR(xaroot))
  127. return xaroot;
  128. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  129. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  130. inode->i_generation);
  131. mutex_lock_nested(&xaroot->d_inode->i_mutex, I_MUTEX_XATTR);
  132. xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  133. if (!IS_ERR(xadir) && !xadir->d_inode) {
  134. int err = -ENODATA;
  135. if (xattr_may_create(flags))
  136. err = xattr_mkdir(xaroot->d_inode, xadir, 0700);
  137. if (err) {
  138. dput(xadir);
  139. xadir = ERR_PTR(err);
  140. }
  141. }
  142. mutex_unlock(&xaroot->d_inode->i_mutex);
  143. dput(xaroot);
  144. return xadir;
  145. }
  146. /* The following are side effects of other operations that aren't explicitly
  147. * modifying extended attributes. This includes operations such as permissions
  148. * or ownership changes, object deletions, etc. */
  149. struct reiserfs_dentry_buf {
  150. struct dentry *xadir;
  151. int count;
  152. struct dentry *dentries[8];
  153. };
  154. static int
  155. fill_with_dentries(void *buf, const char *name, int namelen, loff_t offset,
  156. u64 ino, unsigned int d_type)
  157. {
  158. struct reiserfs_dentry_buf *dbuf = buf;
  159. struct dentry *dentry;
  160. WARN_ON_ONCE(!mutex_is_locked(&dbuf->xadir->d_inode->i_mutex));
  161. if (dbuf->count == ARRAY_SIZE(dbuf->dentries))
  162. return -ENOSPC;
  163. if (name[0] == '.' && (namelen < 2 ||
  164. (namelen == 2 && name[1] == '.')))
  165. return 0;
  166. dentry = lookup_one_len(name, dbuf->xadir, namelen);
  167. if (IS_ERR(dentry)) {
  168. return PTR_ERR(dentry);
  169. } else if (!dentry->d_inode) {
  170. /* A directory entry exists, but no file? */
  171. reiserfs_error(dentry->d_sb, "xattr-20003",
  172. "Corrupted directory: xattr %s listed but "
  173. "not found for file %s.\n",
  174. dentry->d_name.name, dbuf->xadir->d_name.name);
  175. dput(dentry);
  176. return -EIO;
  177. }
  178. dbuf->dentries[dbuf->count++] = dentry;
  179. return 0;
  180. }
  181. static void
  182. cleanup_dentry_buf(struct reiserfs_dentry_buf *buf)
  183. {
  184. int i;
  185. for (i = 0; i < buf->count; i++)
  186. if (buf->dentries[i])
  187. dput(buf->dentries[i]);
  188. }
  189. static int reiserfs_for_each_xattr(struct inode *inode,
  190. int (*action)(struct dentry *, void *),
  191. void *data)
  192. {
  193. struct dentry *dir;
  194. int i, err = 0;
  195. loff_t pos = 0;
  196. struct reiserfs_dentry_buf buf = {
  197. .count = 0,
  198. };
  199. /* Skip out, an xattr has no xattrs associated with it */
  200. if (IS_PRIVATE(inode) || get_inode_sd_version(inode) == STAT_DATA_V1)
  201. return 0;
  202. reiserfs_write_unlock(inode->i_sb);
  203. dir = open_xa_dir(inode, XATTR_REPLACE);
  204. if (IS_ERR(dir)) {
  205. err = PTR_ERR(dir);
  206. reiserfs_write_lock(inode->i_sb);
  207. goto out;
  208. } else if (!dir->d_inode) {
  209. err = 0;
  210. reiserfs_write_lock(inode->i_sb);
  211. goto out_dir;
  212. }
  213. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  214. reiserfs_write_lock(inode->i_sb);
  215. buf.xadir = dir;
  216. err = reiserfs_readdir_dentry(dir, &buf, fill_with_dentries, &pos);
  217. while ((err == 0 || err == -ENOSPC) && buf.count) {
  218. err = 0;
  219. for (i = 0; i < buf.count && buf.dentries[i]; i++) {
  220. int lerr = 0;
  221. struct dentry *dentry = buf.dentries[i];
  222. if (err == 0 && !S_ISDIR(dentry->d_inode->i_mode))
  223. lerr = action(dentry, data);
  224. dput(dentry);
  225. buf.dentries[i] = NULL;
  226. err = lerr ?: err;
  227. }
  228. buf.count = 0;
  229. if (!err)
  230. err = reiserfs_readdir_dentry(dir, &buf,
  231. fill_with_dentries, &pos);
  232. }
  233. mutex_unlock(&dir->d_inode->i_mutex);
  234. /* Clean up after a failed readdir */
  235. cleanup_dentry_buf(&buf);
  236. if (!err) {
  237. /* We start a transaction here to avoid a ABBA situation
  238. * between the xattr root's i_mutex and the journal lock.
  239. * This doesn't incur much additional overhead since the
  240. * new transaction will just nest inside the
  241. * outer transaction. */
  242. int blocks = JOURNAL_PER_BALANCE_CNT * 2 + 2 +
  243. 4 * REISERFS_QUOTA_TRANS_BLOCKS(inode->i_sb);
  244. struct reiserfs_transaction_handle th;
  245. err = journal_begin(&th, inode->i_sb, blocks);
  246. if (!err) {
  247. int jerror;
  248. reiserfs_mutex_lock_nested_safe(
  249. &dir->d_parent->d_inode->i_mutex,
  250. I_MUTEX_XATTR, inode->i_sb);
  251. err = action(dir, data);
  252. jerror = journal_end(&th, inode->i_sb, blocks);
  253. mutex_unlock(&dir->d_parent->d_inode->i_mutex);
  254. err = jerror ?: err;
  255. }
  256. }
  257. out_dir:
  258. dput(dir);
  259. out:
  260. /* -ENODATA isn't an error */
  261. if (err == -ENODATA)
  262. err = 0;
  263. return err;
  264. }
  265. static int delete_one_xattr(struct dentry *dentry, void *data)
  266. {
  267. struct inode *dir = dentry->d_parent->d_inode;
  268. /* This is the xattr dir, handle specially. */
  269. if (S_ISDIR(dentry->d_inode->i_mode))
  270. return xattr_rmdir(dir, dentry);
  271. return xattr_unlink(dir, dentry);
  272. }
  273. static int chown_one_xattr(struct dentry *dentry, void *data)
  274. {
  275. struct iattr *attrs = data;
  276. return reiserfs_setattr(dentry, attrs);
  277. }
  278. /* No i_mutex, but the inode is unconnected. */
  279. int reiserfs_delete_xattrs(struct inode *inode)
  280. {
  281. int err = reiserfs_for_each_xattr(inode, delete_one_xattr, NULL);
  282. if (err)
  283. reiserfs_warning(inode->i_sb, "jdm-20004",
  284. "Couldn't delete all xattrs (%d)\n", err);
  285. return err;
  286. }
  287. /* inode->i_mutex: down */
  288. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  289. {
  290. int err = reiserfs_for_each_xattr(inode, chown_one_xattr, attrs);
  291. if (err)
  292. reiserfs_warning(inode->i_sb, "jdm-20007",
  293. "Couldn't chown all xattrs (%d)\n", err);
  294. return err;
  295. }
  296. #ifdef CONFIG_REISERFS_FS_XATTR
  297. /* Returns a dentry corresponding to a specific extended attribute file
  298. * for the inode. If flags allow, the file is created. Otherwise, a
  299. * valid or negative dentry, or an error is returned. */
  300. static struct dentry *xattr_lookup(struct inode *inode, const char *name,
  301. int flags)
  302. {
  303. struct dentry *xadir, *xafile;
  304. int err = 0;
  305. xadir = open_xa_dir(inode, flags);
  306. if (IS_ERR(xadir))
  307. return ERR_CAST(xadir);
  308. mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
  309. xafile = lookup_one_len(name, xadir, strlen(name));
  310. if (IS_ERR(xafile)) {
  311. err = PTR_ERR(xafile);
  312. goto out;
  313. }
  314. if (xafile->d_inode && (flags & XATTR_CREATE))
  315. err = -EEXIST;
  316. if (!xafile->d_inode) {
  317. err = -ENODATA;
  318. if (xattr_may_create(flags))
  319. err = xattr_create(xadir->d_inode, xafile,
  320. 0700|S_IFREG);
  321. }
  322. if (err)
  323. dput(xafile);
  324. out:
  325. mutex_unlock(&xadir->d_inode->i_mutex);
  326. dput(xadir);
  327. if (err)
  328. return ERR_PTR(err);
  329. return xafile;
  330. }
  331. /* Internal operations on file data */
  332. static inline void reiserfs_put_page(struct page *page)
  333. {
  334. kunmap(page);
  335. page_cache_release(page);
  336. }
  337. static struct page *reiserfs_get_page(struct inode *dir, size_t n)
  338. {
  339. struct address_space *mapping = dir->i_mapping;
  340. struct page *page;
  341. /* We can deadlock if we try to free dentries,
  342. and an unlink/rmdir has just occurred - GFP_NOFS avoids this */
  343. mapping_set_gfp_mask(mapping, GFP_NOFS);
  344. page = read_mapping_page(mapping, n >> PAGE_CACHE_SHIFT, NULL);
  345. if (!IS_ERR(page)) {
  346. kmap(page);
  347. if (PageError(page))
  348. goto fail;
  349. }
  350. return page;
  351. fail:
  352. reiserfs_put_page(page);
  353. return ERR_PTR(-EIO);
  354. }
  355. static inline __u32 xattr_hash(const char *msg, int len)
  356. {
  357. return csum_partial(msg, len, 0);
  358. }
  359. int reiserfs_commit_write(struct file *f, struct page *page,
  360. unsigned from, unsigned to);
  361. static void update_ctime(struct inode *inode)
  362. {
  363. struct timespec now = current_fs_time(inode->i_sb);
  364. if (inode_unhashed(inode) || !inode->i_nlink ||
  365. timespec_equal(&inode->i_ctime, &now))
  366. return;
  367. inode->i_ctime = CURRENT_TIME_SEC;
  368. mark_inode_dirty(inode);
  369. }
  370. static int lookup_and_delete_xattr(struct inode *inode, const char *name)
  371. {
  372. int err = 0;
  373. struct dentry *dentry, *xadir;
  374. xadir = open_xa_dir(inode, XATTR_REPLACE);
  375. if (IS_ERR(xadir))
  376. return PTR_ERR(xadir);
  377. mutex_lock_nested(&xadir->d_inode->i_mutex, I_MUTEX_XATTR);
  378. dentry = lookup_one_len(name, xadir, strlen(name));
  379. if (IS_ERR(dentry)) {
  380. err = PTR_ERR(dentry);
  381. goto out_dput;
  382. }
  383. if (dentry->d_inode) {
  384. reiserfs_write_lock(inode->i_sb);
  385. err = xattr_unlink(xadir->d_inode, dentry);
  386. reiserfs_write_unlock(inode->i_sb);
  387. update_ctime(inode);
  388. }
  389. dput(dentry);
  390. out_dput:
  391. mutex_unlock(&xadir->d_inode->i_mutex);
  392. dput(xadir);
  393. return err;
  394. }
  395. /* Generic extended attribute operations that can be used by xa plugins */
  396. /*
  397. * inode->i_mutex: down
  398. */
  399. int
  400. reiserfs_xattr_set_handle(struct reiserfs_transaction_handle *th,
  401. struct inode *inode, const char *name,
  402. const void *buffer, size_t buffer_size, int flags)
  403. {
  404. int err = 0;
  405. struct dentry *dentry;
  406. struct page *page;
  407. char *data;
  408. size_t file_pos = 0;
  409. size_t buffer_pos = 0;
  410. size_t new_size;
  411. __u32 xahash = 0;
  412. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  413. return -EOPNOTSUPP;
  414. reiserfs_write_unlock(inode->i_sb);
  415. if (!buffer) {
  416. err = lookup_and_delete_xattr(inode, name);
  417. reiserfs_write_lock(inode->i_sb);
  418. return err;
  419. }
  420. dentry = xattr_lookup(inode, name, flags);
  421. if (IS_ERR(dentry)) {
  422. reiserfs_write_lock(inode->i_sb);
  423. return PTR_ERR(dentry);
  424. }
  425. down_write(&REISERFS_I(inode)->i_xattr_sem);
  426. reiserfs_write_lock(inode->i_sb);
  427. xahash = xattr_hash(buffer, buffer_size);
  428. while (buffer_pos < buffer_size || buffer_pos == 0) {
  429. size_t chunk;
  430. size_t skip = 0;
  431. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  432. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  433. chunk = PAGE_CACHE_SIZE;
  434. else
  435. chunk = buffer_size - buffer_pos;
  436. page = reiserfs_get_page(dentry->d_inode, file_pos);
  437. if (IS_ERR(page)) {
  438. err = PTR_ERR(page);
  439. goto out_unlock;
  440. }
  441. lock_page(page);
  442. data = page_address(page);
  443. if (file_pos == 0) {
  444. struct reiserfs_xattr_header *rxh;
  445. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  446. if (chunk + skip > PAGE_CACHE_SIZE)
  447. chunk = PAGE_CACHE_SIZE - skip;
  448. rxh = (struct reiserfs_xattr_header *)data;
  449. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  450. rxh->h_hash = cpu_to_le32(xahash);
  451. }
  452. err = __reiserfs_write_begin(page, page_offset, chunk + skip);
  453. if (!err) {
  454. if (buffer)
  455. memcpy(data + skip, buffer + buffer_pos, chunk);
  456. err = reiserfs_commit_write(NULL, page, page_offset,
  457. page_offset + chunk +
  458. skip);
  459. }
  460. unlock_page(page);
  461. reiserfs_put_page(page);
  462. buffer_pos += chunk;
  463. file_pos += chunk;
  464. skip = 0;
  465. if (err || buffer_size == 0 || !buffer)
  466. break;
  467. }
  468. new_size = buffer_size + sizeof(struct reiserfs_xattr_header);
  469. if (!err && new_size < i_size_read(dentry->d_inode)) {
  470. struct iattr newattrs = {
  471. .ia_ctime = current_fs_time(inode->i_sb),
  472. .ia_size = new_size,
  473. .ia_valid = ATTR_SIZE | ATTR_CTIME,
  474. };
  475. reiserfs_write_unlock(inode->i_sb);
  476. mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_XATTR);
  477. inode_dio_wait(dentry->d_inode);
  478. reiserfs_write_lock(inode->i_sb);
  479. err = reiserfs_setattr(dentry, &newattrs);
  480. mutex_unlock(&dentry->d_inode->i_mutex);
  481. } else
  482. update_ctime(inode);
  483. out_unlock:
  484. up_write(&REISERFS_I(inode)->i_xattr_sem);
  485. dput(dentry);
  486. return err;
  487. }
  488. /* We need to start a transaction to maintain lock ordering */
  489. int reiserfs_xattr_set(struct inode *inode, const char *name,
  490. const void *buffer, size_t buffer_size, int flags)
  491. {
  492. struct reiserfs_transaction_handle th;
  493. int error, error2;
  494. size_t jbegin_count = reiserfs_xattr_nblocks(inode, buffer_size);
  495. if (!(flags & XATTR_REPLACE))
  496. jbegin_count += reiserfs_xattr_jcreate_nblocks(inode);
  497. reiserfs_write_lock(inode->i_sb);
  498. error = journal_begin(&th, inode->i_sb, jbegin_count);
  499. if (error) {
  500. reiserfs_write_unlock(inode->i_sb);
  501. return error;
  502. }
  503. error = reiserfs_xattr_set_handle(&th, inode, name,
  504. buffer, buffer_size, flags);
  505. error2 = journal_end(&th, inode->i_sb, jbegin_count);
  506. if (error == 0)
  507. error = error2;
  508. reiserfs_write_unlock(inode->i_sb);
  509. return error;
  510. }
  511. /*
  512. * inode->i_mutex: down
  513. */
  514. int
  515. reiserfs_xattr_get(struct inode *inode, const char *name, void *buffer,
  516. size_t buffer_size)
  517. {
  518. ssize_t err = 0;
  519. struct dentry *dentry;
  520. size_t isize;
  521. size_t file_pos = 0;
  522. size_t buffer_pos = 0;
  523. struct page *page;
  524. __u32 hash = 0;
  525. if (name == NULL)
  526. return -EINVAL;
  527. /* We can't have xattrs attached to v1 items since they don't have
  528. * generation numbers */
  529. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  530. return -EOPNOTSUPP;
  531. dentry = xattr_lookup(inode, name, XATTR_REPLACE);
  532. if (IS_ERR(dentry)) {
  533. err = PTR_ERR(dentry);
  534. goto out;
  535. }
  536. down_read(&REISERFS_I(inode)->i_xattr_sem);
  537. isize = i_size_read(dentry->d_inode);
  538. /* Just return the size needed */
  539. if (buffer == NULL) {
  540. err = isize - sizeof(struct reiserfs_xattr_header);
  541. goto out_unlock;
  542. }
  543. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  544. err = -ERANGE;
  545. goto out_unlock;
  546. }
  547. while (file_pos < isize) {
  548. size_t chunk;
  549. char *data;
  550. size_t skip = 0;
  551. if (isize - file_pos > PAGE_CACHE_SIZE)
  552. chunk = PAGE_CACHE_SIZE;
  553. else
  554. chunk = isize - file_pos;
  555. page = reiserfs_get_page(dentry->d_inode, file_pos);
  556. if (IS_ERR(page)) {
  557. err = PTR_ERR(page);
  558. goto out_unlock;
  559. }
  560. lock_page(page);
  561. data = page_address(page);
  562. if (file_pos == 0) {
  563. struct reiserfs_xattr_header *rxh =
  564. (struct reiserfs_xattr_header *)data;
  565. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  566. chunk -= skip;
  567. /* Magic doesn't match up.. */
  568. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  569. unlock_page(page);
  570. reiserfs_put_page(page);
  571. reiserfs_warning(inode->i_sb, "jdm-20001",
  572. "Invalid magic for xattr (%s) "
  573. "associated with %k", name,
  574. INODE_PKEY(inode));
  575. err = -EIO;
  576. goto out_unlock;
  577. }
  578. hash = le32_to_cpu(rxh->h_hash);
  579. }
  580. memcpy(buffer + buffer_pos, data + skip, chunk);
  581. unlock_page(page);
  582. reiserfs_put_page(page);
  583. file_pos += chunk;
  584. buffer_pos += chunk;
  585. skip = 0;
  586. }
  587. err = isize - sizeof(struct reiserfs_xattr_header);
  588. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  589. hash) {
  590. reiserfs_warning(inode->i_sb, "jdm-20002",
  591. "Invalid hash for xattr (%s) associated "
  592. "with %k", name, INODE_PKEY(inode));
  593. err = -EIO;
  594. }
  595. out_unlock:
  596. up_read(&REISERFS_I(inode)->i_xattr_sem);
  597. dput(dentry);
  598. out:
  599. return err;
  600. }
  601. /*
  602. * In order to implement different sets of xattr operations for each xattr
  603. * prefix with the generic xattr API, a filesystem should create a
  604. * null-terminated array of struct xattr_handler (one for each prefix) and
  605. * hang a pointer to it off of the s_xattr field of the superblock.
  606. *
  607. * The generic_fooxattr() functions will use this list to dispatch xattr
  608. * operations to the correct xattr_handler.
  609. */
  610. #define for_each_xattr_handler(handlers, handler) \
  611. for ((handler) = *(handlers)++; \
  612. (handler) != NULL; \
  613. (handler) = *(handlers)++)
  614. /* This is the implementation for the xattr plugin infrastructure */
  615. static inline const struct xattr_handler *
  616. find_xattr_handler_prefix(const struct xattr_handler **handlers,
  617. const char *name)
  618. {
  619. const struct xattr_handler *xah;
  620. if (!handlers)
  621. return NULL;
  622. for_each_xattr_handler(handlers, xah) {
  623. if (strncmp(xah->prefix, name, strlen(xah->prefix)) == 0)
  624. break;
  625. }
  626. return xah;
  627. }
  628. /*
  629. * Inode operation getxattr()
  630. */
  631. ssize_t
  632. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  633. size_t size)
  634. {
  635. const struct xattr_handler *handler;
  636. handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
  637. if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  638. return -EOPNOTSUPP;
  639. return handler->get(dentry, name, buffer, size, handler->flags);
  640. }
  641. /*
  642. * Inode operation setxattr()
  643. *
  644. * dentry->d_inode->i_mutex down
  645. */
  646. int
  647. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  648. size_t size, int flags)
  649. {
  650. const struct xattr_handler *handler;
  651. handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
  652. if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  653. return -EOPNOTSUPP;
  654. return handler->set(dentry, name, value, size, flags, handler->flags);
  655. }
  656. /*
  657. * Inode operation removexattr()
  658. *
  659. * dentry->d_inode->i_mutex down
  660. */
  661. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  662. {
  663. const struct xattr_handler *handler;
  664. handler = find_xattr_handler_prefix(dentry->d_sb->s_xattr, name);
  665. if (!handler || get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  666. return -EOPNOTSUPP;
  667. return handler->set(dentry, name, NULL, 0, XATTR_REPLACE, handler->flags);
  668. }
  669. struct listxattr_buf {
  670. size_t size;
  671. size_t pos;
  672. char *buf;
  673. struct dentry *dentry;
  674. };
  675. static int listxattr_filler(void *buf, const char *name, int namelen,
  676. loff_t offset, u64 ino, unsigned int d_type)
  677. {
  678. struct listxattr_buf *b = (struct listxattr_buf *)buf;
  679. size_t size;
  680. if (name[0] != '.' ||
  681. (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  682. const struct xattr_handler *handler;
  683. handler = find_xattr_handler_prefix(b->dentry->d_sb->s_xattr,
  684. name);
  685. if (!handler) /* Unsupported xattr name */
  686. return 0;
  687. if (b->buf) {
  688. size = handler->list(b->dentry, b->buf + b->pos,
  689. b->size, name, namelen,
  690. handler->flags);
  691. if (size > b->size)
  692. return -ERANGE;
  693. } else {
  694. size = handler->list(b->dentry, NULL, 0, name,
  695. namelen, handler->flags);
  696. }
  697. b->pos += size;
  698. }
  699. return 0;
  700. }
  701. /*
  702. * Inode operation listxattr()
  703. *
  704. * We totally ignore the generic listxattr here because it would be stupid
  705. * not to. Since the xattrs are organized in a directory, we can just
  706. * readdir to find them.
  707. */
  708. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  709. {
  710. struct dentry *dir;
  711. int err = 0;
  712. loff_t pos = 0;
  713. struct listxattr_buf buf = {
  714. .dentry = dentry,
  715. .buf = buffer,
  716. .size = buffer ? size : 0,
  717. };
  718. if (!dentry->d_inode)
  719. return -EINVAL;
  720. if (!dentry->d_sb->s_xattr ||
  721. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  722. return -EOPNOTSUPP;
  723. dir = open_xa_dir(dentry->d_inode, XATTR_REPLACE);
  724. if (IS_ERR(dir)) {
  725. err = PTR_ERR(dir);
  726. if (err == -ENODATA)
  727. err = 0; /* Not an error if there aren't any xattrs */
  728. goto out;
  729. }
  730. mutex_lock_nested(&dir->d_inode->i_mutex, I_MUTEX_XATTR);
  731. err = reiserfs_readdir_dentry(dir, &buf, listxattr_filler, &pos);
  732. mutex_unlock(&dir->d_inode->i_mutex);
  733. if (!err)
  734. err = buf.pos;
  735. dput(dir);
  736. out:
  737. return err;
  738. }
  739. static int create_privroot(struct dentry *dentry)
  740. {
  741. int err;
  742. struct inode *inode = dentry->d_parent->d_inode;
  743. WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
  744. err = xattr_mkdir(inode, dentry, 0700);
  745. if (err || !dentry->d_inode) {
  746. reiserfs_warning(dentry->d_sb, "jdm-20006",
  747. "xattrs/ACLs enabled and couldn't "
  748. "find/create .reiserfs_priv. "
  749. "Failing mount.");
  750. return -EOPNOTSUPP;
  751. }
  752. dentry->d_inode->i_flags |= S_PRIVATE;
  753. reiserfs_info(dentry->d_sb, "Created %s - reserved for xattr "
  754. "storage.\n", PRIVROOT_NAME);
  755. return 0;
  756. }
  757. #else
  758. int __init reiserfs_xattr_register_handlers(void) { return 0; }
  759. void reiserfs_xattr_unregister_handlers(void) {}
  760. static int create_privroot(struct dentry *dentry) { return 0; }
  761. #endif
  762. /* Actual operations that are exported to VFS-land */
  763. const struct xattr_handler *reiserfs_xattr_handlers[] = {
  764. #ifdef CONFIG_REISERFS_FS_XATTR
  765. &reiserfs_xattr_user_handler,
  766. &reiserfs_xattr_trusted_handler,
  767. #endif
  768. #ifdef CONFIG_REISERFS_FS_SECURITY
  769. &reiserfs_xattr_security_handler,
  770. #endif
  771. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  772. &reiserfs_posix_acl_access_handler,
  773. &reiserfs_posix_acl_default_handler,
  774. #endif
  775. NULL
  776. };
  777. static int xattr_mount_check(struct super_block *s)
  778. {
  779. /* We need generation numbers to ensure that the oid mapping is correct
  780. * v3.5 filesystems don't have them. */
  781. if (old_format_only(s)) {
  782. if (reiserfs_xattrs_optional(s)) {
  783. /* Old format filesystem, but optional xattrs have
  784. * been enabled. Error out. */
  785. reiserfs_warning(s, "jdm-2005",
  786. "xattrs/ACLs not supported "
  787. "on pre-v3.6 format filesystems. "
  788. "Failing mount.");
  789. return -EOPNOTSUPP;
  790. }
  791. }
  792. return 0;
  793. }
  794. int reiserfs_permission(struct inode *inode, int mask)
  795. {
  796. /*
  797. * We don't do permission checks on the internal objects.
  798. * Permissions are determined by the "owning" object.
  799. */
  800. if (IS_PRIVATE(inode))
  801. return 0;
  802. return generic_permission(inode, mask);
  803. }
  804. static int xattr_hide_revalidate(struct dentry *dentry, struct nameidata *nd)
  805. {
  806. return -EPERM;
  807. }
  808. static const struct dentry_operations xattr_lookup_poison_ops = {
  809. .d_revalidate = xattr_hide_revalidate,
  810. };
  811. int reiserfs_lookup_privroot(struct super_block *s)
  812. {
  813. struct dentry *dentry;
  814. int err = 0;
  815. /* If we don't have the privroot located yet - go find it */
  816. reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
  817. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  818. strlen(PRIVROOT_NAME));
  819. if (!IS_ERR(dentry)) {
  820. REISERFS_SB(s)->priv_root = dentry;
  821. d_set_d_op(dentry, &xattr_lookup_poison_ops);
  822. if (dentry->d_inode)
  823. dentry->d_inode->i_flags |= S_PRIVATE;
  824. } else
  825. err = PTR_ERR(dentry);
  826. mutex_unlock(&s->s_root->d_inode->i_mutex);
  827. return err;
  828. }
  829. /* We need to take a copy of the mount flags since things like
  830. * MS_RDONLY don't get set until *after* we're called.
  831. * mount_flags != mount_options */
  832. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  833. {
  834. int err = 0;
  835. struct dentry *privroot = REISERFS_SB(s)->priv_root;
  836. err = xattr_mount_check(s);
  837. if (err)
  838. goto error;
  839. if (!privroot->d_inode && !(mount_flags & MS_RDONLY)) {
  840. reiserfs_mutex_lock_safe(&s->s_root->d_inode->i_mutex, s);
  841. err = create_privroot(REISERFS_SB(s)->priv_root);
  842. mutex_unlock(&s->s_root->d_inode->i_mutex);
  843. }
  844. if (privroot->d_inode) {
  845. s->s_xattr = reiserfs_xattr_handlers;
  846. reiserfs_mutex_lock_safe(&privroot->d_inode->i_mutex, s);
  847. if (!REISERFS_SB(s)->xattr_root) {
  848. struct dentry *dentry;
  849. dentry = lookup_one_len(XAROOT_NAME, privroot,
  850. strlen(XAROOT_NAME));
  851. if (!IS_ERR(dentry))
  852. REISERFS_SB(s)->xattr_root = dentry;
  853. else
  854. err = PTR_ERR(dentry);
  855. }
  856. mutex_unlock(&privroot->d_inode->i_mutex);
  857. }
  858. error:
  859. if (err) {
  860. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  861. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  862. }
  863. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  864. if (reiserfs_posixacl(s))
  865. s->s_flags |= MS_POSIXACL;
  866. else
  867. s->s_flags &= ~MS_POSIXACL;
  868. return err;
  869. }