xattr.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. #include <linux/reiserfs_fs.h>
  31. #include <linux/capability.h>
  32. #include <linux/dcache.h>
  33. #include <linux/namei.h>
  34. #include <linux/errno.h>
  35. #include <linux/fs.h>
  36. #include <linux/file.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/xattr.h>
  39. #include <linux/reiserfs_xattr.h>
  40. #include <linux/reiserfs_acl.h>
  41. #include <asm/uaccess.h>
  42. #include <net/checksum.h>
  43. #include <linux/smp_lock.h>
  44. #include <linux/stat.h>
  45. #include <asm/semaphore.h>
  46. #define FL_READONLY 128
  47. #define FL_DIR_SEM_HELD 256
  48. #define PRIVROOT_NAME ".reiserfs_priv"
  49. #define XAROOT_NAME "xattrs"
  50. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  51. *prefix);
  52. /* Returns the dentry referring to the root of the extended attribute
  53. * directory tree. If it has already been retrieved, it is used. If it
  54. * hasn't been created and the flags indicate creation is allowed, we
  55. * attempt to create it. On error, we return a pointer-encoded error.
  56. */
  57. static struct dentry *get_xa_root(struct super_block *sb, int flags)
  58. {
  59. struct dentry *privroot = dget(REISERFS_SB(sb)->priv_root);
  60. struct dentry *xaroot;
  61. /* This needs to be created at mount-time */
  62. if (!privroot)
  63. return ERR_PTR(-ENODATA);
  64. mutex_lock_nested(&privroot->d_inode->i_mutex, I_MUTEX_XATTR);
  65. if (REISERFS_SB(sb)->xattr_root) {
  66. xaroot = dget(REISERFS_SB(sb)->xattr_root);
  67. goto out;
  68. }
  69. xaroot = lookup_one_len(XAROOT_NAME, privroot, strlen(XAROOT_NAME));
  70. if (IS_ERR(xaroot)) {
  71. goto out;
  72. } else if (!xaroot->d_inode) {
  73. int err = -ENODATA;
  74. if (flags == 0 || flags & XATTR_CREATE)
  75. err = privroot->d_inode->i_op->mkdir(privroot->d_inode,
  76. xaroot, 0700);
  77. if (err) {
  78. dput(xaroot);
  79. xaroot = ERR_PTR(err);
  80. goto out;
  81. }
  82. }
  83. REISERFS_SB(sb)->xattr_root = dget(xaroot);
  84. out:
  85. mutex_unlock(&privroot->d_inode->i_mutex);
  86. dput(privroot);
  87. return xaroot;
  88. }
  89. /* Opens the directory corresponding to the inode's extended attribute store.
  90. * If flags allow, the tree to the directory may be created. If creation is
  91. * prohibited, -ENODATA is returned. */
  92. static struct dentry *open_xa_dir(const struct inode *inode, int flags)
  93. {
  94. struct dentry *xaroot, *xadir;
  95. char namebuf[17];
  96. xaroot = get_xa_root(inode->i_sb, flags);
  97. if (IS_ERR(xaroot))
  98. return xaroot;
  99. /* ok, we have xaroot open */
  100. snprintf(namebuf, sizeof(namebuf), "%X.%X",
  101. le32_to_cpu(INODE_PKEY(inode)->k_objectid),
  102. inode->i_generation);
  103. xadir = lookup_one_len(namebuf, xaroot, strlen(namebuf));
  104. if (IS_ERR(xadir)) {
  105. dput(xaroot);
  106. return xadir;
  107. }
  108. if (!xadir->d_inode) {
  109. int err;
  110. if (flags == 0 || flags & XATTR_CREATE) {
  111. /* Although there is nothing else trying to create this directory,
  112. * another directory with the same hash may be created, so we need
  113. * to protect against that */
  114. err =
  115. xaroot->d_inode->i_op->mkdir(xaroot->d_inode, xadir,
  116. 0700);
  117. if (err) {
  118. dput(xaroot);
  119. dput(xadir);
  120. return ERR_PTR(err);
  121. }
  122. }
  123. if (!xadir->d_inode) {
  124. dput(xaroot);
  125. dput(xadir);
  126. return ERR_PTR(-ENODATA);
  127. }
  128. }
  129. dput(xaroot);
  130. return xadir;
  131. }
  132. /* Returns a dentry corresponding to a specific extended attribute file
  133. * for the inode. If flags allow, the file is created. Otherwise, a
  134. * valid or negative dentry, or an error is returned. */
  135. static struct dentry *get_xa_file_dentry(const struct inode *inode,
  136. const char *name, int flags)
  137. {
  138. struct dentry *xadir, *xafile;
  139. int err = 0;
  140. xadir = open_xa_dir(inode, flags);
  141. if (IS_ERR(xadir)) {
  142. return ERR_CAST(xadir);
  143. } else if (xadir && !xadir->d_inode) {
  144. dput(xadir);
  145. return ERR_PTR(-ENODATA);
  146. }
  147. xafile = lookup_one_len(name, xadir, strlen(name));
  148. if (IS_ERR(xafile)) {
  149. dput(xadir);
  150. return ERR_CAST(xafile);
  151. }
  152. if (xafile->d_inode) { /* file exists */
  153. if (flags & XATTR_CREATE) {
  154. err = -EEXIST;
  155. dput(xafile);
  156. goto out;
  157. }
  158. } else if (flags & XATTR_REPLACE || flags & FL_READONLY) {
  159. goto out;
  160. } else {
  161. /* inode->i_mutex is down, so nothing else can try to create
  162. * the same xattr */
  163. err = xadir->d_inode->i_op->create(xadir->d_inode, xafile,
  164. 0700 | S_IFREG, NULL);
  165. if (err) {
  166. dput(xafile);
  167. goto out;
  168. }
  169. }
  170. out:
  171. dput(xadir);
  172. if (err)
  173. xafile = ERR_PTR(err);
  174. else if (!xafile->d_inode) {
  175. dput(xafile);
  176. xafile = ERR_PTR(-ENODATA);
  177. }
  178. return xafile;
  179. }
  180. /*
  181. * this is very similar to fs/reiserfs/dir.c:reiserfs_readdir, but
  182. * we need to drop the path before calling the filldir struct. That
  183. * would be a big performance hit to the non-xattr case, so I've copied
  184. * the whole thing for now. --clm
  185. *
  186. * the big difference is that I go backwards through the directory,
  187. * and don't mess with f->f_pos, but the idea is the same. Do some
  188. * action on each and every entry in the directory.
  189. *
  190. * we're called with i_mutex held, so there are no worries about the directory
  191. * changing underneath us.
  192. */
  193. static int __xattr_readdir(struct inode *inode, void *dirent, filldir_t filldir)
  194. {
  195. struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
  196. INITIALIZE_PATH(path_to_entry);
  197. struct buffer_head *bh;
  198. int entry_num;
  199. struct item_head *ih, tmp_ih;
  200. int search_res;
  201. char *local_buf;
  202. loff_t next_pos;
  203. char small_buf[32]; /* avoid kmalloc if we can */
  204. struct reiserfs_de_head *deh;
  205. int d_reclen;
  206. char *d_name;
  207. off_t d_off;
  208. ino_t d_ino;
  209. struct reiserfs_dir_entry de;
  210. /* form key for search the next directory entry using f_pos field of
  211. file structure */
  212. next_pos = max_reiserfs_offset(inode);
  213. while (1) {
  214. research:
  215. if (next_pos <= DOT_DOT_OFFSET)
  216. break;
  217. make_cpu_key(&pos_key, inode, next_pos, TYPE_DIRENTRY, 3);
  218. search_res =
  219. search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
  220. &de);
  221. if (search_res == IO_ERROR) {
  222. // FIXME: we could just skip part of directory which could
  223. // not be read
  224. pathrelse(&path_to_entry);
  225. return -EIO;
  226. }
  227. if (search_res == NAME_NOT_FOUND)
  228. de.de_entry_num--;
  229. set_de_name_and_namelen(&de);
  230. entry_num = de.de_entry_num;
  231. deh = &(de.de_deh[entry_num]);
  232. bh = de.de_bh;
  233. ih = de.de_ih;
  234. if (!is_direntry_le_ih(ih)) {
  235. reiserfs_warning(inode->i_sb, "not direntry %h", ih);
  236. break;
  237. }
  238. copy_item_head(&tmp_ih, ih);
  239. /* we must have found item, that is item of this directory, */
  240. RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
  241. "vs-9000: found item %h does not match to dir we readdir %K",
  242. ih, &pos_key);
  243. if (deh_offset(deh) <= DOT_DOT_OFFSET) {
  244. break;
  245. }
  246. /* look for the previous entry in the directory */
  247. next_pos = deh_offset(deh) - 1;
  248. if (!de_visible(deh))
  249. /* it is hidden entry */
  250. continue;
  251. d_reclen = entry_length(bh, ih, entry_num);
  252. d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
  253. d_off = deh_offset(deh);
  254. d_ino = deh_objectid(deh);
  255. if (!d_name[d_reclen - 1])
  256. d_reclen = strlen(d_name);
  257. if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)) {
  258. /* too big to send back to VFS */
  259. continue;
  260. }
  261. /* Ignore the .reiserfs_priv entry */
  262. if (reiserfs_xattrs(inode->i_sb) &&
  263. !old_format_only(inode->i_sb) &&
  264. deh_objectid(deh) ==
  265. le32_to_cpu(INODE_PKEY
  266. (REISERFS_SB(inode->i_sb)->priv_root->d_inode)->
  267. k_objectid))
  268. continue;
  269. if (d_reclen <= 32) {
  270. local_buf = small_buf;
  271. } else {
  272. local_buf = kmalloc(d_reclen, GFP_NOFS);
  273. if (!local_buf) {
  274. pathrelse(&path_to_entry);
  275. return -ENOMEM;
  276. }
  277. if (item_moved(&tmp_ih, &path_to_entry)) {
  278. kfree(local_buf);
  279. /* sigh, must retry. Do this same offset again */
  280. next_pos = d_off;
  281. goto research;
  282. }
  283. }
  284. // Note, that we copy name to user space via temporary
  285. // buffer (local_buf) because filldir will block if
  286. // user space buffer is swapped out. At that time
  287. // entry can move to somewhere else
  288. memcpy(local_buf, d_name, d_reclen);
  289. /* the filldir function might need to start transactions,
  290. * or do who knows what. Release the path now that we've
  291. * copied all the important stuff out of the deh
  292. */
  293. pathrelse(&path_to_entry);
  294. if (filldir(dirent, local_buf, d_reclen, d_off, d_ino,
  295. DT_UNKNOWN) < 0) {
  296. if (local_buf != small_buf) {
  297. kfree(local_buf);
  298. }
  299. goto end;
  300. }
  301. if (local_buf != small_buf) {
  302. kfree(local_buf);
  303. }
  304. } /* while */
  305. end:
  306. pathrelse(&path_to_entry);
  307. return 0;
  308. }
  309. /*
  310. * this could be done with dedicated readdir ops for the xattr files,
  311. * but I want to get something working asap
  312. * this is stolen from vfs_readdir
  313. *
  314. */
  315. static
  316. int xattr_readdir(struct inode *inode, filldir_t filler, void *buf)
  317. {
  318. int res = -ENOENT;
  319. mutex_lock_nested(&inode->i_mutex, I_MUTEX_XATTR);
  320. if (!IS_DEADDIR(inode)) {
  321. lock_kernel();
  322. res = __xattr_readdir(inode, buf, filler);
  323. unlock_kernel();
  324. }
  325. mutex_unlock(&inode->i_mutex);
  326. return res;
  327. }
  328. /* Internal operations on file data */
  329. static inline void reiserfs_put_page(struct page *page)
  330. {
  331. kunmap(page);
  332. page_cache_release(page);
  333. }
  334. static struct page *reiserfs_get_page(struct inode *dir, unsigned long n)
  335. {
  336. struct address_space *mapping = dir->i_mapping;
  337. struct page *page;
  338. /* We can deadlock if we try to free dentries,
  339. and an unlink/rmdir has just occured - GFP_NOFS avoids this */
  340. mapping_set_gfp_mask(mapping, GFP_NOFS);
  341. page = read_mapping_page(mapping, n, NULL);
  342. if (!IS_ERR(page)) {
  343. kmap(page);
  344. if (PageError(page))
  345. goto fail;
  346. }
  347. return page;
  348. fail:
  349. reiserfs_put_page(page);
  350. return ERR_PTR(-EIO);
  351. }
  352. static inline __u32 xattr_hash(const char *msg, int len)
  353. {
  354. return csum_partial(msg, len, 0);
  355. }
  356. int reiserfs_commit_write(struct file *f, struct page *page,
  357. unsigned from, unsigned to);
  358. int reiserfs_prepare_write(struct file *f, struct page *page,
  359. unsigned from, unsigned to);
  360. /* Generic extended attribute operations that can be used by xa plugins */
  361. /*
  362. * inode->i_mutex: down
  363. */
  364. int
  365. reiserfs_xattr_set(struct inode *inode, const char *name, const void *buffer,
  366. size_t buffer_size, int flags)
  367. {
  368. int err = 0;
  369. struct dentry *dentry;
  370. struct page *page;
  371. char *data;
  372. struct address_space *mapping;
  373. size_t file_pos = 0;
  374. size_t buffer_pos = 0;
  375. struct inode *xinode;
  376. struct iattr newattrs;
  377. __u32 xahash = 0;
  378. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  379. return -EOPNOTSUPP;
  380. /* Empty xattrs are ok, they're just empty files, no hash */
  381. if (buffer && buffer_size)
  382. xahash = xattr_hash(buffer, buffer_size);
  383. open_file:
  384. dentry = get_xa_file_dentry(inode, name, flags);
  385. if (IS_ERR(dentry)) {
  386. err = PTR_ERR(dentry);
  387. goto out;
  388. }
  389. xinode = dentry->d_inode;
  390. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  391. /* we need to copy it off.. */
  392. if (xinode->i_nlink > 1) {
  393. dput(dentry);
  394. err = reiserfs_xattr_del(inode, name);
  395. if (err < 0)
  396. goto out;
  397. /* We just killed the old one, we're not replacing anymore */
  398. if (flags & XATTR_REPLACE)
  399. flags &= ~XATTR_REPLACE;
  400. goto open_file;
  401. }
  402. /* Resize it so we're ok to write there */
  403. newattrs.ia_size = buffer_size;
  404. newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
  405. mutex_lock_nested(&xinode->i_mutex, I_MUTEX_XATTR);
  406. err = notify_change(dentry, &newattrs);
  407. if (err)
  408. goto out_filp;
  409. mapping = xinode->i_mapping;
  410. while (buffer_pos < buffer_size || buffer_pos == 0) {
  411. size_t chunk;
  412. size_t skip = 0;
  413. size_t page_offset = (file_pos & (PAGE_CACHE_SIZE - 1));
  414. if (buffer_size - buffer_pos > PAGE_CACHE_SIZE)
  415. chunk = PAGE_CACHE_SIZE;
  416. else
  417. chunk = buffer_size - buffer_pos;
  418. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  419. if (IS_ERR(page)) {
  420. err = PTR_ERR(page);
  421. goto out_filp;
  422. }
  423. lock_page(page);
  424. data = page_address(page);
  425. if (file_pos == 0) {
  426. struct reiserfs_xattr_header *rxh;
  427. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  428. if (chunk + skip > PAGE_CACHE_SIZE)
  429. chunk = PAGE_CACHE_SIZE - skip;
  430. rxh = (struct reiserfs_xattr_header *)data;
  431. rxh->h_magic = cpu_to_le32(REISERFS_XATTR_MAGIC);
  432. rxh->h_hash = cpu_to_le32(xahash);
  433. }
  434. err = reiserfs_prepare_write(NULL, page, page_offset,
  435. page_offset + chunk + skip);
  436. if (!err) {
  437. if (buffer)
  438. memcpy(data + skip, buffer + buffer_pos, chunk);
  439. err = reiserfs_commit_write(NULL, page, page_offset,
  440. page_offset + chunk +
  441. skip);
  442. }
  443. unlock_page(page);
  444. reiserfs_put_page(page);
  445. buffer_pos += chunk;
  446. file_pos += chunk;
  447. skip = 0;
  448. if (err || buffer_size == 0 || !buffer)
  449. break;
  450. }
  451. /* We can't mark the inode dirty if it's not hashed. This is the case
  452. * when we're inheriting the default ACL. If we dirty it, the inode
  453. * gets marked dirty, but won't (ever) make it onto the dirty list until
  454. * it's synced explicitly to clear I_DIRTY. This is bad. */
  455. if (!hlist_unhashed(&inode->i_hash)) {
  456. inode->i_ctime = CURRENT_TIME_SEC;
  457. mark_inode_dirty(inode);
  458. }
  459. out_filp:
  460. mutex_unlock(&xinode->i_mutex);
  461. dput(dentry);
  462. out:
  463. return err;
  464. }
  465. /*
  466. * inode->i_mutex: down
  467. */
  468. int
  469. reiserfs_xattr_get(const struct inode *inode, const char *name, void *buffer,
  470. size_t buffer_size)
  471. {
  472. ssize_t err = 0;
  473. struct dentry *dentry;
  474. size_t isize;
  475. size_t file_pos = 0;
  476. size_t buffer_pos = 0;
  477. struct page *page;
  478. struct inode *xinode;
  479. __u32 hash = 0;
  480. if (name == NULL)
  481. return -EINVAL;
  482. /* We can't have xattrs attached to v1 items since they don't have
  483. * generation numbers */
  484. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  485. return -EOPNOTSUPP;
  486. dentry = get_xa_file_dentry(inode, name, FL_READONLY);
  487. if (IS_ERR(dentry)) {
  488. err = PTR_ERR(dentry);
  489. goto out;
  490. }
  491. xinode = dentry->d_inode;
  492. isize = xinode->i_size;
  493. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  494. /* Just return the size needed */
  495. if (buffer == NULL) {
  496. err = isize - sizeof(struct reiserfs_xattr_header);
  497. goto out_dput;
  498. }
  499. if (buffer_size < isize - sizeof(struct reiserfs_xattr_header)) {
  500. err = -ERANGE;
  501. goto out_dput;
  502. }
  503. while (file_pos < isize) {
  504. size_t chunk;
  505. char *data;
  506. size_t skip = 0;
  507. if (isize - file_pos > PAGE_CACHE_SIZE)
  508. chunk = PAGE_CACHE_SIZE;
  509. else
  510. chunk = isize - file_pos;
  511. page = reiserfs_get_page(xinode, file_pos >> PAGE_CACHE_SHIFT);
  512. if (IS_ERR(page)) {
  513. err = PTR_ERR(page);
  514. goto out_dput;
  515. }
  516. lock_page(page);
  517. data = page_address(page);
  518. if (file_pos == 0) {
  519. struct reiserfs_xattr_header *rxh =
  520. (struct reiserfs_xattr_header *)data;
  521. skip = file_pos = sizeof(struct reiserfs_xattr_header);
  522. chunk -= skip;
  523. /* Magic doesn't match up.. */
  524. if (rxh->h_magic != cpu_to_le32(REISERFS_XATTR_MAGIC)) {
  525. unlock_page(page);
  526. reiserfs_put_page(page);
  527. reiserfs_warning(inode->i_sb,
  528. "Invalid magic for xattr (%s) "
  529. "associated with %k", name,
  530. INODE_PKEY(inode));
  531. err = -EIO;
  532. goto out_dput;
  533. }
  534. hash = le32_to_cpu(rxh->h_hash);
  535. }
  536. memcpy(buffer + buffer_pos, data + skip, chunk);
  537. unlock_page(page);
  538. reiserfs_put_page(page);
  539. file_pos += chunk;
  540. buffer_pos += chunk;
  541. skip = 0;
  542. }
  543. err = isize - sizeof(struct reiserfs_xattr_header);
  544. if (xattr_hash(buffer, isize - sizeof(struct reiserfs_xattr_header)) !=
  545. hash) {
  546. reiserfs_warning(inode->i_sb,
  547. "Invalid hash for xattr (%s) associated "
  548. "with %k", name, INODE_PKEY(inode));
  549. err = -EIO;
  550. }
  551. out_dput:
  552. dput(dentry);
  553. out:
  554. return err;
  555. }
  556. static int
  557. __reiserfs_xattr_del(struct dentry *xadir, const char *name, int namelen)
  558. {
  559. struct dentry *dentry;
  560. struct inode *dir = xadir->d_inode;
  561. int err = 0;
  562. dentry = lookup_one_len(name, xadir, namelen);
  563. if (IS_ERR(dentry)) {
  564. err = PTR_ERR(dentry);
  565. goto out;
  566. } else if (!dentry->d_inode) {
  567. err = -ENODATA;
  568. goto out_file;
  569. }
  570. /* Skip directories.. */
  571. if (S_ISDIR(dentry->d_inode->i_mode))
  572. goto out_file;
  573. if (!is_reiserfs_priv_object(dentry->d_inode)) {
  574. reiserfs_warning(dir->i_sb, "OID %08x [%.*s/%.*s] doesn't have "
  575. "priv flag set [parent is %sset].",
  576. le32_to_cpu(INODE_PKEY(dentry->d_inode)->
  577. k_objectid), xadir->d_name.len,
  578. xadir->d_name.name, namelen, name,
  579. is_reiserfs_priv_object(xadir->
  580. d_inode) ? "" :
  581. "not ");
  582. dput(dentry);
  583. return -EIO;
  584. }
  585. err = dir->i_op->unlink(dir, dentry);
  586. if (!err)
  587. d_delete(dentry);
  588. out_file:
  589. dput(dentry);
  590. out:
  591. return err;
  592. }
  593. int reiserfs_xattr_del(struct inode *inode, const char *name)
  594. {
  595. struct dentry *dir;
  596. int err;
  597. dir = open_xa_dir(inode, FL_READONLY);
  598. if (IS_ERR(dir)) {
  599. err = PTR_ERR(dir);
  600. goto out;
  601. }
  602. err = __reiserfs_xattr_del(dir, name, strlen(name));
  603. dput(dir);
  604. if (!err) {
  605. inode->i_ctime = CURRENT_TIME_SEC;
  606. mark_inode_dirty(inode);
  607. }
  608. out:
  609. return err;
  610. }
  611. /* The following are side effects of other operations that aren't explicitly
  612. * modifying extended attributes. This includes operations such as permissions
  613. * or ownership changes, object deletions, etc. */
  614. static int
  615. reiserfs_delete_xattrs_filler(void *buf, const char *name, int namelen,
  616. loff_t offset, u64 ino, unsigned int d_type)
  617. {
  618. struct dentry *xadir = (struct dentry *)buf;
  619. return __reiserfs_xattr_del(xadir, name, namelen);
  620. }
  621. /* This is called w/ inode->i_mutex downed */
  622. int reiserfs_delete_xattrs(struct inode *inode)
  623. {
  624. struct dentry *dir, *root;
  625. int err = 0;
  626. /* Skip out, an xattr has no xattrs associated with it */
  627. if (is_reiserfs_priv_object(inode) ||
  628. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  629. !reiserfs_xattrs(inode->i_sb)) {
  630. return 0;
  631. }
  632. reiserfs_read_lock_xattrs(inode->i_sb);
  633. dir = open_xa_dir(inode, FL_READONLY);
  634. reiserfs_read_unlock_xattrs(inode->i_sb);
  635. if (IS_ERR(dir)) {
  636. err = PTR_ERR(dir);
  637. goto out;
  638. } else if (!dir->d_inode) {
  639. dput(dir);
  640. return 0;
  641. }
  642. lock_kernel();
  643. err = xattr_readdir(dir->d_inode, reiserfs_delete_xattrs_filler, dir);
  644. if (err) {
  645. unlock_kernel();
  646. goto out_dir;
  647. }
  648. /* Leftovers besides . and .. -- that's not good. */
  649. if (dir->d_inode->i_nlink <= 2) {
  650. root = get_xa_root(inode->i_sb, XATTR_REPLACE);
  651. reiserfs_write_lock_xattrs(inode->i_sb);
  652. err = vfs_rmdir(root->d_inode, dir);
  653. reiserfs_write_unlock_xattrs(inode->i_sb);
  654. dput(root);
  655. } else {
  656. reiserfs_warning(inode->i_sb,
  657. "Couldn't remove all entries in directory");
  658. }
  659. unlock_kernel();
  660. out_dir:
  661. dput(dir);
  662. out:
  663. if (!err)
  664. REISERFS_I(inode)->i_flags =
  665. REISERFS_I(inode)->i_flags & ~i_has_xattr_dir;
  666. return err;
  667. }
  668. struct reiserfs_chown_buf {
  669. struct inode *inode;
  670. struct dentry *xadir;
  671. struct iattr *attrs;
  672. };
  673. /* XXX: If there is a better way to do this, I'd love to hear about it */
  674. static int
  675. reiserfs_chown_xattrs_filler(void *buf, const char *name, int namelen,
  676. loff_t offset, u64 ino, unsigned int d_type)
  677. {
  678. struct reiserfs_chown_buf *chown_buf = (struct reiserfs_chown_buf *)buf;
  679. struct dentry *xafile, *xadir = chown_buf->xadir;
  680. struct iattr *attrs = chown_buf->attrs;
  681. int err = 0;
  682. xafile = lookup_one_len(name, xadir, namelen);
  683. if (IS_ERR(xafile))
  684. return PTR_ERR(xafile);
  685. else if (!xafile->d_inode) {
  686. dput(xafile);
  687. return -ENODATA;
  688. }
  689. if (!S_ISDIR(xafile->d_inode->i_mode))
  690. err = notify_change(xafile, attrs);
  691. dput(xafile);
  692. return err;
  693. }
  694. int reiserfs_chown_xattrs(struct inode *inode, struct iattr *attrs)
  695. {
  696. struct dentry *dir;
  697. int err = 0;
  698. struct reiserfs_chown_buf buf;
  699. unsigned int ia_valid = attrs->ia_valid;
  700. /* Skip out, an xattr has no xattrs associated with it */
  701. if (is_reiserfs_priv_object(inode) ||
  702. get_inode_sd_version(inode) == STAT_DATA_V1 ||
  703. !reiserfs_xattrs(inode->i_sb)) {
  704. return 0;
  705. }
  706. reiserfs_read_lock_xattrs(inode->i_sb);
  707. dir = open_xa_dir(inode, FL_READONLY);
  708. reiserfs_read_unlock_xattrs(inode->i_sb);
  709. if (IS_ERR(dir)) {
  710. if (PTR_ERR(dir) != -ENODATA)
  711. err = PTR_ERR(dir);
  712. goto out;
  713. } else if (!dir->d_inode) {
  714. dput(dir);
  715. goto out;
  716. }
  717. lock_kernel();
  718. attrs->ia_valid &= (ATTR_UID | ATTR_GID | ATTR_CTIME);
  719. buf.xadir = dir;
  720. buf.attrs = attrs;
  721. buf.inode = inode;
  722. err = xattr_readdir(dir->d_inode, reiserfs_chown_xattrs_filler, &buf);
  723. if (err) {
  724. unlock_kernel();
  725. goto out_dir;
  726. }
  727. err = notify_change(dir, attrs);
  728. unlock_kernel();
  729. out_dir:
  730. dput(dir);
  731. out:
  732. attrs->ia_valid = ia_valid;
  733. return err;
  734. }
  735. /* Actual operations that are exported to VFS-land */
  736. /*
  737. * Inode operation getxattr()
  738. * Preliminary locking: we down dentry->d_inode->i_mutex
  739. */
  740. ssize_t
  741. reiserfs_getxattr(struct dentry * dentry, const char *name, void *buffer,
  742. size_t size)
  743. {
  744. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  745. int err;
  746. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  747. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  748. return -EOPNOTSUPP;
  749. reiserfs_read_lock_xattr_i(dentry->d_inode);
  750. reiserfs_read_lock_xattrs(dentry->d_sb);
  751. err = xah->get(dentry->d_inode, name, buffer, size);
  752. reiserfs_read_unlock_xattrs(dentry->d_sb);
  753. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  754. return err;
  755. }
  756. /*
  757. * Inode operation setxattr()
  758. *
  759. * dentry->d_inode->i_mutex down
  760. */
  761. int
  762. reiserfs_setxattr(struct dentry *dentry, const char *name, const void *value,
  763. size_t size, int flags)
  764. {
  765. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  766. int err;
  767. int lock;
  768. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  769. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  770. return -EOPNOTSUPP;
  771. reiserfs_write_lock_xattr_i(dentry->d_inode);
  772. lock = !has_xattr_dir(dentry->d_inode);
  773. if (lock)
  774. reiserfs_write_lock_xattrs(dentry->d_sb);
  775. else
  776. reiserfs_read_lock_xattrs(dentry->d_sb);
  777. err = xah->set(dentry->d_inode, name, value, size, flags);
  778. if (lock)
  779. reiserfs_write_unlock_xattrs(dentry->d_sb);
  780. else
  781. reiserfs_read_unlock_xattrs(dentry->d_sb);
  782. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  783. return err;
  784. }
  785. /*
  786. * Inode operation removexattr()
  787. *
  788. * dentry->d_inode->i_mutex down
  789. */
  790. int reiserfs_removexattr(struct dentry *dentry, const char *name)
  791. {
  792. int err;
  793. struct reiserfs_xattr_handler *xah = find_xattr_handler_prefix(name);
  794. if (!xah || !reiserfs_xattrs(dentry->d_sb) ||
  795. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  796. return -EOPNOTSUPP;
  797. reiserfs_write_lock_xattr_i(dentry->d_inode);
  798. reiserfs_read_lock_xattrs(dentry->d_sb);
  799. /* Deletion pre-operation */
  800. if (xah->del) {
  801. err = xah->del(dentry->d_inode, name);
  802. if (err)
  803. goto out;
  804. }
  805. err = reiserfs_xattr_del(dentry->d_inode, name);
  806. dentry->d_inode->i_ctime = CURRENT_TIME_SEC;
  807. mark_inode_dirty(dentry->d_inode);
  808. out:
  809. reiserfs_read_unlock_xattrs(dentry->d_sb);
  810. reiserfs_write_unlock_xattr_i(dentry->d_inode);
  811. return err;
  812. }
  813. /* This is what filldir will use:
  814. * r_pos will always contain the amount of space required for the entire
  815. * list. If r_pos becomes larger than r_size, we need more space and we
  816. * return an error indicating this. If r_pos is less than r_size, then we've
  817. * filled the buffer successfully and we return success */
  818. struct reiserfs_listxattr_buf {
  819. int r_pos;
  820. int r_size;
  821. char *r_buf;
  822. struct inode *r_inode;
  823. };
  824. static int
  825. reiserfs_listxattr_filler(void *buf, const char *name, int namelen,
  826. loff_t offset, u64 ino, unsigned int d_type)
  827. {
  828. struct reiserfs_listxattr_buf *b = (struct reiserfs_listxattr_buf *)buf;
  829. int len = 0;
  830. if (name[0] != '.'
  831. || (namelen != 1 && (name[1] != '.' || namelen != 2))) {
  832. struct reiserfs_xattr_handler *xah =
  833. find_xattr_handler_prefix(name);
  834. if (!xah)
  835. return 0; /* Unsupported xattr name, skip it */
  836. /* We call ->list() twice because the operation isn't required to just
  837. * return the name back - we want to make sure we have enough space */
  838. len += xah->list(b->r_inode, name, namelen, NULL);
  839. if (len) {
  840. if (b->r_pos + len + 1 <= b->r_size) {
  841. char *p = b->r_buf + b->r_pos;
  842. p += xah->list(b->r_inode, name, namelen, p);
  843. *p++ = '\0';
  844. }
  845. b->r_pos += len + 1;
  846. }
  847. }
  848. return 0;
  849. }
  850. /*
  851. * Inode operation listxattr()
  852. *
  853. * Preliminary locking: we down dentry->d_inode->i_mutex
  854. */
  855. ssize_t reiserfs_listxattr(struct dentry * dentry, char *buffer, size_t size)
  856. {
  857. struct dentry *dir;
  858. int err = 0;
  859. struct reiserfs_listxattr_buf buf;
  860. if (!dentry->d_inode)
  861. return -EINVAL;
  862. if (!reiserfs_xattrs(dentry->d_sb) ||
  863. get_inode_sd_version(dentry->d_inode) == STAT_DATA_V1)
  864. return -EOPNOTSUPP;
  865. reiserfs_read_lock_xattr_i(dentry->d_inode);
  866. reiserfs_read_lock_xattrs(dentry->d_sb);
  867. dir = open_xa_dir(dentry->d_inode, FL_READONLY);
  868. reiserfs_read_unlock_xattrs(dentry->d_sb);
  869. if (IS_ERR(dir)) {
  870. err = PTR_ERR(dir);
  871. if (err == -ENODATA)
  872. err = 0; /* Not an error if there aren't any xattrs */
  873. goto out;
  874. }
  875. buf.r_buf = buffer;
  876. buf.r_size = buffer ? size : 0;
  877. buf.r_pos = 0;
  878. buf.r_inode = dentry->d_inode;
  879. REISERFS_I(dentry->d_inode)->i_flags |= i_has_xattr_dir;
  880. err = xattr_readdir(dir->d_inode, reiserfs_listxattr_filler, &buf);
  881. if (err)
  882. goto out_dir;
  883. if (buf.r_pos > buf.r_size && buffer != NULL)
  884. err = -ERANGE;
  885. else
  886. err = buf.r_pos;
  887. out_dir:
  888. dput(dir);
  889. out:
  890. reiserfs_read_unlock_xattr_i(dentry->d_inode);
  891. return err;
  892. }
  893. /* This is the implementation for the xattr plugin infrastructure */
  894. static LIST_HEAD(xattr_handlers);
  895. static DEFINE_RWLOCK(handler_lock);
  896. static struct reiserfs_xattr_handler *find_xattr_handler_prefix(const char
  897. *prefix)
  898. {
  899. struct reiserfs_xattr_handler *xah = NULL;
  900. struct list_head *p;
  901. read_lock(&handler_lock);
  902. list_for_each(p, &xattr_handlers) {
  903. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  904. if (strncmp(xah->prefix, prefix, strlen(xah->prefix)) == 0)
  905. break;
  906. xah = NULL;
  907. }
  908. read_unlock(&handler_lock);
  909. return xah;
  910. }
  911. static void __unregister_handlers(void)
  912. {
  913. struct reiserfs_xattr_handler *xah;
  914. struct list_head *p, *tmp;
  915. list_for_each_safe(p, tmp, &xattr_handlers) {
  916. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  917. if (xah->exit)
  918. xah->exit();
  919. list_del_init(p);
  920. }
  921. INIT_LIST_HEAD(&xattr_handlers);
  922. }
  923. int __init reiserfs_xattr_register_handlers(void)
  924. {
  925. int err = 0;
  926. struct reiserfs_xattr_handler *xah;
  927. struct list_head *p;
  928. write_lock(&handler_lock);
  929. /* If we're already initialized, nothing to do */
  930. if (!list_empty(&xattr_handlers)) {
  931. write_unlock(&handler_lock);
  932. return 0;
  933. }
  934. /* Add the handlers */
  935. list_add_tail(&user_handler.handlers, &xattr_handlers);
  936. list_add_tail(&trusted_handler.handlers, &xattr_handlers);
  937. #ifdef CONFIG_REISERFS_FS_SECURITY
  938. list_add_tail(&security_handler.handlers, &xattr_handlers);
  939. #endif
  940. #ifdef CONFIG_REISERFS_FS_POSIX_ACL
  941. list_add_tail(&posix_acl_access_handler.handlers, &xattr_handlers);
  942. list_add_tail(&posix_acl_default_handler.handlers, &xattr_handlers);
  943. #endif
  944. /* Run initializers, if available */
  945. list_for_each(p, &xattr_handlers) {
  946. xah = list_entry(p, struct reiserfs_xattr_handler, handlers);
  947. if (xah->init) {
  948. err = xah->init();
  949. if (err) {
  950. list_del_init(p);
  951. break;
  952. }
  953. }
  954. }
  955. /* Clean up other handlers, if any failed */
  956. if (err)
  957. __unregister_handlers();
  958. write_unlock(&handler_lock);
  959. return err;
  960. }
  961. void reiserfs_xattr_unregister_handlers(void)
  962. {
  963. write_lock(&handler_lock);
  964. __unregister_handlers();
  965. write_unlock(&handler_lock);
  966. }
  967. /* This will catch lookups from the fs root to .reiserfs_priv */
  968. static int
  969. xattr_lookup_poison(struct dentry *dentry, struct qstr *q1, struct qstr *name)
  970. {
  971. struct dentry *priv_root = REISERFS_SB(dentry->d_sb)->priv_root;
  972. if (name->len == priv_root->d_name.len &&
  973. name->hash == priv_root->d_name.hash &&
  974. !memcmp(name->name, priv_root->d_name.name, name->len)) {
  975. return -ENOENT;
  976. } else if (q1->len == name->len &&
  977. !memcmp(q1->name, name->name, name->len))
  978. return 0;
  979. return 1;
  980. }
  981. static struct dentry_operations xattr_lookup_poison_ops = {
  982. .d_compare = xattr_lookup_poison,
  983. };
  984. /* We need to take a copy of the mount flags since things like
  985. * MS_RDONLY don't get set until *after* we're called.
  986. * mount_flags != mount_options */
  987. int reiserfs_xattr_init(struct super_block *s, int mount_flags)
  988. {
  989. int err = 0;
  990. /* We need generation numbers to ensure that the oid mapping is correct
  991. * v3.5 filesystems don't have them. */
  992. if (!old_format_only(s)) {
  993. set_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  994. } else if (reiserfs_xattrs_optional(s)) {
  995. /* Old format filesystem, but optional xattrs have been enabled
  996. * at mount time. Error out. */
  997. reiserfs_warning(s, "xattrs/ACLs not supported on pre v3.6 "
  998. "format filesystem. Failing mount.");
  999. err = -EOPNOTSUPP;
  1000. goto error;
  1001. } else {
  1002. /* Old format filesystem, but no optional xattrs have been enabled. This
  1003. * means we silently disable xattrs on the filesystem. */
  1004. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1005. }
  1006. /* If we don't have the privroot located yet - go find it */
  1007. if (reiserfs_xattrs(s) && !REISERFS_SB(s)->priv_root) {
  1008. struct dentry *dentry;
  1009. dentry = lookup_one_len(PRIVROOT_NAME, s->s_root,
  1010. strlen(PRIVROOT_NAME));
  1011. if (!IS_ERR(dentry)) {
  1012. if (!(mount_flags & MS_RDONLY) && !dentry->d_inode) {
  1013. struct inode *inode = dentry->d_parent->d_inode;
  1014. mutex_lock_nested(&inode->i_mutex,
  1015. I_MUTEX_XATTR);
  1016. err = inode->i_op->mkdir(inode, dentry, 0700);
  1017. mutex_unlock(&inode->i_mutex);
  1018. if (err) {
  1019. dput(dentry);
  1020. dentry = NULL;
  1021. }
  1022. if (dentry && dentry->d_inode)
  1023. reiserfs_warning(s,
  1024. "Created %s on %s - reserved for "
  1025. "xattr storage.",
  1026. PRIVROOT_NAME,
  1027. reiserfs_bdevname
  1028. (inode->i_sb));
  1029. } else if (!dentry->d_inode) {
  1030. dput(dentry);
  1031. dentry = NULL;
  1032. }
  1033. } else
  1034. err = PTR_ERR(dentry);
  1035. if (!err && dentry) {
  1036. s->s_root->d_op = &xattr_lookup_poison_ops;
  1037. reiserfs_mark_inode_private(dentry->d_inode);
  1038. REISERFS_SB(s)->priv_root = dentry;
  1039. } else if (!(mount_flags & MS_RDONLY)) { /* xattrs are unavailable */
  1040. /* If we're read-only it just means that the dir hasn't been
  1041. * created. Not an error -- just no xattrs on the fs. We'll
  1042. * check again if we go read-write */
  1043. reiserfs_warning(s, "xattrs/ACLs enabled and couldn't "
  1044. "find/create .reiserfs_priv. Failing mount.");
  1045. err = -EOPNOTSUPP;
  1046. }
  1047. }
  1048. error:
  1049. /* This is only nonzero if there was an error initializing the xattr
  1050. * directory or if there is a condition where we don't support them. */
  1051. if (err) {
  1052. clear_bit(REISERFS_XATTRS, &(REISERFS_SB(s)->s_mount_opt));
  1053. clear_bit(REISERFS_XATTRS_USER, &(REISERFS_SB(s)->s_mount_opt));
  1054. clear_bit(REISERFS_POSIXACL, &(REISERFS_SB(s)->s_mount_opt));
  1055. }
  1056. /* The super_block MS_POSIXACL must mirror the (no)acl mount option. */
  1057. s->s_flags = s->s_flags & ~MS_POSIXACL;
  1058. if (reiserfs_posixacl(s))
  1059. s->s_flags |= MS_POSIXACL;
  1060. return err;
  1061. }
  1062. static int reiserfs_check_acl(struct inode *inode, int mask)
  1063. {
  1064. struct posix_acl *acl;
  1065. int error = -EAGAIN; /* do regular unix permission checks by default */
  1066. reiserfs_read_lock_xattr_i(inode);
  1067. reiserfs_read_lock_xattrs(inode->i_sb);
  1068. acl = reiserfs_get_acl(inode, ACL_TYPE_ACCESS);
  1069. reiserfs_read_unlock_xattrs(inode->i_sb);
  1070. reiserfs_read_unlock_xattr_i(inode);
  1071. if (acl) {
  1072. if (!IS_ERR(acl)) {
  1073. error = posix_acl_permission(inode, acl, mask);
  1074. posix_acl_release(acl);
  1075. } else if (PTR_ERR(acl) != -ENODATA)
  1076. error = PTR_ERR(acl);
  1077. }
  1078. return error;
  1079. }
  1080. int reiserfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  1081. {
  1082. /*
  1083. * We don't do permission checks on the internal objects.
  1084. * Permissions are determined by the "owning" object.
  1085. */
  1086. if (is_reiserfs_priv_object(inode))
  1087. return 0;
  1088. /*
  1089. * Stat data v1 doesn't support ACLs.
  1090. */
  1091. if (get_inode_sd_version(inode) == STAT_DATA_V1)
  1092. return generic_permission(inode, mask, NULL);
  1093. else
  1094. return generic_permission(inode, mask, reiserfs_check_acl);
  1095. }