xfs_ioctl.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_ioctl.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_ialloc.h"
  40. #include "xfs_rtalloc.h"
  41. #include "xfs_itable.h"
  42. #include "xfs_error.h"
  43. #include "xfs_rw.h"
  44. #include "xfs_attr.h"
  45. #include "xfs_bmap.h"
  46. #include "xfs_buf_item.h"
  47. #include "xfs_utils.h"
  48. #include "xfs_dfrag.h"
  49. #include "xfs_fsops.h"
  50. #include "xfs_vnodeops.h"
  51. #include "xfs_quota.h"
  52. #include "xfs_inode_item.h"
  53. #include "xfs_export.h"
  54. #include "xfs_trace.h"
  55. #include <linux/capability.h>
  56. #include <linux/dcache.h>
  57. #include <linux/mount.h>
  58. #include <linux/namei.h>
  59. #include <linux/pagemap.h>
  60. #include <linux/slab.h>
  61. #include <linux/exportfs.h>
  62. /*
  63. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  64. * a file or fs handle.
  65. *
  66. * XFS_IOC_PATH_TO_FSHANDLE
  67. * returns fs handle for a mount point or path within that mount point
  68. * XFS_IOC_FD_TO_HANDLE
  69. * returns full handle for a FD opened in user space
  70. * XFS_IOC_PATH_TO_HANDLE
  71. * returns full handle for a path
  72. */
  73. int
  74. xfs_find_handle(
  75. unsigned int cmd,
  76. xfs_fsop_handlereq_t *hreq)
  77. {
  78. int hsize;
  79. xfs_handle_t handle;
  80. struct inode *inode;
  81. struct file *file = NULL;
  82. struct path path;
  83. int error;
  84. struct xfs_inode *ip;
  85. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  86. file = fget(hreq->fd);
  87. if (!file)
  88. return -EBADF;
  89. inode = file->f_path.dentry->d_inode;
  90. } else {
  91. error = user_lpath((const char __user *)hreq->path, &path);
  92. if (error)
  93. return error;
  94. inode = path.dentry->d_inode;
  95. }
  96. ip = XFS_I(inode);
  97. /*
  98. * We can only generate handles for inodes residing on a XFS filesystem,
  99. * and only for regular files, directories or symbolic links.
  100. */
  101. error = -EINVAL;
  102. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  103. goto out_put;
  104. error = -EBADF;
  105. if (!S_ISREG(inode->i_mode) &&
  106. !S_ISDIR(inode->i_mode) &&
  107. !S_ISLNK(inode->i_mode))
  108. goto out_put;
  109. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  110. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  111. /*
  112. * This handle only contains an fsid, zero the rest.
  113. */
  114. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  115. hsize = sizeof(xfs_fsid_t);
  116. } else {
  117. int lock_mode;
  118. lock_mode = xfs_ilock_map_shared(ip);
  119. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  120. sizeof(handle.ha_fid.fid_len);
  121. handle.ha_fid.fid_pad = 0;
  122. handle.ha_fid.fid_gen = ip->i_d.di_gen;
  123. handle.ha_fid.fid_ino = ip->i_ino;
  124. xfs_iunlock_map_shared(ip, lock_mode);
  125. hsize = XFS_HSIZE(handle);
  126. }
  127. error = -EFAULT;
  128. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  129. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  130. goto out_put;
  131. error = 0;
  132. out_put:
  133. if (cmd == XFS_IOC_FD_TO_HANDLE)
  134. fput(file);
  135. else
  136. path_put(&path);
  137. return error;
  138. }
  139. /*
  140. * No need to do permission checks on the various pathname components
  141. * as the handle operations are privileged.
  142. */
  143. STATIC int
  144. xfs_handle_acceptable(
  145. void *context,
  146. struct dentry *dentry)
  147. {
  148. return 1;
  149. }
  150. /*
  151. * Convert userspace handle data into a dentry.
  152. */
  153. struct dentry *
  154. xfs_handle_to_dentry(
  155. struct file *parfilp,
  156. void __user *uhandle,
  157. u32 hlen)
  158. {
  159. xfs_handle_t handle;
  160. struct xfs_fid64 fid;
  161. /*
  162. * Only allow handle opens under a directory.
  163. */
  164. if (!S_ISDIR(parfilp->f_path.dentry->d_inode->i_mode))
  165. return ERR_PTR(-ENOTDIR);
  166. if (hlen != sizeof(xfs_handle_t))
  167. return ERR_PTR(-EINVAL);
  168. if (copy_from_user(&handle, uhandle, hlen))
  169. return ERR_PTR(-EFAULT);
  170. if (handle.ha_fid.fid_len !=
  171. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  172. return ERR_PTR(-EINVAL);
  173. memset(&fid, 0, sizeof(struct fid));
  174. fid.ino = handle.ha_fid.fid_ino;
  175. fid.gen = handle.ha_fid.fid_gen;
  176. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  177. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  178. xfs_handle_acceptable, NULL);
  179. }
  180. STATIC struct dentry *
  181. xfs_handlereq_to_dentry(
  182. struct file *parfilp,
  183. xfs_fsop_handlereq_t *hreq)
  184. {
  185. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  186. }
  187. int
  188. xfs_open_by_handle(
  189. struct file *parfilp,
  190. xfs_fsop_handlereq_t *hreq)
  191. {
  192. const struct cred *cred = current_cred();
  193. int error;
  194. int fd;
  195. int permflag;
  196. struct file *filp;
  197. struct inode *inode;
  198. struct dentry *dentry;
  199. if (!capable(CAP_SYS_ADMIN))
  200. return -XFS_ERROR(EPERM);
  201. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  202. if (IS_ERR(dentry))
  203. return PTR_ERR(dentry);
  204. inode = dentry->d_inode;
  205. /* Restrict xfs_open_by_handle to directories & regular files. */
  206. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  207. error = -XFS_ERROR(EPERM);
  208. goto out_dput;
  209. }
  210. #if BITS_PER_LONG != 32
  211. hreq->oflags |= O_LARGEFILE;
  212. #endif
  213. /* Put open permission in namei format. */
  214. permflag = hreq->oflags;
  215. if ((permflag+1) & O_ACCMODE)
  216. permflag++;
  217. if (permflag & O_TRUNC)
  218. permflag |= 2;
  219. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  220. (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
  221. error = -XFS_ERROR(EPERM);
  222. goto out_dput;
  223. }
  224. if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  225. error = -XFS_ERROR(EACCES);
  226. goto out_dput;
  227. }
  228. /* Can't write directories. */
  229. if (S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
  230. error = -XFS_ERROR(EISDIR);
  231. goto out_dput;
  232. }
  233. fd = get_unused_fd();
  234. if (fd < 0) {
  235. error = fd;
  236. goto out_dput;
  237. }
  238. filp = dentry_open(dentry, mntget(parfilp->f_path.mnt),
  239. hreq->oflags, cred);
  240. if (IS_ERR(filp)) {
  241. put_unused_fd(fd);
  242. return PTR_ERR(filp);
  243. }
  244. if (inode->i_mode & S_IFREG) {
  245. filp->f_flags |= O_NOATIME;
  246. filp->f_mode |= FMODE_NOCMTIME;
  247. }
  248. fd_install(fd, filp);
  249. return fd;
  250. out_dput:
  251. dput(dentry);
  252. return error;
  253. }
  254. /*
  255. * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
  256. * unused first argument.
  257. */
  258. STATIC int
  259. do_readlink(
  260. char __user *buffer,
  261. int buflen,
  262. const char *link)
  263. {
  264. int len;
  265. len = PTR_ERR(link);
  266. if (IS_ERR(link))
  267. goto out;
  268. len = strlen(link);
  269. if (len > (unsigned) buflen)
  270. len = buflen;
  271. if (copy_to_user(buffer, link, len))
  272. len = -EFAULT;
  273. out:
  274. return len;
  275. }
  276. int
  277. xfs_readlink_by_handle(
  278. struct file *parfilp,
  279. xfs_fsop_handlereq_t *hreq)
  280. {
  281. struct dentry *dentry;
  282. __u32 olen;
  283. void *link;
  284. int error;
  285. if (!capable(CAP_SYS_ADMIN))
  286. return -XFS_ERROR(EPERM);
  287. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  288. if (IS_ERR(dentry))
  289. return PTR_ERR(dentry);
  290. /* Restrict this handle operation to symlinks only. */
  291. if (!S_ISLNK(dentry->d_inode->i_mode)) {
  292. error = -XFS_ERROR(EINVAL);
  293. goto out_dput;
  294. }
  295. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  296. error = -XFS_ERROR(EFAULT);
  297. goto out_dput;
  298. }
  299. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  300. if (!link) {
  301. error = -XFS_ERROR(ENOMEM);
  302. goto out_dput;
  303. }
  304. error = -xfs_readlink(XFS_I(dentry->d_inode), link);
  305. if (error)
  306. goto out_kfree;
  307. error = do_readlink(hreq->ohandle, olen, link);
  308. if (error)
  309. goto out_kfree;
  310. out_kfree:
  311. kfree(link);
  312. out_dput:
  313. dput(dentry);
  314. return error;
  315. }
  316. STATIC int
  317. xfs_fssetdm_by_handle(
  318. struct file *parfilp,
  319. void __user *arg)
  320. {
  321. int error;
  322. struct fsdmidata fsd;
  323. xfs_fsop_setdm_handlereq_t dmhreq;
  324. struct dentry *dentry;
  325. if (!capable(CAP_MKNOD))
  326. return -XFS_ERROR(EPERM);
  327. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  328. return -XFS_ERROR(EFAULT);
  329. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  330. if (IS_ERR(dentry))
  331. return PTR_ERR(dentry);
  332. if (IS_IMMUTABLE(dentry->d_inode) || IS_APPEND(dentry->d_inode)) {
  333. error = -XFS_ERROR(EPERM);
  334. goto out;
  335. }
  336. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  337. error = -XFS_ERROR(EFAULT);
  338. goto out;
  339. }
  340. error = -xfs_set_dmattrs(XFS_I(dentry->d_inode), fsd.fsd_dmevmask,
  341. fsd.fsd_dmstate);
  342. out:
  343. dput(dentry);
  344. return error;
  345. }
  346. STATIC int
  347. xfs_attrlist_by_handle(
  348. struct file *parfilp,
  349. void __user *arg)
  350. {
  351. int error = -ENOMEM;
  352. attrlist_cursor_kern_t *cursor;
  353. xfs_fsop_attrlist_handlereq_t al_hreq;
  354. struct dentry *dentry;
  355. char *kbuf;
  356. if (!capable(CAP_SYS_ADMIN))
  357. return -XFS_ERROR(EPERM);
  358. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  359. return -XFS_ERROR(EFAULT);
  360. if (al_hreq.buflen > XATTR_LIST_MAX)
  361. return -XFS_ERROR(EINVAL);
  362. /*
  363. * Reject flags, only allow namespaces.
  364. */
  365. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  366. return -XFS_ERROR(EINVAL);
  367. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  368. if (IS_ERR(dentry))
  369. return PTR_ERR(dentry);
  370. kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
  371. if (!kbuf)
  372. goto out_dput;
  373. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  374. error = -xfs_attr_list(XFS_I(dentry->d_inode), kbuf, al_hreq.buflen,
  375. al_hreq.flags, cursor);
  376. if (error)
  377. goto out_kfree;
  378. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  379. error = -EFAULT;
  380. out_kfree:
  381. kfree(kbuf);
  382. out_dput:
  383. dput(dentry);
  384. return error;
  385. }
  386. int
  387. xfs_attrmulti_attr_get(
  388. struct inode *inode,
  389. unsigned char *name,
  390. unsigned char __user *ubuf,
  391. __uint32_t *len,
  392. __uint32_t flags)
  393. {
  394. unsigned char *kbuf;
  395. int error = EFAULT;
  396. if (*len > XATTR_SIZE_MAX)
  397. return EINVAL;
  398. kbuf = kmalloc(*len, GFP_KERNEL);
  399. if (!kbuf)
  400. return ENOMEM;
  401. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  402. if (error)
  403. goto out_kfree;
  404. if (copy_to_user(ubuf, kbuf, *len))
  405. error = EFAULT;
  406. out_kfree:
  407. kfree(kbuf);
  408. return error;
  409. }
  410. int
  411. xfs_attrmulti_attr_set(
  412. struct inode *inode,
  413. unsigned char *name,
  414. const unsigned char __user *ubuf,
  415. __uint32_t len,
  416. __uint32_t flags)
  417. {
  418. unsigned char *kbuf;
  419. int error = EFAULT;
  420. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  421. return EPERM;
  422. if (len > XATTR_SIZE_MAX)
  423. return EINVAL;
  424. kbuf = memdup_user(ubuf, len);
  425. if (IS_ERR(kbuf))
  426. return PTR_ERR(kbuf);
  427. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  428. return error;
  429. }
  430. int
  431. xfs_attrmulti_attr_remove(
  432. struct inode *inode,
  433. unsigned char *name,
  434. __uint32_t flags)
  435. {
  436. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  437. return EPERM;
  438. return xfs_attr_remove(XFS_I(inode), name, flags);
  439. }
  440. STATIC int
  441. xfs_attrmulti_by_handle(
  442. struct file *parfilp,
  443. void __user *arg)
  444. {
  445. int error;
  446. xfs_attr_multiop_t *ops;
  447. xfs_fsop_attrmulti_handlereq_t am_hreq;
  448. struct dentry *dentry;
  449. unsigned int i, size;
  450. unsigned char *attr_name;
  451. if (!capable(CAP_SYS_ADMIN))
  452. return -XFS_ERROR(EPERM);
  453. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  454. return -XFS_ERROR(EFAULT);
  455. /* overflow check */
  456. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  457. return -E2BIG;
  458. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  459. if (IS_ERR(dentry))
  460. return PTR_ERR(dentry);
  461. error = E2BIG;
  462. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  463. if (!size || size > 16 * PAGE_SIZE)
  464. goto out_dput;
  465. ops = memdup_user(am_hreq.ops, size);
  466. if (IS_ERR(ops)) {
  467. error = PTR_ERR(ops);
  468. goto out_dput;
  469. }
  470. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  471. if (!attr_name)
  472. goto out_kfree_ops;
  473. error = 0;
  474. for (i = 0; i < am_hreq.opcount; i++) {
  475. ops[i].am_error = strncpy_from_user((char *)attr_name,
  476. ops[i].am_attrname, MAXNAMELEN);
  477. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  478. error = -ERANGE;
  479. if (ops[i].am_error < 0)
  480. break;
  481. switch (ops[i].am_opcode) {
  482. case ATTR_OP_GET:
  483. ops[i].am_error = xfs_attrmulti_attr_get(
  484. dentry->d_inode, attr_name,
  485. ops[i].am_attrvalue, &ops[i].am_length,
  486. ops[i].am_flags);
  487. break;
  488. case ATTR_OP_SET:
  489. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  490. if (ops[i].am_error)
  491. break;
  492. ops[i].am_error = xfs_attrmulti_attr_set(
  493. dentry->d_inode, attr_name,
  494. ops[i].am_attrvalue, ops[i].am_length,
  495. ops[i].am_flags);
  496. mnt_drop_write(parfilp->f_path.mnt);
  497. break;
  498. case ATTR_OP_REMOVE:
  499. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  500. if (ops[i].am_error)
  501. break;
  502. ops[i].am_error = xfs_attrmulti_attr_remove(
  503. dentry->d_inode, attr_name,
  504. ops[i].am_flags);
  505. mnt_drop_write(parfilp->f_path.mnt);
  506. break;
  507. default:
  508. ops[i].am_error = EINVAL;
  509. }
  510. }
  511. if (copy_to_user(am_hreq.ops, ops, size))
  512. error = XFS_ERROR(EFAULT);
  513. kfree(attr_name);
  514. out_kfree_ops:
  515. kfree(ops);
  516. out_dput:
  517. dput(dentry);
  518. return -error;
  519. }
  520. int
  521. xfs_ioc_space(
  522. struct xfs_inode *ip,
  523. struct inode *inode,
  524. struct file *filp,
  525. int ioflags,
  526. unsigned int cmd,
  527. xfs_flock64_t *bf)
  528. {
  529. int attr_flags = 0;
  530. int error;
  531. /*
  532. * Only allow the sys admin to reserve space unless
  533. * unwritten extents are enabled.
  534. */
  535. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  536. !capable(CAP_SYS_ADMIN))
  537. return -XFS_ERROR(EPERM);
  538. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  539. return -XFS_ERROR(EPERM);
  540. if (!(filp->f_mode & FMODE_WRITE))
  541. return -XFS_ERROR(EBADF);
  542. if (!S_ISREG(inode->i_mode))
  543. return -XFS_ERROR(EINVAL);
  544. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  545. attr_flags |= XFS_ATTR_NONBLOCK;
  546. if (ioflags & IO_INVIS)
  547. attr_flags |= XFS_ATTR_DMI;
  548. error = xfs_change_file_space(ip, cmd, bf, filp->f_pos, attr_flags);
  549. return -error;
  550. }
  551. STATIC int
  552. xfs_ioc_bulkstat(
  553. xfs_mount_t *mp,
  554. unsigned int cmd,
  555. void __user *arg)
  556. {
  557. xfs_fsop_bulkreq_t bulkreq;
  558. int count; /* # of records returned */
  559. xfs_ino_t inlast; /* last inode number */
  560. int done;
  561. int error;
  562. /* done = 1 if there are more stats to get and if bulkstat */
  563. /* should be called again (unused here, but used in dmapi) */
  564. if (!capable(CAP_SYS_ADMIN))
  565. return -EPERM;
  566. if (XFS_FORCED_SHUTDOWN(mp))
  567. return -XFS_ERROR(EIO);
  568. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  569. return -XFS_ERROR(EFAULT);
  570. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  571. return -XFS_ERROR(EFAULT);
  572. if ((count = bulkreq.icount) <= 0)
  573. return -XFS_ERROR(EINVAL);
  574. if (bulkreq.ubuffer == NULL)
  575. return -XFS_ERROR(EINVAL);
  576. if (cmd == XFS_IOC_FSINUMBERS)
  577. error = xfs_inumbers(mp, &inlast, &count,
  578. bulkreq.ubuffer, xfs_inumbers_fmt);
  579. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  580. error = xfs_bulkstat_single(mp, &inlast,
  581. bulkreq.ubuffer, &done);
  582. else /* XFS_IOC_FSBULKSTAT */
  583. error = xfs_bulkstat(mp, &inlast, &count,
  584. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  585. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  586. BULKSTAT_FG_QUICK, &done);
  587. if (error)
  588. return -error;
  589. if (bulkreq.ocount != NULL) {
  590. if (copy_to_user(bulkreq.lastip, &inlast,
  591. sizeof(xfs_ino_t)))
  592. return -XFS_ERROR(EFAULT);
  593. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  594. return -XFS_ERROR(EFAULT);
  595. }
  596. return 0;
  597. }
  598. STATIC int
  599. xfs_ioc_fsgeometry_v1(
  600. xfs_mount_t *mp,
  601. void __user *arg)
  602. {
  603. xfs_fsop_geom_v1_t fsgeo;
  604. int error;
  605. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  606. if (error)
  607. return -error;
  608. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  609. return -XFS_ERROR(EFAULT);
  610. return 0;
  611. }
  612. STATIC int
  613. xfs_ioc_fsgeometry(
  614. xfs_mount_t *mp,
  615. void __user *arg)
  616. {
  617. xfs_fsop_geom_t fsgeo;
  618. int error;
  619. error = xfs_fs_geometry(mp, &fsgeo, 4);
  620. if (error)
  621. return -error;
  622. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  623. return -XFS_ERROR(EFAULT);
  624. return 0;
  625. }
  626. /*
  627. * Linux extended inode flags interface.
  628. */
  629. STATIC unsigned int
  630. xfs_merge_ioc_xflags(
  631. unsigned int flags,
  632. unsigned int start)
  633. {
  634. unsigned int xflags = start;
  635. if (flags & FS_IMMUTABLE_FL)
  636. xflags |= XFS_XFLAG_IMMUTABLE;
  637. else
  638. xflags &= ~XFS_XFLAG_IMMUTABLE;
  639. if (flags & FS_APPEND_FL)
  640. xflags |= XFS_XFLAG_APPEND;
  641. else
  642. xflags &= ~XFS_XFLAG_APPEND;
  643. if (flags & FS_SYNC_FL)
  644. xflags |= XFS_XFLAG_SYNC;
  645. else
  646. xflags &= ~XFS_XFLAG_SYNC;
  647. if (flags & FS_NOATIME_FL)
  648. xflags |= XFS_XFLAG_NOATIME;
  649. else
  650. xflags &= ~XFS_XFLAG_NOATIME;
  651. if (flags & FS_NODUMP_FL)
  652. xflags |= XFS_XFLAG_NODUMP;
  653. else
  654. xflags &= ~XFS_XFLAG_NODUMP;
  655. return xflags;
  656. }
  657. STATIC unsigned int
  658. xfs_di2lxflags(
  659. __uint16_t di_flags)
  660. {
  661. unsigned int flags = 0;
  662. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  663. flags |= FS_IMMUTABLE_FL;
  664. if (di_flags & XFS_DIFLAG_APPEND)
  665. flags |= FS_APPEND_FL;
  666. if (di_flags & XFS_DIFLAG_SYNC)
  667. flags |= FS_SYNC_FL;
  668. if (di_flags & XFS_DIFLAG_NOATIME)
  669. flags |= FS_NOATIME_FL;
  670. if (di_flags & XFS_DIFLAG_NODUMP)
  671. flags |= FS_NODUMP_FL;
  672. return flags;
  673. }
  674. STATIC int
  675. xfs_ioc_fsgetxattr(
  676. xfs_inode_t *ip,
  677. int attr,
  678. void __user *arg)
  679. {
  680. struct fsxattr fa;
  681. xfs_ilock(ip, XFS_ILOCK_SHARED);
  682. fa.fsx_xflags = xfs_ip2xflags(ip);
  683. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  684. fa.fsx_projid = ip->i_d.di_projid;
  685. if (attr) {
  686. if (ip->i_afp) {
  687. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  688. fa.fsx_nextents = ip->i_afp->if_bytes /
  689. sizeof(xfs_bmbt_rec_t);
  690. else
  691. fa.fsx_nextents = ip->i_d.di_anextents;
  692. } else
  693. fa.fsx_nextents = 0;
  694. } else {
  695. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  696. fa.fsx_nextents = ip->i_df.if_bytes /
  697. sizeof(xfs_bmbt_rec_t);
  698. else
  699. fa.fsx_nextents = ip->i_d.di_nextents;
  700. }
  701. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  702. if (copy_to_user(arg, &fa, sizeof(fa)))
  703. return -EFAULT;
  704. return 0;
  705. }
  706. STATIC void
  707. xfs_set_diflags(
  708. struct xfs_inode *ip,
  709. unsigned int xflags)
  710. {
  711. unsigned int di_flags;
  712. /* can't set PREALLOC this way, just preserve it */
  713. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  714. if (xflags & XFS_XFLAG_IMMUTABLE)
  715. di_flags |= XFS_DIFLAG_IMMUTABLE;
  716. if (xflags & XFS_XFLAG_APPEND)
  717. di_flags |= XFS_DIFLAG_APPEND;
  718. if (xflags & XFS_XFLAG_SYNC)
  719. di_flags |= XFS_DIFLAG_SYNC;
  720. if (xflags & XFS_XFLAG_NOATIME)
  721. di_flags |= XFS_DIFLAG_NOATIME;
  722. if (xflags & XFS_XFLAG_NODUMP)
  723. di_flags |= XFS_DIFLAG_NODUMP;
  724. if (xflags & XFS_XFLAG_PROJINHERIT)
  725. di_flags |= XFS_DIFLAG_PROJINHERIT;
  726. if (xflags & XFS_XFLAG_NODEFRAG)
  727. di_flags |= XFS_DIFLAG_NODEFRAG;
  728. if (xflags & XFS_XFLAG_FILESTREAM)
  729. di_flags |= XFS_DIFLAG_FILESTREAM;
  730. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  731. if (xflags & XFS_XFLAG_RTINHERIT)
  732. di_flags |= XFS_DIFLAG_RTINHERIT;
  733. if (xflags & XFS_XFLAG_NOSYMLINKS)
  734. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  735. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  736. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  737. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  738. if (xflags & XFS_XFLAG_REALTIME)
  739. di_flags |= XFS_DIFLAG_REALTIME;
  740. if (xflags & XFS_XFLAG_EXTSIZE)
  741. di_flags |= XFS_DIFLAG_EXTSIZE;
  742. }
  743. ip->i_d.di_flags = di_flags;
  744. }
  745. STATIC void
  746. xfs_diflags_to_linux(
  747. struct xfs_inode *ip)
  748. {
  749. struct inode *inode = VFS_I(ip);
  750. unsigned int xflags = xfs_ip2xflags(ip);
  751. if (xflags & XFS_XFLAG_IMMUTABLE)
  752. inode->i_flags |= S_IMMUTABLE;
  753. else
  754. inode->i_flags &= ~S_IMMUTABLE;
  755. if (xflags & XFS_XFLAG_APPEND)
  756. inode->i_flags |= S_APPEND;
  757. else
  758. inode->i_flags &= ~S_APPEND;
  759. if (xflags & XFS_XFLAG_SYNC)
  760. inode->i_flags |= S_SYNC;
  761. else
  762. inode->i_flags &= ~S_SYNC;
  763. if (xflags & XFS_XFLAG_NOATIME)
  764. inode->i_flags |= S_NOATIME;
  765. else
  766. inode->i_flags &= ~S_NOATIME;
  767. }
  768. #define FSX_PROJID 1
  769. #define FSX_EXTSIZE 2
  770. #define FSX_XFLAGS 4
  771. #define FSX_NONBLOCK 8
  772. STATIC int
  773. xfs_ioctl_setattr(
  774. xfs_inode_t *ip,
  775. struct fsxattr *fa,
  776. int mask)
  777. {
  778. struct xfs_mount *mp = ip->i_mount;
  779. struct xfs_trans *tp;
  780. unsigned int lock_flags = 0;
  781. struct xfs_dquot *udqp = NULL;
  782. struct xfs_dquot *gdqp = NULL;
  783. struct xfs_dquot *olddquot = NULL;
  784. int code;
  785. xfs_itrace_entry(ip);
  786. if (mp->m_flags & XFS_MOUNT_RDONLY)
  787. return XFS_ERROR(EROFS);
  788. if (XFS_FORCED_SHUTDOWN(mp))
  789. return XFS_ERROR(EIO);
  790. /*
  791. * If disk quotas is on, we make sure that the dquots do exist on disk,
  792. * before we start any other transactions. Trying to do this later
  793. * is messy. We don't care to take a readlock to look at the ids
  794. * in inode here, because we can't hold it across the trans_reserve.
  795. * If the IDs do change before we take the ilock, we're covered
  796. * because the i_*dquot fields will get updated anyway.
  797. */
  798. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  799. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  800. ip->i_d.di_gid, fa->fsx_projid,
  801. XFS_QMOPT_PQUOTA, &udqp, &gdqp);
  802. if (code)
  803. return code;
  804. }
  805. /*
  806. * For the other attributes, we acquire the inode lock and
  807. * first do an error checking pass.
  808. */
  809. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  810. code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  811. if (code)
  812. goto error_return;
  813. lock_flags = XFS_ILOCK_EXCL;
  814. xfs_ilock(ip, lock_flags);
  815. /*
  816. * CAP_FOWNER overrides the following restrictions:
  817. *
  818. * The user ID of the calling process must be equal
  819. * to the file owner ID, except in cases where the
  820. * CAP_FSETID capability is applicable.
  821. */
  822. if (current_fsuid() != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
  823. code = XFS_ERROR(EPERM);
  824. goto error_return;
  825. }
  826. /*
  827. * Do a quota reservation only if projid is actually going to change.
  828. */
  829. if (mask & FSX_PROJID) {
  830. if (XFS_IS_QUOTA_RUNNING(mp) &&
  831. XFS_IS_PQUOTA_ON(mp) &&
  832. ip->i_d.di_projid != fa->fsx_projid) {
  833. ASSERT(tp);
  834. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
  835. capable(CAP_FOWNER) ?
  836. XFS_QMOPT_FORCE_RES : 0);
  837. if (code) /* out of quota */
  838. goto error_return;
  839. }
  840. }
  841. if (mask & FSX_EXTSIZE) {
  842. /*
  843. * Can't change extent size if any extents are allocated.
  844. */
  845. if (ip->i_d.di_nextents &&
  846. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  847. fa->fsx_extsize)) {
  848. code = XFS_ERROR(EINVAL); /* EFBIG? */
  849. goto error_return;
  850. }
  851. /*
  852. * Extent size must be a multiple of the appropriate block
  853. * size, if set at all.
  854. */
  855. if (fa->fsx_extsize != 0) {
  856. xfs_extlen_t size;
  857. if (XFS_IS_REALTIME_INODE(ip) ||
  858. ((mask & FSX_XFLAGS) &&
  859. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  860. size = mp->m_sb.sb_rextsize <<
  861. mp->m_sb.sb_blocklog;
  862. } else {
  863. size = mp->m_sb.sb_blocksize;
  864. }
  865. if (fa->fsx_extsize % size) {
  866. code = XFS_ERROR(EINVAL);
  867. goto error_return;
  868. }
  869. }
  870. }
  871. if (mask & FSX_XFLAGS) {
  872. /*
  873. * Can't change realtime flag if any extents are allocated.
  874. */
  875. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  876. (XFS_IS_REALTIME_INODE(ip)) !=
  877. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  878. code = XFS_ERROR(EINVAL); /* EFBIG? */
  879. goto error_return;
  880. }
  881. /*
  882. * If realtime flag is set then must have realtime data.
  883. */
  884. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  885. if ((mp->m_sb.sb_rblocks == 0) ||
  886. (mp->m_sb.sb_rextsize == 0) ||
  887. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  888. code = XFS_ERROR(EINVAL);
  889. goto error_return;
  890. }
  891. }
  892. /*
  893. * Can't modify an immutable/append-only file unless
  894. * we have appropriate permission.
  895. */
  896. if ((ip->i_d.di_flags &
  897. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  898. (fa->fsx_xflags &
  899. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  900. !capable(CAP_LINUX_IMMUTABLE)) {
  901. code = XFS_ERROR(EPERM);
  902. goto error_return;
  903. }
  904. }
  905. xfs_trans_ijoin(tp, ip, lock_flags);
  906. xfs_trans_ihold(tp, ip);
  907. /*
  908. * Change file ownership. Must be the owner or privileged.
  909. */
  910. if (mask & FSX_PROJID) {
  911. /*
  912. * CAP_FSETID overrides the following restrictions:
  913. *
  914. * The set-user-ID and set-group-ID bits of a file will be
  915. * cleared upon successful return from chown()
  916. */
  917. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  918. !capable(CAP_FSETID))
  919. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  920. /*
  921. * Change the ownerships and register quota modifications
  922. * in the transaction.
  923. */
  924. if (ip->i_d.di_projid != fa->fsx_projid) {
  925. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  926. olddquot = xfs_qm_vop_chown(tp, ip,
  927. &ip->i_gdquot, gdqp);
  928. }
  929. ip->i_d.di_projid = fa->fsx_projid;
  930. /*
  931. * We may have to rev the inode as well as
  932. * the superblock version number since projids didn't
  933. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  934. */
  935. if (ip->i_d.di_version == 1)
  936. xfs_bump_ino_vers2(tp, ip);
  937. }
  938. }
  939. if (mask & FSX_EXTSIZE)
  940. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  941. if (mask & FSX_XFLAGS) {
  942. xfs_set_diflags(ip, fa->fsx_xflags);
  943. xfs_diflags_to_linux(ip);
  944. }
  945. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  946. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  947. XFS_STATS_INC(xs_ig_attrchg);
  948. /*
  949. * If this is a synchronous mount, make sure that the
  950. * transaction goes to disk before returning to the user.
  951. * This is slightly sub-optimal in that truncates require
  952. * two sync transactions instead of one for wsync filesystems.
  953. * One for the truncate and one for the timestamps since we
  954. * don't want to change the timestamps unless we're sure the
  955. * truncate worked. Truncates are less than 1% of the laddis
  956. * mix so this probably isn't worth the trouble to optimize.
  957. */
  958. if (mp->m_flags & XFS_MOUNT_WSYNC)
  959. xfs_trans_set_sync(tp);
  960. code = xfs_trans_commit(tp, 0);
  961. xfs_iunlock(ip, lock_flags);
  962. /*
  963. * Release any dquot(s) the inode had kept before chown.
  964. */
  965. xfs_qm_dqrele(olddquot);
  966. xfs_qm_dqrele(udqp);
  967. xfs_qm_dqrele(gdqp);
  968. if (code)
  969. return code;
  970. if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
  971. XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
  972. NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
  973. (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
  974. }
  975. return 0;
  976. error_return:
  977. xfs_qm_dqrele(udqp);
  978. xfs_qm_dqrele(gdqp);
  979. xfs_trans_cancel(tp, 0);
  980. if (lock_flags)
  981. xfs_iunlock(ip, lock_flags);
  982. return code;
  983. }
  984. STATIC int
  985. xfs_ioc_fssetxattr(
  986. xfs_inode_t *ip,
  987. struct file *filp,
  988. void __user *arg)
  989. {
  990. struct fsxattr fa;
  991. unsigned int mask;
  992. if (copy_from_user(&fa, arg, sizeof(fa)))
  993. return -EFAULT;
  994. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  995. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  996. mask |= FSX_NONBLOCK;
  997. return -xfs_ioctl_setattr(ip, &fa, mask);
  998. }
  999. STATIC int
  1000. xfs_ioc_getxflags(
  1001. xfs_inode_t *ip,
  1002. void __user *arg)
  1003. {
  1004. unsigned int flags;
  1005. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1006. if (copy_to_user(arg, &flags, sizeof(flags)))
  1007. return -EFAULT;
  1008. return 0;
  1009. }
  1010. STATIC int
  1011. xfs_ioc_setxflags(
  1012. xfs_inode_t *ip,
  1013. struct file *filp,
  1014. void __user *arg)
  1015. {
  1016. struct fsxattr fa;
  1017. unsigned int flags;
  1018. unsigned int mask;
  1019. if (copy_from_user(&flags, arg, sizeof(flags)))
  1020. return -EFAULT;
  1021. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1022. FS_NOATIME_FL | FS_NODUMP_FL | \
  1023. FS_SYNC_FL))
  1024. return -EOPNOTSUPP;
  1025. mask = FSX_XFLAGS;
  1026. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1027. mask |= FSX_NONBLOCK;
  1028. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1029. return -xfs_ioctl_setattr(ip, &fa, mask);
  1030. }
  1031. STATIC int
  1032. xfs_getbmap_format(void **ap, struct getbmapx *bmv, int *full)
  1033. {
  1034. struct getbmap __user *base = *ap;
  1035. /* copy only getbmap portion (not getbmapx) */
  1036. if (copy_to_user(base, bmv, sizeof(struct getbmap)))
  1037. return XFS_ERROR(EFAULT);
  1038. *ap += sizeof(struct getbmap);
  1039. return 0;
  1040. }
  1041. STATIC int
  1042. xfs_ioc_getbmap(
  1043. struct xfs_inode *ip,
  1044. int ioflags,
  1045. unsigned int cmd,
  1046. void __user *arg)
  1047. {
  1048. struct getbmapx bmx;
  1049. int error;
  1050. if (copy_from_user(&bmx, arg, sizeof(struct getbmapx)))
  1051. return -XFS_ERROR(EFAULT);
  1052. if (bmx.bmv_count < 2)
  1053. return -XFS_ERROR(EINVAL);
  1054. bmx.bmv_iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1055. if (ioflags & IO_INVIS)
  1056. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1057. error = xfs_getbmap(ip, &bmx, xfs_getbmap_format,
  1058. (struct getbmap *)arg+1);
  1059. if (error)
  1060. return -error;
  1061. /* copy back header - only size of getbmap */
  1062. if (copy_to_user(arg, &bmx, sizeof(struct getbmap)))
  1063. return -XFS_ERROR(EFAULT);
  1064. return 0;
  1065. }
  1066. STATIC int
  1067. xfs_getbmapx_format(void **ap, struct getbmapx *bmv, int *full)
  1068. {
  1069. struct getbmapx __user *base = *ap;
  1070. if (copy_to_user(base, bmv, sizeof(struct getbmapx)))
  1071. return XFS_ERROR(EFAULT);
  1072. *ap += sizeof(struct getbmapx);
  1073. return 0;
  1074. }
  1075. STATIC int
  1076. xfs_ioc_getbmapx(
  1077. struct xfs_inode *ip,
  1078. void __user *arg)
  1079. {
  1080. struct getbmapx bmx;
  1081. int error;
  1082. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1083. return -XFS_ERROR(EFAULT);
  1084. if (bmx.bmv_count < 2)
  1085. return -XFS_ERROR(EINVAL);
  1086. if (bmx.bmv_iflags & (~BMV_IF_VALID))
  1087. return -XFS_ERROR(EINVAL);
  1088. error = xfs_getbmap(ip, &bmx, xfs_getbmapx_format,
  1089. (struct getbmapx *)arg+1);
  1090. if (error)
  1091. return -error;
  1092. /* copy back header */
  1093. if (copy_to_user(arg, &bmx, sizeof(struct getbmapx)))
  1094. return -XFS_ERROR(EFAULT);
  1095. return 0;
  1096. }
  1097. /*
  1098. * Note: some of the ioctl's return positive numbers as a
  1099. * byte count indicating success, such as readlink_by_handle.
  1100. * So we don't "sign flip" like most other routines. This means
  1101. * true errors need to be returned as a negative value.
  1102. */
  1103. long
  1104. xfs_file_ioctl(
  1105. struct file *filp,
  1106. unsigned int cmd,
  1107. unsigned long p)
  1108. {
  1109. struct inode *inode = filp->f_path.dentry->d_inode;
  1110. struct xfs_inode *ip = XFS_I(inode);
  1111. struct xfs_mount *mp = ip->i_mount;
  1112. void __user *arg = (void __user *)p;
  1113. int ioflags = 0;
  1114. int error;
  1115. if (filp->f_mode & FMODE_NOCMTIME)
  1116. ioflags |= IO_INVIS;
  1117. xfs_itrace_entry(ip);
  1118. switch (cmd) {
  1119. case XFS_IOC_ALLOCSP:
  1120. case XFS_IOC_FREESP:
  1121. case XFS_IOC_RESVSP:
  1122. case XFS_IOC_UNRESVSP:
  1123. case XFS_IOC_ALLOCSP64:
  1124. case XFS_IOC_FREESP64:
  1125. case XFS_IOC_RESVSP64:
  1126. case XFS_IOC_UNRESVSP64: {
  1127. xfs_flock64_t bf;
  1128. if (copy_from_user(&bf, arg, sizeof(bf)))
  1129. return -XFS_ERROR(EFAULT);
  1130. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  1131. }
  1132. case XFS_IOC_DIOINFO: {
  1133. struct dioattr da;
  1134. xfs_buftarg_t *target =
  1135. XFS_IS_REALTIME_INODE(ip) ?
  1136. mp->m_rtdev_targp : mp->m_ddev_targp;
  1137. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1138. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1139. if (copy_to_user(arg, &da, sizeof(da)))
  1140. return -XFS_ERROR(EFAULT);
  1141. return 0;
  1142. }
  1143. case XFS_IOC_FSBULKSTAT_SINGLE:
  1144. case XFS_IOC_FSBULKSTAT:
  1145. case XFS_IOC_FSINUMBERS:
  1146. return xfs_ioc_bulkstat(mp, cmd, arg);
  1147. case XFS_IOC_FSGEOMETRY_V1:
  1148. return xfs_ioc_fsgeometry_v1(mp, arg);
  1149. case XFS_IOC_FSGEOMETRY:
  1150. return xfs_ioc_fsgeometry(mp, arg);
  1151. case XFS_IOC_GETVERSION:
  1152. return put_user(inode->i_generation, (int __user *)arg);
  1153. case XFS_IOC_FSGETXATTR:
  1154. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1155. case XFS_IOC_FSGETXATTRA:
  1156. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1157. case XFS_IOC_FSSETXATTR:
  1158. return xfs_ioc_fssetxattr(ip, filp, arg);
  1159. case XFS_IOC_GETXFLAGS:
  1160. return xfs_ioc_getxflags(ip, arg);
  1161. case XFS_IOC_SETXFLAGS:
  1162. return xfs_ioc_setxflags(ip, filp, arg);
  1163. case XFS_IOC_FSSETDM: {
  1164. struct fsdmidata dmi;
  1165. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1166. return -XFS_ERROR(EFAULT);
  1167. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1168. dmi.fsd_dmstate);
  1169. return -error;
  1170. }
  1171. case XFS_IOC_GETBMAP:
  1172. case XFS_IOC_GETBMAPA:
  1173. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1174. case XFS_IOC_GETBMAPX:
  1175. return xfs_ioc_getbmapx(ip, arg);
  1176. case XFS_IOC_FD_TO_HANDLE:
  1177. case XFS_IOC_PATH_TO_HANDLE:
  1178. case XFS_IOC_PATH_TO_FSHANDLE: {
  1179. xfs_fsop_handlereq_t hreq;
  1180. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1181. return -XFS_ERROR(EFAULT);
  1182. return xfs_find_handle(cmd, &hreq);
  1183. }
  1184. case XFS_IOC_OPEN_BY_HANDLE: {
  1185. xfs_fsop_handlereq_t hreq;
  1186. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1187. return -XFS_ERROR(EFAULT);
  1188. return xfs_open_by_handle(filp, &hreq);
  1189. }
  1190. case XFS_IOC_FSSETDM_BY_HANDLE:
  1191. return xfs_fssetdm_by_handle(filp, arg);
  1192. case XFS_IOC_READLINK_BY_HANDLE: {
  1193. xfs_fsop_handlereq_t hreq;
  1194. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1195. return -XFS_ERROR(EFAULT);
  1196. return xfs_readlink_by_handle(filp, &hreq);
  1197. }
  1198. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1199. return xfs_attrlist_by_handle(filp, arg);
  1200. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1201. return xfs_attrmulti_by_handle(filp, arg);
  1202. case XFS_IOC_SWAPEXT: {
  1203. struct xfs_swapext sxp;
  1204. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1205. return -XFS_ERROR(EFAULT);
  1206. error = xfs_swapext(&sxp);
  1207. return -error;
  1208. }
  1209. case XFS_IOC_FSCOUNTS: {
  1210. xfs_fsop_counts_t out;
  1211. error = xfs_fs_counts(mp, &out);
  1212. if (error)
  1213. return -error;
  1214. if (copy_to_user(arg, &out, sizeof(out)))
  1215. return -XFS_ERROR(EFAULT);
  1216. return 0;
  1217. }
  1218. case XFS_IOC_SET_RESBLKS: {
  1219. xfs_fsop_resblks_t inout;
  1220. __uint64_t in;
  1221. if (!capable(CAP_SYS_ADMIN))
  1222. return -EPERM;
  1223. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1224. return -XFS_ERROR(EROFS);
  1225. if (copy_from_user(&inout, arg, sizeof(inout)))
  1226. return -XFS_ERROR(EFAULT);
  1227. /* input parameter is passed in resblks field of structure */
  1228. in = inout.resblks;
  1229. error = xfs_reserve_blocks(mp, &in, &inout);
  1230. if (error)
  1231. return -error;
  1232. if (copy_to_user(arg, &inout, sizeof(inout)))
  1233. return -XFS_ERROR(EFAULT);
  1234. return 0;
  1235. }
  1236. case XFS_IOC_GET_RESBLKS: {
  1237. xfs_fsop_resblks_t out;
  1238. if (!capable(CAP_SYS_ADMIN))
  1239. return -EPERM;
  1240. error = xfs_reserve_blocks(mp, NULL, &out);
  1241. if (error)
  1242. return -error;
  1243. if (copy_to_user(arg, &out, sizeof(out)))
  1244. return -XFS_ERROR(EFAULT);
  1245. return 0;
  1246. }
  1247. case XFS_IOC_FSGROWFSDATA: {
  1248. xfs_growfs_data_t in;
  1249. if (copy_from_user(&in, arg, sizeof(in)))
  1250. return -XFS_ERROR(EFAULT);
  1251. error = xfs_growfs_data(mp, &in);
  1252. return -error;
  1253. }
  1254. case XFS_IOC_FSGROWFSLOG: {
  1255. xfs_growfs_log_t in;
  1256. if (copy_from_user(&in, arg, sizeof(in)))
  1257. return -XFS_ERROR(EFAULT);
  1258. error = xfs_growfs_log(mp, &in);
  1259. return -error;
  1260. }
  1261. case XFS_IOC_FSGROWFSRT: {
  1262. xfs_growfs_rt_t in;
  1263. if (copy_from_user(&in, arg, sizeof(in)))
  1264. return -XFS_ERROR(EFAULT);
  1265. error = xfs_growfs_rt(mp, &in);
  1266. return -error;
  1267. }
  1268. case XFS_IOC_GOINGDOWN: {
  1269. __uint32_t in;
  1270. if (!capable(CAP_SYS_ADMIN))
  1271. return -EPERM;
  1272. if (get_user(in, (__uint32_t __user *)arg))
  1273. return -XFS_ERROR(EFAULT);
  1274. error = xfs_fs_goingdown(mp, in);
  1275. return -error;
  1276. }
  1277. case XFS_IOC_ERROR_INJECTION: {
  1278. xfs_error_injection_t in;
  1279. if (!capable(CAP_SYS_ADMIN))
  1280. return -EPERM;
  1281. if (copy_from_user(&in, arg, sizeof(in)))
  1282. return -XFS_ERROR(EFAULT);
  1283. error = xfs_errortag_add(in.errtag, mp);
  1284. return -error;
  1285. }
  1286. case XFS_IOC_ERROR_CLEARALL:
  1287. if (!capable(CAP_SYS_ADMIN))
  1288. return -EPERM;
  1289. error = xfs_errortag_clearall(mp, 1);
  1290. return -error;
  1291. default:
  1292. return -ENOTTY;
  1293. }
  1294. }