xfs_ioctl.c 34 KB

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