xattr.c 35 KB

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