xfs_ioctl.c 34 KB

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