ioctl.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/highmem.h>
  26. #include <linux/time.h>
  27. #include <linux/init.h>
  28. #include <linux/string.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mount.h>
  31. #include <linux/mpage.h>
  32. #include <linux/namei.h>
  33. #include <linux/swap.h>
  34. #include <linux/writeback.h>
  35. #include <linux/statfs.h>
  36. #include <linux/compat.h>
  37. #include <linux/bit_spinlock.h>
  38. #include <linux/security.h>
  39. #include <linux/xattr.h>
  40. #include <linux/vmalloc.h>
  41. #include <linux/slab.h>
  42. #include "compat.h"
  43. #include "ctree.h"
  44. #include "disk-io.h"
  45. #include "transaction.h"
  46. #include "btrfs_inode.h"
  47. #include "ioctl.h"
  48. #include "print-tree.h"
  49. #include "volumes.h"
  50. #include "locking.h"
  51. /* Mask out flags that are inappropriate for the given type of inode. */
  52. static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
  53. {
  54. if (S_ISDIR(mode))
  55. return flags;
  56. else if (S_ISREG(mode))
  57. return flags & ~FS_DIRSYNC_FL;
  58. else
  59. return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
  60. }
  61. /*
  62. * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
  63. */
  64. static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
  65. {
  66. unsigned int iflags = 0;
  67. if (flags & BTRFS_INODE_SYNC)
  68. iflags |= FS_SYNC_FL;
  69. if (flags & BTRFS_INODE_IMMUTABLE)
  70. iflags |= FS_IMMUTABLE_FL;
  71. if (flags & BTRFS_INODE_APPEND)
  72. iflags |= FS_APPEND_FL;
  73. if (flags & BTRFS_INODE_NODUMP)
  74. iflags |= FS_NODUMP_FL;
  75. if (flags & BTRFS_INODE_NOATIME)
  76. iflags |= FS_NOATIME_FL;
  77. if (flags & BTRFS_INODE_DIRSYNC)
  78. iflags |= FS_DIRSYNC_FL;
  79. return iflags;
  80. }
  81. /*
  82. * Update inode->i_flags based on the btrfs internal flags.
  83. */
  84. void btrfs_update_iflags(struct inode *inode)
  85. {
  86. struct btrfs_inode *ip = BTRFS_I(inode);
  87. inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
  88. if (ip->flags & BTRFS_INODE_SYNC)
  89. inode->i_flags |= S_SYNC;
  90. if (ip->flags & BTRFS_INODE_IMMUTABLE)
  91. inode->i_flags |= S_IMMUTABLE;
  92. if (ip->flags & BTRFS_INODE_APPEND)
  93. inode->i_flags |= S_APPEND;
  94. if (ip->flags & BTRFS_INODE_NOATIME)
  95. inode->i_flags |= S_NOATIME;
  96. if (ip->flags & BTRFS_INODE_DIRSYNC)
  97. inode->i_flags |= S_DIRSYNC;
  98. }
  99. /*
  100. * Inherit flags from the parent inode.
  101. *
  102. * Unlike extN we don't have any flags we don't want to inherit currently.
  103. */
  104. void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
  105. {
  106. unsigned int flags;
  107. if (!dir)
  108. return;
  109. flags = BTRFS_I(dir)->flags;
  110. if (S_ISREG(inode->i_mode))
  111. flags &= ~BTRFS_INODE_DIRSYNC;
  112. else if (!S_ISDIR(inode->i_mode))
  113. flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
  114. BTRFS_I(inode)->flags = flags;
  115. btrfs_update_iflags(inode);
  116. }
  117. static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
  118. {
  119. struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
  120. unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
  121. if (copy_to_user(arg, &flags, sizeof(flags)))
  122. return -EFAULT;
  123. return 0;
  124. }
  125. static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
  126. {
  127. struct inode *inode = file->f_path.dentry->d_inode;
  128. struct btrfs_inode *ip = BTRFS_I(inode);
  129. struct btrfs_root *root = ip->root;
  130. struct btrfs_trans_handle *trans;
  131. unsigned int flags, oldflags;
  132. int ret;
  133. if (btrfs_root_readonly(root))
  134. return -EROFS;
  135. if (copy_from_user(&flags, arg, sizeof(flags)))
  136. return -EFAULT;
  137. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  138. FS_NOATIME_FL | FS_NODUMP_FL | \
  139. FS_SYNC_FL | FS_DIRSYNC_FL))
  140. return -EOPNOTSUPP;
  141. if (!is_owner_or_cap(inode))
  142. return -EACCES;
  143. mutex_lock(&inode->i_mutex);
  144. flags = btrfs_mask_flags(inode->i_mode, flags);
  145. oldflags = btrfs_flags_to_ioctl(ip->flags);
  146. if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
  147. if (!capable(CAP_LINUX_IMMUTABLE)) {
  148. ret = -EPERM;
  149. goto out_unlock;
  150. }
  151. }
  152. ret = mnt_want_write(file->f_path.mnt);
  153. if (ret)
  154. goto out_unlock;
  155. if (flags & FS_SYNC_FL)
  156. ip->flags |= BTRFS_INODE_SYNC;
  157. else
  158. ip->flags &= ~BTRFS_INODE_SYNC;
  159. if (flags & FS_IMMUTABLE_FL)
  160. ip->flags |= BTRFS_INODE_IMMUTABLE;
  161. else
  162. ip->flags &= ~BTRFS_INODE_IMMUTABLE;
  163. if (flags & FS_APPEND_FL)
  164. ip->flags |= BTRFS_INODE_APPEND;
  165. else
  166. ip->flags &= ~BTRFS_INODE_APPEND;
  167. if (flags & FS_NODUMP_FL)
  168. ip->flags |= BTRFS_INODE_NODUMP;
  169. else
  170. ip->flags &= ~BTRFS_INODE_NODUMP;
  171. if (flags & FS_NOATIME_FL)
  172. ip->flags |= BTRFS_INODE_NOATIME;
  173. else
  174. ip->flags &= ~BTRFS_INODE_NOATIME;
  175. if (flags & FS_DIRSYNC_FL)
  176. ip->flags |= BTRFS_INODE_DIRSYNC;
  177. else
  178. ip->flags &= ~BTRFS_INODE_DIRSYNC;
  179. trans = btrfs_join_transaction(root, 1);
  180. BUG_ON(!trans);
  181. ret = btrfs_update_inode(trans, root, inode);
  182. BUG_ON(ret);
  183. btrfs_update_iflags(inode);
  184. inode->i_ctime = CURRENT_TIME;
  185. btrfs_end_transaction(trans, root);
  186. mnt_drop_write(file->f_path.mnt);
  187. out_unlock:
  188. mutex_unlock(&inode->i_mutex);
  189. return 0;
  190. }
  191. static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
  192. {
  193. struct inode *inode = file->f_path.dentry->d_inode;
  194. return put_user(inode->i_generation, arg);
  195. }
  196. static noinline int create_subvol(struct btrfs_root *root,
  197. struct dentry *dentry,
  198. char *name, int namelen,
  199. u64 *async_transid)
  200. {
  201. struct btrfs_trans_handle *trans;
  202. struct btrfs_key key;
  203. struct btrfs_root_item root_item;
  204. struct btrfs_inode_item *inode_item;
  205. struct extent_buffer *leaf;
  206. struct btrfs_root *new_root;
  207. struct dentry *parent = dget_parent(dentry);
  208. struct inode *dir;
  209. int ret;
  210. int err;
  211. u64 objectid;
  212. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  213. u64 index = 0;
  214. ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
  215. 0, &objectid);
  216. if (ret) {
  217. dput(parent);
  218. return ret;
  219. }
  220. dir = parent->d_inode;
  221. /*
  222. * 1 - inode item
  223. * 2 - refs
  224. * 1 - root item
  225. * 2 - dir items
  226. */
  227. trans = btrfs_start_transaction(root, 6);
  228. if (IS_ERR(trans)) {
  229. dput(parent);
  230. return PTR_ERR(trans);
  231. }
  232. leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
  233. 0, objectid, NULL, 0, 0, 0);
  234. if (IS_ERR(leaf)) {
  235. ret = PTR_ERR(leaf);
  236. goto fail;
  237. }
  238. memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
  239. btrfs_set_header_bytenr(leaf, leaf->start);
  240. btrfs_set_header_generation(leaf, trans->transid);
  241. btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
  242. btrfs_set_header_owner(leaf, objectid);
  243. write_extent_buffer(leaf, root->fs_info->fsid,
  244. (unsigned long)btrfs_header_fsid(leaf),
  245. BTRFS_FSID_SIZE);
  246. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  247. (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
  248. BTRFS_UUID_SIZE);
  249. btrfs_mark_buffer_dirty(leaf);
  250. inode_item = &root_item.inode;
  251. memset(inode_item, 0, sizeof(*inode_item));
  252. inode_item->generation = cpu_to_le64(1);
  253. inode_item->size = cpu_to_le64(3);
  254. inode_item->nlink = cpu_to_le32(1);
  255. inode_item->nbytes = cpu_to_le64(root->leafsize);
  256. inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
  257. btrfs_set_root_bytenr(&root_item, leaf->start);
  258. btrfs_set_root_generation(&root_item, trans->transid);
  259. btrfs_set_root_level(&root_item, 0);
  260. btrfs_set_root_refs(&root_item, 1);
  261. btrfs_set_root_used(&root_item, leaf->len);
  262. btrfs_set_root_last_snapshot(&root_item, 0);
  263. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  264. root_item.drop_level = 0;
  265. btrfs_tree_unlock(leaf);
  266. free_extent_buffer(leaf);
  267. leaf = NULL;
  268. btrfs_set_root_dirid(&root_item, new_dirid);
  269. key.objectid = objectid;
  270. key.offset = 0;
  271. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  272. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  273. &root_item);
  274. if (ret)
  275. goto fail;
  276. key.offset = (u64)-1;
  277. new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
  278. BUG_ON(IS_ERR(new_root));
  279. btrfs_record_root_in_trans(trans, new_root);
  280. ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
  281. BTRFS_I(dir)->block_group);
  282. /*
  283. * insert the directory item
  284. */
  285. ret = btrfs_set_inode_index(dir, &index);
  286. BUG_ON(ret);
  287. ret = btrfs_insert_dir_item(trans, root,
  288. name, namelen, dir->i_ino, &key,
  289. BTRFS_FT_DIR, index);
  290. if (ret)
  291. goto fail;
  292. btrfs_i_size_write(dir, dir->i_size + namelen * 2);
  293. ret = btrfs_update_inode(trans, root, dir);
  294. BUG_ON(ret);
  295. ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
  296. objectid, root->root_key.objectid,
  297. dir->i_ino, index, name, namelen);
  298. BUG_ON(ret);
  299. d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
  300. fail:
  301. dput(parent);
  302. if (async_transid) {
  303. *async_transid = trans->transid;
  304. err = btrfs_commit_transaction_async(trans, root, 1);
  305. } else {
  306. err = btrfs_commit_transaction(trans, root);
  307. }
  308. if (err && !ret)
  309. ret = err;
  310. return ret;
  311. }
  312. static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
  313. char *name, int namelen, u64 *async_transid,
  314. bool readonly)
  315. {
  316. struct inode *inode;
  317. struct dentry *parent;
  318. struct btrfs_pending_snapshot *pending_snapshot;
  319. struct btrfs_trans_handle *trans;
  320. int ret;
  321. if (!root->ref_cows)
  322. return -EINVAL;
  323. pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
  324. if (!pending_snapshot)
  325. return -ENOMEM;
  326. btrfs_init_block_rsv(&pending_snapshot->block_rsv);
  327. pending_snapshot->dentry = dentry;
  328. pending_snapshot->root = root;
  329. pending_snapshot->readonly = readonly;
  330. trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
  331. if (IS_ERR(trans)) {
  332. ret = PTR_ERR(trans);
  333. goto fail;
  334. }
  335. ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
  336. BUG_ON(ret);
  337. list_add(&pending_snapshot->list,
  338. &trans->transaction->pending_snapshots);
  339. if (async_transid) {
  340. *async_transid = trans->transid;
  341. ret = btrfs_commit_transaction_async(trans,
  342. root->fs_info->extent_root, 1);
  343. } else {
  344. ret = btrfs_commit_transaction(trans,
  345. root->fs_info->extent_root);
  346. }
  347. BUG_ON(ret);
  348. ret = pending_snapshot->error;
  349. if (ret)
  350. goto fail;
  351. btrfs_orphan_cleanup(pending_snapshot->snap);
  352. parent = dget_parent(dentry);
  353. inode = btrfs_lookup_dentry(parent->d_inode, dentry);
  354. dput(parent);
  355. if (IS_ERR(inode)) {
  356. ret = PTR_ERR(inode);
  357. goto fail;
  358. }
  359. BUG_ON(!inode);
  360. d_instantiate(dentry, inode);
  361. ret = 0;
  362. fail:
  363. kfree(pending_snapshot);
  364. return ret;
  365. }
  366. /* copy of check_sticky in fs/namei.c()
  367. * It's inline, so penalty for filesystems that don't use sticky bit is
  368. * minimal.
  369. */
  370. static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
  371. {
  372. uid_t fsuid = current_fsuid();
  373. if (!(dir->i_mode & S_ISVTX))
  374. return 0;
  375. if (inode->i_uid == fsuid)
  376. return 0;
  377. if (dir->i_uid == fsuid)
  378. return 0;
  379. return !capable(CAP_FOWNER);
  380. }
  381. /* copy of may_delete in fs/namei.c()
  382. * Check whether we can remove a link victim from directory dir, check
  383. * whether the type of victim is right.
  384. * 1. We can't do it if dir is read-only (done in permission())
  385. * 2. We should have write and exec permissions on dir
  386. * 3. We can't remove anything from append-only dir
  387. * 4. We can't do anything with immutable dir (done in permission())
  388. * 5. If the sticky bit on dir is set we should either
  389. * a. be owner of dir, or
  390. * b. be owner of victim, or
  391. * c. have CAP_FOWNER capability
  392. * 6. If the victim is append-only or immutable we can't do antyhing with
  393. * links pointing to it.
  394. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  395. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  396. * 9. We can't remove a root or mountpoint.
  397. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  398. * nfs_async_unlink().
  399. */
  400. static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
  401. {
  402. int error;
  403. if (!victim->d_inode)
  404. return -ENOENT;
  405. BUG_ON(victim->d_parent->d_inode != dir);
  406. audit_inode_child(victim, dir);
  407. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  408. if (error)
  409. return error;
  410. if (IS_APPEND(dir))
  411. return -EPERM;
  412. if (btrfs_check_sticky(dir, victim->d_inode)||
  413. IS_APPEND(victim->d_inode)||
  414. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  415. return -EPERM;
  416. if (isdir) {
  417. if (!S_ISDIR(victim->d_inode->i_mode))
  418. return -ENOTDIR;
  419. if (IS_ROOT(victim))
  420. return -EBUSY;
  421. } else if (S_ISDIR(victim->d_inode->i_mode))
  422. return -EISDIR;
  423. if (IS_DEADDIR(dir))
  424. return -ENOENT;
  425. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  426. return -EBUSY;
  427. return 0;
  428. }
  429. /* copy of may_create in fs/namei.c() */
  430. static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
  431. {
  432. if (child->d_inode)
  433. return -EEXIST;
  434. if (IS_DEADDIR(dir))
  435. return -ENOENT;
  436. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  437. }
  438. /*
  439. * Create a new subvolume below @parent. This is largely modeled after
  440. * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
  441. * inside this filesystem so it's quite a bit simpler.
  442. */
  443. static noinline int btrfs_mksubvol(struct path *parent,
  444. char *name, int namelen,
  445. struct btrfs_root *snap_src,
  446. u64 *async_transid, bool readonly)
  447. {
  448. struct inode *dir = parent->dentry->d_inode;
  449. struct dentry *dentry;
  450. int error;
  451. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  452. dentry = lookup_one_len(name, parent->dentry, namelen);
  453. error = PTR_ERR(dentry);
  454. if (IS_ERR(dentry))
  455. goto out_unlock;
  456. error = -EEXIST;
  457. if (dentry->d_inode)
  458. goto out_dput;
  459. error = mnt_want_write(parent->mnt);
  460. if (error)
  461. goto out_dput;
  462. error = btrfs_may_create(dir, dentry);
  463. if (error)
  464. goto out_drop_write;
  465. down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  466. if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
  467. goto out_up_read;
  468. if (snap_src) {
  469. error = create_snapshot(snap_src, dentry,
  470. name, namelen, async_transid, readonly);
  471. } else {
  472. error = create_subvol(BTRFS_I(dir)->root, dentry,
  473. name, namelen, async_transid);
  474. }
  475. if (!error)
  476. fsnotify_mkdir(dir, dentry);
  477. out_up_read:
  478. up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
  479. out_drop_write:
  480. mnt_drop_write(parent->mnt);
  481. out_dput:
  482. dput(dentry);
  483. out_unlock:
  484. mutex_unlock(&dir->i_mutex);
  485. return error;
  486. }
  487. static int should_defrag_range(struct inode *inode, u64 start, u64 len,
  488. int thresh, u64 *last_len, u64 *skip,
  489. u64 *defrag_end)
  490. {
  491. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  492. struct extent_map *em = NULL;
  493. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  494. int ret = 1;
  495. if (thresh == 0)
  496. thresh = 256 * 1024;
  497. /*
  498. * make sure that once we start defragging and extent, we keep on
  499. * defragging it
  500. */
  501. if (start < *defrag_end)
  502. return 1;
  503. *skip = 0;
  504. /*
  505. * hopefully we have this extent in the tree already, try without
  506. * the full extent lock
  507. */
  508. read_lock(&em_tree->lock);
  509. em = lookup_extent_mapping(em_tree, start, len);
  510. read_unlock(&em_tree->lock);
  511. if (!em) {
  512. /* get the big lock and read metadata off disk */
  513. lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  514. em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
  515. unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
  516. if (IS_ERR(em))
  517. return 0;
  518. }
  519. /* this will cover holes, and inline extents */
  520. if (em->block_start >= EXTENT_MAP_LAST_BYTE)
  521. ret = 0;
  522. /*
  523. * we hit a real extent, if it is big don't bother defragging it again
  524. */
  525. if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
  526. ret = 0;
  527. /*
  528. * last_len ends up being a counter of how many bytes we've defragged.
  529. * every time we choose not to defrag an extent, we reset *last_len
  530. * so that the next tiny extent will force a defrag.
  531. *
  532. * The end result of this is that tiny extents before a single big
  533. * extent will force at least part of that big extent to be defragged.
  534. */
  535. if (ret) {
  536. *last_len += len;
  537. *defrag_end = extent_map_end(em);
  538. } else {
  539. *last_len = 0;
  540. *skip = extent_map_end(em);
  541. *defrag_end = 0;
  542. }
  543. free_extent_map(em);
  544. return ret;
  545. }
  546. static int btrfs_defrag_file(struct file *file,
  547. struct btrfs_ioctl_defrag_range_args *range)
  548. {
  549. struct inode *inode = fdentry(file)->d_inode;
  550. struct btrfs_root *root = BTRFS_I(inode)->root;
  551. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  552. struct btrfs_ordered_extent *ordered;
  553. struct page *page;
  554. struct btrfs_super_block *disk_super;
  555. unsigned long last_index;
  556. unsigned long ra_pages = root->fs_info->bdi.ra_pages;
  557. unsigned long total_read = 0;
  558. u64 features;
  559. u64 page_start;
  560. u64 page_end;
  561. u64 last_len = 0;
  562. u64 skip = 0;
  563. u64 defrag_end = 0;
  564. unsigned long i;
  565. int ret;
  566. int compress_type = BTRFS_COMPRESS_ZLIB;
  567. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
  568. if (range->compress_type > BTRFS_COMPRESS_TYPES)
  569. return -EINVAL;
  570. if (range->compress_type)
  571. compress_type = range->compress_type;
  572. }
  573. if (inode->i_size == 0)
  574. return 0;
  575. if (range->start + range->len > range->start) {
  576. last_index = min_t(u64, inode->i_size - 1,
  577. range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
  578. } else {
  579. last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
  580. }
  581. i = range->start >> PAGE_CACHE_SHIFT;
  582. while (i <= last_index) {
  583. if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
  584. PAGE_CACHE_SIZE,
  585. range->extent_thresh,
  586. &last_len, &skip,
  587. &defrag_end)) {
  588. unsigned long next;
  589. /*
  590. * the should_defrag function tells us how much to skip
  591. * bump our counter by the suggested amount
  592. */
  593. next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  594. i = max(i + 1, next);
  595. continue;
  596. }
  597. if (total_read % ra_pages == 0) {
  598. btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
  599. min(last_index, i + ra_pages - 1));
  600. }
  601. total_read++;
  602. mutex_lock(&inode->i_mutex);
  603. if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
  604. BTRFS_I(inode)->force_compress = compress_type;
  605. ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
  606. if (ret)
  607. goto err_unlock;
  608. again:
  609. if (inode->i_size == 0 ||
  610. i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
  611. ret = 0;
  612. goto err_reservations;
  613. }
  614. page = grab_cache_page(inode->i_mapping, i);
  615. if (!page) {
  616. ret = -ENOMEM;
  617. goto err_reservations;
  618. }
  619. if (!PageUptodate(page)) {
  620. btrfs_readpage(NULL, page);
  621. lock_page(page);
  622. if (!PageUptodate(page)) {
  623. unlock_page(page);
  624. page_cache_release(page);
  625. ret = -EIO;
  626. goto err_reservations;
  627. }
  628. }
  629. if (page->mapping != inode->i_mapping) {
  630. unlock_page(page);
  631. page_cache_release(page);
  632. goto again;
  633. }
  634. wait_on_page_writeback(page);
  635. if (PageDirty(page)) {
  636. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  637. goto loop_unlock;
  638. }
  639. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  640. page_end = page_start + PAGE_CACHE_SIZE - 1;
  641. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  642. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  643. if (ordered) {
  644. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  645. unlock_page(page);
  646. page_cache_release(page);
  647. btrfs_start_ordered_extent(inode, ordered, 1);
  648. btrfs_put_ordered_extent(ordered);
  649. goto again;
  650. }
  651. set_page_extent_mapped(page);
  652. /*
  653. * this makes sure page_mkwrite is called on the
  654. * page if it is dirtied again later
  655. */
  656. clear_page_dirty_for_io(page);
  657. clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
  658. page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
  659. EXTENT_DO_ACCOUNTING, GFP_NOFS);
  660. btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
  661. ClearPageChecked(page);
  662. set_page_dirty(page);
  663. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  664. loop_unlock:
  665. unlock_page(page);
  666. page_cache_release(page);
  667. mutex_unlock(&inode->i_mutex);
  668. balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
  669. i++;
  670. }
  671. if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
  672. filemap_flush(inode->i_mapping);
  673. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  674. /* the filemap_flush will queue IO into the worker threads, but
  675. * we have to make sure the IO is actually started and that
  676. * ordered extents get created before we return
  677. */
  678. atomic_inc(&root->fs_info->async_submit_draining);
  679. while (atomic_read(&root->fs_info->nr_async_submits) ||
  680. atomic_read(&root->fs_info->async_delalloc_pages)) {
  681. wait_event(root->fs_info->async_submit_wait,
  682. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  683. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  684. }
  685. atomic_dec(&root->fs_info->async_submit_draining);
  686. mutex_lock(&inode->i_mutex);
  687. BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
  688. mutex_unlock(&inode->i_mutex);
  689. }
  690. disk_super = &root->fs_info->super_copy;
  691. features = btrfs_super_incompat_flags(disk_super);
  692. if (range->compress_type == BTRFS_COMPRESS_LZO) {
  693. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  694. btrfs_set_super_incompat_flags(disk_super, features);
  695. }
  696. return 0;
  697. err_reservations:
  698. btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
  699. err_unlock:
  700. mutex_unlock(&inode->i_mutex);
  701. return ret;
  702. }
  703. static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
  704. void __user *arg)
  705. {
  706. u64 new_size;
  707. u64 old_size;
  708. u64 devid = 1;
  709. struct btrfs_ioctl_vol_args *vol_args;
  710. struct btrfs_trans_handle *trans;
  711. struct btrfs_device *device = NULL;
  712. char *sizestr;
  713. char *devstr = NULL;
  714. int ret = 0;
  715. int mod = 0;
  716. if (root->fs_info->sb->s_flags & MS_RDONLY)
  717. return -EROFS;
  718. if (!capable(CAP_SYS_ADMIN))
  719. return -EPERM;
  720. vol_args = memdup_user(arg, sizeof(*vol_args));
  721. if (IS_ERR(vol_args))
  722. return PTR_ERR(vol_args);
  723. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  724. mutex_lock(&root->fs_info->volume_mutex);
  725. sizestr = vol_args->name;
  726. devstr = strchr(sizestr, ':');
  727. if (devstr) {
  728. char *end;
  729. sizestr = devstr + 1;
  730. *devstr = '\0';
  731. devstr = vol_args->name;
  732. devid = simple_strtoull(devstr, &end, 10);
  733. printk(KERN_INFO "resizing devid %llu\n",
  734. (unsigned long long)devid);
  735. }
  736. device = btrfs_find_device(root, devid, NULL, NULL);
  737. if (!device) {
  738. printk(KERN_INFO "resizer unable to find device %llu\n",
  739. (unsigned long long)devid);
  740. ret = -EINVAL;
  741. goto out_unlock;
  742. }
  743. if (!strcmp(sizestr, "max"))
  744. new_size = device->bdev->bd_inode->i_size;
  745. else {
  746. if (sizestr[0] == '-') {
  747. mod = -1;
  748. sizestr++;
  749. } else if (sizestr[0] == '+') {
  750. mod = 1;
  751. sizestr++;
  752. }
  753. new_size = memparse(sizestr, NULL);
  754. if (new_size == 0) {
  755. ret = -EINVAL;
  756. goto out_unlock;
  757. }
  758. }
  759. old_size = device->total_bytes;
  760. if (mod < 0) {
  761. if (new_size > old_size) {
  762. ret = -EINVAL;
  763. goto out_unlock;
  764. }
  765. new_size = old_size - new_size;
  766. } else if (mod > 0) {
  767. new_size = old_size + new_size;
  768. }
  769. if (new_size < 256 * 1024 * 1024) {
  770. ret = -EINVAL;
  771. goto out_unlock;
  772. }
  773. if (new_size > device->bdev->bd_inode->i_size) {
  774. ret = -EFBIG;
  775. goto out_unlock;
  776. }
  777. do_div(new_size, root->sectorsize);
  778. new_size *= root->sectorsize;
  779. printk(KERN_INFO "new size for %s is %llu\n",
  780. device->name, (unsigned long long)new_size);
  781. if (new_size > old_size) {
  782. trans = btrfs_start_transaction(root, 0);
  783. ret = btrfs_grow_device(trans, device, new_size);
  784. btrfs_commit_transaction(trans, root);
  785. } else {
  786. ret = btrfs_shrink_device(device, new_size);
  787. }
  788. out_unlock:
  789. mutex_unlock(&root->fs_info->volume_mutex);
  790. kfree(vol_args);
  791. return ret;
  792. }
  793. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  794. char *name,
  795. unsigned long fd,
  796. int subvol,
  797. u64 *transid,
  798. bool readonly)
  799. {
  800. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  801. struct file *src_file;
  802. int namelen;
  803. int ret = 0;
  804. if (root->fs_info->sb->s_flags & MS_RDONLY)
  805. return -EROFS;
  806. namelen = strlen(name);
  807. if (strchr(name, '/')) {
  808. ret = -EINVAL;
  809. goto out;
  810. }
  811. if (subvol) {
  812. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  813. NULL, transid, readonly);
  814. } else {
  815. struct inode *src_inode;
  816. src_file = fget(fd);
  817. if (!src_file) {
  818. ret = -EINVAL;
  819. goto out;
  820. }
  821. src_inode = src_file->f_path.dentry->d_inode;
  822. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  823. printk(KERN_INFO "btrfs: Snapshot src from "
  824. "another FS\n");
  825. ret = -EINVAL;
  826. fput(src_file);
  827. goto out;
  828. }
  829. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  830. BTRFS_I(src_inode)->root,
  831. transid, readonly);
  832. fput(src_file);
  833. }
  834. out:
  835. return ret;
  836. }
  837. static noinline int btrfs_ioctl_snap_create(struct file *file,
  838. void __user *arg, int subvol)
  839. {
  840. struct btrfs_ioctl_vol_args *vol_args;
  841. int ret;
  842. vol_args = memdup_user(arg, sizeof(*vol_args));
  843. if (IS_ERR(vol_args))
  844. return PTR_ERR(vol_args);
  845. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  846. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  847. vol_args->fd, subvol,
  848. NULL, false);
  849. kfree(vol_args);
  850. return ret;
  851. }
  852. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  853. void __user *arg, int subvol)
  854. {
  855. struct btrfs_ioctl_vol_args_v2 *vol_args;
  856. int ret;
  857. u64 transid = 0;
  858. u64 *ptr = NULL;
  859. bool readonly = false;
  860. vol_args = memdup_user(arg, sizeof(*vol_args));
  861. if (IS_ERR(vol_args))
  862. return PTR_ERR(vol_args);
  863. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  864. if (vol_args->flags &
  865. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  866. ret = -EOPNOTSUPP;
  867. goto out;
  868. }
  869. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  870. ptr = &transid;
  871. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  872. readonly = true;
  873. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  874. vol_args->fd, subvol,
  875. ptr, readonly);
  876. if (ret == 0 && ptr &&
  877. copy_to_user(arg +
  878. offsetof(struct btrfs_ioctl_vol_args_v2,
  879. transid), ptr, sizeof(*ptr)))
  880. ret = -EFAULT;
  881. out:
  882. kfree(vol_args);
  883. return ret;
  884. }
  885. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  886. void __user *arg)
  887. {
  888. struct inode *inode = fdentry(file)->d_inode;
  889. struct btrfs_root *root = BTRFS_I(inode)->root;
  890. int ret = 0;
  891. u64 flags = 0;
  892. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  893. return -EINVAL;
  894. down_read(&root->fs_info->subvol_sem);
  895. if (btrfs_root_readonly(root))
  896. flags |= BTRFS_SUBVOL_RDONLY;
  897. up_read(&root->fs_info->subvol_sem);
  898. if (copy_to_user(arg, &flags, sizeof(flags)))
  899. ret = -EFAULT;
  900. return ret;
  901. }
  902. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  903. void __user *arg)
  904. {
  905. struct inode *inode = fdentry(file)->d_inode;
  906. struct btrfs_root *root = BTRFS_I(inode)->root;
  907. struct btrfs_trans_handle *trans;
  908. u64 root_flags;
  909. u64 flags;
  910. int ret = 0;
  911. if (root->fs_info->sb->s_flags & MS_RDONLY)
  912. return -EROFS;
  913. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  914. return -EINVAL;
  915. if (copy_from_user(&flags, arg, sizeof(flags)))
  916. return -EFAULT;
  917. if (flags & ~BTRFS_SUBVOL_CREATE_ASYNC)
  918. return -EINVAL;
  919. if (flags & ~BTRFS_SUBVOL_RDONLY)
  920. return -EOPNOTSUPP;
  921. down_write(&root->fs_info->subvol_sem);
  922. /* nothing to do */
  923. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  924. goto out;
  925. root_flags = btrfs_root_flags(&root->root_item);
  926. if (flags & BTRFS_SUBVOL_RDONLY)
  927. btrfs_set_root_flags(&root->root_item,
  928. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  929. else
  930. btrfs_set_root_flags(&root->root_item,
  931. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  932. trans = btrfs_start_transaction(root, 1);
  933. if (IS_ERR(trans)) {
  934. ret = PTR_ERR(trans);
  935. goto out_reset;
  936. }
  937. ret = btrfs_update_root(trans, root,
  938. &root->root_key, &root->root_item);
  939. btrfs_commit_transaction(trans, root);
  940. out_reset:
  941. if (ret)
  942. btrfs_set_root_flags(&root->root_item, root_flags);
  943. out:
  944. up_write(&root->fs_info->subvol_sem);
  945. return ret;
  946. }
  947. /*
  948. * helper to check if the subvolume references other subvolumes
  949. */
  950. static noinline int may_destroy_subvol(struct btrfs_root *root)
  951. {
  952. struct btrfs_path *path;
  953. struct btrfs_key key;
  954. int ret;
  955. path = btrfs_alloc_path();
  956. if (!path)
  957. return -ENOMEM;
  958. key.objectid = root->root_key.objectid;
  959. key.type = BTRFS_ROOT_REF_KEY;
  960. key.offset = (u64)-1;
  961. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  962. &key, path, 0, 0);
  963. if (ret < 0)
  964. goto out;
  965. BUG_ON(ret == 0);
  966. ret = 0;
  967. if (path->slots[0] > 0) {
  968. path->slots[0]--;
  969. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  970. if (key.objectid == root->root_key.objectid &&
  971. key.type == BTRFS_ROOT_REF_KEY)
  972. ret = -ENOTEMPTY;
  973. }
  974. out:
  975. btrfs_free_path(path);
  976. return ret;
  977. }
  978. static noinline int key_in_sk(struct btrfs_key *key,
  979. struct btrfs_ioctl_search_key *sk)
  980. {
  981. struct btrfs_key test;
  982. int ret;
  983. test.objectid = sk->min_objectid;
  984. test.type = sk->min_type;
  985. test.offset = sk->min_offset;
  986. ret = btrfs_comp_cpu_keys(key, &test);
  987. if (ret < 0)
  988. return 0;
  989. test.objectid = sk->max_objectid;
  990. test.type = sk->max_type;
  991. test.offset = sk->max_offset;
  992. ret = btrfs_comp_cpu_keys(key, &test);
  993. if (ret > 0)
  994. return 0;
  995. return 1;
  996. }
  997. static noinline int copy_to_sk(struct btrfs_root *root,
  998. struct btrfs_path *path,
  999. struct btrfs_key *key,
  1000. struct btrfs_ioctl_search_key *sk,
  1001. char *buf,
  1002. unsigned long *sk_offset,
  1003. int *num_found)
  1004. {
  1005. u64 found_transid;
  1006. struct extent_buffer *leaf;
  1007. struct btrfs_ioctl_search_header sh;
  1008. unsigned long item_off;
  1009. unsigned long item_len;
  1010. int nritems;
  1011. int i;
  1012. int slot;
  1013. int found = 0;
  1014. int ret = 0;
  1015. leaf = path->nodes[0];
  1016. slot = path->slots[0];
  1017. nritems = btrfs_header_nritems(leaf);
  1018. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1019. i = nritems;
  1020. goto advance_key;
  1021. }
  1022. found_transid = btrfs_header_generation(leaf);
  1023. for (i = slot; i < nritems; i++) {
  1024. item_off = btrfs_item_ptr_offset(leaf, i);
  1025. item_len = btrfs_item_size_nr(leaf, i);
  1026. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1027. item_len = 0;
  1028. if (sizeof(sh) + item_len + *sk_offset >
  1029. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1030. ret = 1;
  1031. goto overflow;
  1032. }
  1033. btrfs_item_key_to_cpu(leaf, key, i);
  1034. if (!key_in_sk(key, sk))
  1035. continue;
  1036. sh.objectid = key->objectid;
  1037. sh.offset = key->offset;
  1038. sh.type = key->type;
  1039. sh.len = item_len;
  1040. sh.transid = found_transid;
  1041. /* copy search result header */
  1042. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1043. *sk_offset += sizeof(sh);
  1044. if (item_len) {
  1045. char *p = buf + *sk_offset;
  1046. /* copy the item */
  1047. read_extent_buffer(leaf, p,
  1048. item_off, item_len);
  1049. *sk_offset += item_len;
  1050. }
  1051. found++;
  1052. if (*num_found >= sk->nr_items)
  1053. break;
  1054. }
  1055. advance_key:
  1056. ret = 0;
  1057. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1058. key->offset++;
  1059. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1060. key->offset = 0;
  1061. key->type++;
  1062. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1063. key->offset = 0;
  1064. key->type = 0;
  1065. key->objectid++;
  1066. } else
  1067. ret = 1;
  1068. overflow:
  1069. *num_found += found;
  1070. return ret;
  1071. }
  1072. static noinline int search_ioctl(struct inode *inode,
  1073. struct btrfs_ioctl_search_args *args)
  1074. {
  1075. struct btrfs_root *root;
  1076. struct btrfs_key key;
  1077. struct btrfs_key max_key;
  1078. struct btrfs_path *path;
  1079. struct btrfs_ioctl_search_key *sk = &args->key;
  1080. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1081. int ret;
  1082. int num_found = 0;
  1083. unsigned long sk_offset = 0;
  1084. path = btrfs_alloc_path();
  1085. if (!path)
  1086. return -ENOMEM;
  1087. if (sk->tree_id == 0) {
  1088. /* search the root of the inode that was passed */
  1089. root = BTRFS_I(inode)->root;
  1090. } else {
  1091. key.objectid = sk->tree_id;
  1092. key.type = BTRFS_ROOT_ITEM_KEY;
  1093. key.offset = (u64)-1;
  1094. root = btrfs_read_fs_root_no_name(info, &key);
  1095. if (IS_ERR(root)) {
  1096. printk(KERN_ERR "could not find root %llu\n",
  1097. sk->tree_id);
  1098. btrfs_free_path(path);
  1099. return -ENOENT;
  1100. }
  1101. }
  1102. key.objectid = sk->min_objectid;
  1103. key.type = sk->min_type;
  1104. key.offset = sk->min_offset;
  1105. max_key.objectid = sk->max_objectid;
  1106. max_key.type = sk->max_type;
  1107. max_key.offset = sk->max_offset;
  1108. path->keep_locks = 1;
  1109. while(1) {
  1110. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1111. sk->min_transid);
  1112. if (ret != 0) {
  1113. if (ret > 0)
  1114. ret = 0;
  1115. goto err;
  1116. }
  1117. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1118. &sk_offset, &num_found);
  1119. btrfs_release_path(root, path);
  1120. if (ret || num_found >= sk->nr_items)
  1121. break;
  1122. }
  1123. ret = 0;
  1124. err:
  1125. sk->nr_items = num_found;
  1126. btrfs_free_path(path);
  1127. return ret;
  1128. }
  1129. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1130. void __user *argp)
  1131. {
  1132. struct btrfs_ioctl_search_args *args;
  1133. struct inode *inode;
  1134. int ret;
  1135. if (!capable(CAP_SYS_ADMIN))
  1136. return -EPERM;
  1137. args = memdup_user(argp, sizeof(*args));
  1138. if (IS_ERR(args))
  1139. return PTR_ERR(args);
  1140. inode = fdentry(file)->d_inode;
  1141. ret = search_ioctl(inode, args);
  1142. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1143. ret = -EFAULT;
  1144. kfree(args);
  1145. return ret;
  1146. }
  1147. /*
  1148. * Search INODE_REFs to identify path name of 'dirid' directory
  1149. * in a 'tree_id' tree. and sets path name to 'name'.
  1150. */
  1151. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1152. u64 tree_id, u64 dirid, char *name)
  1153. {
  1154. struct btrfs_root *root;
  1155. struct btrfs_key key;
  1156. char *ptr;
  1157. int ret = -1;
  1158. int slot;
  1159. int len;
  1160. int total_len = 0;
  1161. struct btrfs_inode_ref *iref;
  1162. struct extent_buffer *l;
  1163. struct btrfs_path *path;
  1164. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1165. name[0]='\0';
  1166. return 0;
  1167. }
  1168. path = btrfs_alloc_path();
  1169. if (!path)
  1170. return -ENOMEM;
  1171. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1172. key.objectid = tree_id;
  1173. key.type = BTRFS_ROOT_ITEM_KEY;
  1174. key.offset = (u64)-1;
  1175. root = btrfs_read_fs_root_no_name(info, &key);
  1176. if (IS_ERR(root)) {
  1177. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1178. ret = -ENOENT;
  1179. goto out;
  1180. }
  1181. key.objectid = dirid;
  1182. key.type = BTRFS_INODE_REF_KEY;
  1183. key.offset = (u64)-1;
  1184. while(1) {
  1185. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1186. if (ret < 0)
  1187. goto out;
  1188. l = path->nodes[0];
  1189. slot = path->slots[0];
  1190. if (ret > 0 && slot > 0)
  1191. slot--;
  1192. btrfs_item_key_to_cpu(l, &key, slot);
  1193. if (ret > 0 && (key.objectid != dirid ||
  1194. key.type != BTRFS_INODE_REF_KEY)) {
  1195. ret = -ENOENT;
  1196. goto out;
  1197. }
  1198. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1199. len = btrfs_inode_ref_name_len(l, iref);
  1200. ptr -= len + 1;
  1201. total_len += len + 1;
  1202. if (ptr < name)
  1203. goto out;
  1204. *(ptr + len) = '/';
  1205. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1206. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1207. break;
  1208. btrfs_release_path(root, path);
  1209. key.objectid = key.offset;
  1210. key.offset = (u64)-1;
  1211. dirid = key.objectid;
  1212. }
  1213. if (ptr < name)
  1214. goto out;
  1215. memcpy(name, ptr, total_len);
  1216. name[total_len]='\0';
  1217. ret = 0;
  1218. out:
  1219. btrfs_free_path(path);
  1220. return ret;
  1221. }
  1222. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1223. void __user *argp)
  1224. {
  1225. struct btrfs_ioctl_ino_lookup_args *args;
  1226. struct inode *inode;
  1227. int ret;
  1228. if (!capable(CAP_SYS_ADMIN))
  1229. return -EPERM;
  1230. args = memdup_user(argp, sizeof(*args));
  1231. if (IS_ERR(args))
  1232. return PTR_ERR(args);
  1233. inode = fdentry(file)->d_inode;
  1234. if (args->treeid == 0)
  1235. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1236. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1237. args->treeid, args->objectid,
  1238. args->name);
  1239. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1240. ret = -EFAULT;
  1241. kfree(args);
  1242. return ret;
  1243. }
  1244. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1245. void __user *arg)
  1246. {
  1247. struct dentry *parent = fdentry(file);
  1248. struct dentry *dentry;
  1249. struct inode *dir = parent->d_inode;
  1250. struct inode *inode;
  1251. struct btrfs_root *root = BTRFS_I(dir)->root;
  1252. struct btrfs_root *dest = NULL;
  1253. struct btrfs_ioctl_vol_args *vol_args;
  1254. struct btrfs_trans_handle *trans;
  1255. int namelen;
  1256. int ret;
  1257. int err = 0;
  1258. vol_args = memdup_user(arg, sizeof(*vol_args));
  1259. if (IS_ERR(vol_args))
  1260. return PTR_ERR(vol_args);
  1261. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1262. namelen = strlen(vol_args->name);
  1263. if (strchr(vol_args->name, '/') ||
  1264. strncmp(vol_args->name, "..", namelen) == 0) {
  1265. err = -EINVAL;
  1266. goto out;
  1267. }
  1268. err = mnt_want_write(file->f_path.mnt);
  1269. if (err)
  1270. goto out;
  1271. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1272. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1273. if (IS_ERR(dentry)) {
  1274. err = PTR_ERR(dentry);
  1275. goto out_unlock_dir;
  1276. }
  1277. if (!dentry->d_inode) {
  1278. err = -ENOENT;
  1279. goto out_dput;
  1280. }
  1281. inode = dentry->d_inode;
  1282. dest = BTRFS_I(inode)->root;
  1283. if (!capable(CAP_SYS_ADMIN)){
  1284. /*
  1285. * Regular user. Only allow this with a special mount
  1286. * option, when the user has write+exec access to the
  1287. * subvol root, and when rmdir(2) would have been
  1288. * allowed.
  1289. *
  1290. * Note that this is _not_ check that the subvol is
  1291. * empty or doesn't contain data that we wouldn't
  1292. * otherwise be able to delete.
  1293. *
  1294. * Users who want to delete empty subvols should try
  1295. * rmdir(2).
  1296. */
  1297. err = -EPERM;
  1298. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1299. goto out_dput;
  1300. /*
  1301. * Do not allow deletion if the parent dir is the same
  1302. * as the dir to be deleted. That means the ioctl
  1303. * must be called on the dentry referencing the root
  1304. * of the subvol, not a random directory contained
  1305. * within it.
  1306. */
  1307. err = -EINVAL;
  1308. if (root == dest)
  1309. goto out_dput;
  1310. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1311. if (err)
  1312. goto out_dput;
  1313. /* check if subvolume may be deleted by a non-root user */
  1314. err = btrfs_may_delete(dir, dentry, 1);
  1315. if (err)
  1316. goto out_dput;
  1317. }
  1318. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1319. err = -EINVAL;
  1320. goto out_dput;
  1321. }
  1322. mutex_lock(&inode->i_mutex);
  1323. err = d_invalidate(dentry);
  1324. if (err)
  1325. goto out_unlock;
  1326. down_write(&root->fs_info->subvol_sem);
  1327. err = may_destroy_subvol(dest);
  1328. if (err)
  1329. goto out_up_write;
  1330. trans = btrfs_start_transaction(root, 0);
  1331. if (IS_ERR(trans)) {
  1332. err = PTR_ERR(trans);
  1333. goto out_up_write;
  1334. }
  1335. trans->block_rsv = &root->fs_info->global_block_rsv;
  1336. ret = btrfs_unlink_subvol(trans, root, dir,
  1337. dest->root_key.objectid,
  1338. dentry->d_name.name,
  1339. dentry->d_name.len);
  1340. BUG_ON(ret);
  1341. btrfs_record_root_in_trans(trans, dest);
  1342. memset(&dest->root_item.drop_progress, 0,
  1343. sizeof(dest->root_item.drop_progress));
  1344. dest->root_item.drop_level = 0;
  1345. btrfs_set_root_refs(&dest->root_item, 0);
  1346. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1347. ret = btrfs_insert_orphan_item(trans,
  1348. root->fs_info->tree_root,
  1349. dest->root_key.objectid);
  1350. BUG_ON(ret);
  1351. }
  1352. ret = btrfs_end_transaction(trans, root);
  1353. BUG_ON(ret);
  1354. inode->i_flags |= S_DEAD;
  1355. out_up_write:
  1356. up_write(&root->fs_info->subvol_sem);
  1357. out_unlock:
  1358. mutex_unlock(&inode->i_mutex);
  1359. if (!err) {
  1360. shrink_dcache_sb(root->fs_info->sb);
  1361. btrfs_invalidate_inodes(dest);
  1362. d_delete(dentry);
  1363. }
  1364. out_dput:
  1365. dput(dentry);
  1366. out_unlock_dir:
  1367. mutex_unlock(&dir->i_mutex);
  1368. mnt_drop_write(file->f_path.mnt);
  1369. out:
  1370. kfree(vol_args);
  1371. return err;
  1372. }
  1373. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1374. {
  1375. struct inode *inode = fdentry(file)->d_inode;
  1376. struct btrfs_root *root = BTRFS_I(inode)->root;
  1377. struct btrfs_ioctl_defrag_range_args *range;
  1378. int ret;
  1379. if (btrfs_root_readonly(root))
  1380. return -EROFS;
  1381. ret = mnt_want_write(file->f_path.mnt);
  1382. if (ret)
  1383. return ret;
  1384. switch (inode->i_mode & S_IFMT) {
  1385. case S_IFDIR:
  1386. if (!capable(CAP_SYS_ADMIN)) {
  1387. ret = -EPERM;
  1388. goto out;
  1389. }
  1390. ret = btrfs_defrag_root(root, 0);
  1391. if (ret)
  1392. goto out;
  1393. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1394. break;
  1395. case S_IFREG:
  1396. if (!(file->f_mode & FMODE_WRITE)) {
  1397. ret = -EINVAL;
  1398. goto out;
  1399. }
  1400. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1401. if (!range) {
  1402. ret = -ENOMEM;
  1403. goto out;
  1404. }
  1405. if (argp) {
  1406. if (copy_from_user(range, argp,
  1407. sizeof(*range))) {
  1408. ret = -EFAULT;
  1409. kfree(range);
  1410. goto out;
  1411. }
  1412. /* compression requires us to start the IO */
  1413. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1414. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1415. range->extent_thresh = (u32)-1;
  1416. }
  1417. } else {
  1418. /* the rest are all set to zero by kzalloc */
  1419. range->len = (u64)-1;
  1420. }
  1421. ret = btrfs_defrag_file(file, range);
  1422. kfree(range);
  1423. break;
  1424. default:
  1425. ret = -EINVAL;
  1426. }
  1427. out:
  1428. mnt_drop_write(file->f_path.mnt);
  1429. return ret;
  1430. }
  1431. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1432. {
  1433. struct btrfs_ioctl_vol_args *vol_args;
  1434. int ret;
  1435. if (!capable(CAP_SYS_ADMIN))
  1436. return -EPERM;
  1437. vol_args = memdup_user(arg, sizeof(*vol_args));
  1438. if (IS_ERR(vol_args))
  1439. return PTR_ERR(vol_args);
  1440. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1441. ret = btrfs_init_new_device(root, vol_args->name);
  1442. kfree(vol_args);
  1443. return ret;
  1444. }
  1445. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1446. {
  1447. struct btrfs_ioctl_vol_args *vol_args;
  1448. int ret;
  1449. if (!capable(CAP_SYS_ADMIN))
  1450. return -EPERM;
  1451. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1452. return -EROFS;
  1453. vol_args = memdup_user(arg, sizeof(*vol_args));
  1454. if (IS_ERR(vol_args))
  1455. return PTR_ERR(vol_args);
  1456. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1457. ret = btrfs_rm_device(root, vol_args->name);
  1458. kfree(vol_args);
  1459. return ret;
  1460. }
  1461. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1462. u64 off, u64 olen, u64 destoff)
  1463. {
  1464. struct inode *inode = fdentry(file)->d_inode;
  1465. struct btrfs_root *root = BTRFS_I(inode)->root;
  1466. struct file *src_file;
  1467. struct inode *src;
  1468. struct btrfs_trans_handle *trans;
  1469. struct btrfs_path *path;
  1470. struct extent_buffer *leaf;
  1471. char *buf;
  1472. struct btrfs_key key;
  1473. u32 nritems;
  1474. int slot;
  1475. int ret;
  1476. u64 len = olen;
  1477. u64 bs = root->fs_info->sb->s_blocksize;
  1478. u64 hint_byte;
  1479. /*
  1480. * TODO:
  1481. * - split compressed inline extents. annoying: we need to
  1482. * decompress into destination's address_space (the file offset
  1483. * may change, so source mapping won't do), then recompress (or
  1484. * otherwise reinsert) a subrange.
  1485. * - allow ranges within the same file to be cloned (provided
  1486. * they don't overlap)?
  1487. */
  1488. /* the destination must be opened for writing */
  1489. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1490. return -EINVAL;
  1491. if (btrfs_root_readonly(root))
  1492. return -EROFS;
  1493. ret = mnt_want_write(file->f_path.mnt);
  1494. if (ret)
  1495. return ret;
  1496. src_file = fget(srcfd);
  1497. if (!src_file) {
  1498. ret = -EBADF;
  1499. goto out_drop_write;
  1500. }
  1501. src = src_file->f_dentry->d_inode;
  1502. ret = -EINVAL;
  1503. if (src == inode)
  1504. goto out_fput;
  1505. /* the src must be open for reading */
  1506. if (!(src_file->f_mode & FMODE_READ))
  1507. goto out_fput;
  1508. ret = -EISDIR;
  1509. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1510. goto out_fput;
  1511. ret = -EXDEV;
  1512. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1513. goto out_fput;
  1514. ret = -ENOMEM;
  1515. buf = vmalloc(btrfs_level_size(root, 0));
  1516. if (!buf)
  1517. goto out_fput;
  1518. path = btrfs_alloc_path();
  1519. if (!path) {
  1520. vfree(buf);
  1521. goto out_fput;
  1522. }
  1523. path->reada = 2;
  1524. if (inode < src) {
  1525. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1526. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1527. } else {
  1528. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1529. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1530. }
  1531. /* determine range to clone */
  1532. ret = -EINVAL;
  1533. if (off + len > src->i_size || off + len < off)
  1534. goto out_unlock;
  1535. if (len == 0)
  1536. olen = len = src->i_size - off;
  1537. /* if we extend to eof, continue to block boundary */
  1538. if (off + len == src->i_size)
  1539. len = ALIGN(src->i_size, bs) - off;
  1540. /* verify the end result is block aligned */
  1541. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1542. !IS_ALIGNED(destoff, bs))
  1543. goto out_unlock;
  1544. /* do any pending delalloc/csum calc on src, one way or
  1545. another, and lock file content */
  1546. while (1) {
  1547. struct btrfs_ordered_extent *ordered;
  1548. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1549. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1550. if (!ordered &&
  1551. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1552. EXTENT_DELALLOC, 0, NULL))
  1553. break;
  1554. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1555. if (ordered)
  1556. btrfs_put_ordered_extent(ordered);
  1557. btrfs_wait_ordered_range(src, off, len);
  1558. }
  1559. /* clone data */
  1560. key.objectid = src->i_ino;
  1561. key.type = BTRFS_EXTENT_DATA_KEY;
  1562. key.offset = 0;
  1563. while (1) {
  1564. /*
  1565. * note the key will change type as we walk through the
  1566. * tree.
  1567. */
  1568. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1569. if (ret < 0)
  1570. goto out;
  1571. nritems = btrfs_header_nritems(path->nodes[0]);
  1572. if (path->slots[0] >= nritems) {
  1573. ret = btrfs_next_leaf(root, path);
  1574. if (ret < 0)
  1575. goto out;
  1576. if (ret > 0)
  1577. break;
  1578. nritems = btrfs_header_nritems(path->nodes[0]);
  1579. }
  1580. leaf = path->nodes[0];
  1581. slot = path->slots[0];
  1582. btrfs_item_key_to_cpu(leaf, &key, slot);
  1583. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1584. key.objectid != src->i_ino)
  1585. break;
  1586. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1587. struct btrfs_file_extent_item *extent;
  1588. int type;
  1589. u32 size;
  1590. struct btrfs_key new_key;
  1591. u64 disko = 0, diskl = 0;
  1592. u64 datao = 0, datal = 0;
  1593. u8 comp;
  1594. u64 endoff;
  1595. size = btrfs_item_size_nr(leaf, slot);
  1596. read_extent_buffer(leaf, buf,
  1597. btrfs_item_ptr_offset(leaf, slot),
  1598. size);
  1599. extent = btrfs_item_ptr(leaf, slot,
  1600. struct btrfs_file_extent_item);
  1601. comp = btrfs_file_extent_compression(leaf, extent);
  1602. type = btrfs_file_extent_type(leaf, extent);
  1603. if (type == BTRFS_FILE_EXTENT_REG ||
  1604. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1605. disko = btrfs_file_extent_disk_bytenr(leaf,
  1606. extent);
  1607. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1608. extent);
  1609. datao = btrfs_file_extent_offset(leaf, extent);
  1610. datal = btrfs_file_extent_num_bytes(leaf,
  1611. extent);
  1612. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1613. /* take upper bound, may be compressed */
  1614. datal = btrfs_file_extent_ram_bytes(leaf,
  1615. extent);
  1616. }
  1617. btrfs_release_path(root, path);
  1618. if (key.offset + datal <= off ||
  1619. key.offset >= off+len)
  1620. goto next;
  1621. memcpy(&new_key, &key, sizeof(new_key));
  1622. new_key.objectid = inode->i_ino;
  1623. new_key.offset = key.offset + destoff - off;
  1624. trans = btrfs_start_transaction(root, 1);
  1625. if (IS_ERR(trans)) {
  1626. ret = PTR_ERR(trans);
  1627. goto out;
  1628. }
  1629. if (type == BTRFS_FILE_EXTENT_REG ||
  1630. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1631. if (off > key.offset) {
  1632. datao += off - key.offset;
  1633. datal -= off - key.offset;
  1634. }
  1635. if (key.offset + datal > off + len)
  1636. datal = off + len - key.offset;
  1637. ret = btrfs_drop_extents(trans, inode,
  1638. new_key.offset,
  1639. new_key.offset + datal,
  1640. &hint_byte, 1);
  1641. BUG_ON(ret);
  1642. ret = btrfs_insert_empty_item(trans, root, path,
  1643. &new_key, size);
  1644. BUG_ON(ret);
  1645. leaf = path->nodes[0];
  1646. slot = path->slots[0];
  1647. write_extent_buffer(leaf, buf,
  1648. btrfs_item_ptr_offset(leaf, slot),
  1649. size);
  1650. extent = btrfs_item_ptr(leaf, slot,
  1651. struct btrfs_file_extent_item);
  1652. /* disko == 0 means it's a hole */
  1653. if (!disko)
  1654. datao = 0;
  1655. btrfs_set_file_extent_offset(leaf, extent,
  1656. datao);
  1657. btrfs_set_file_extent_num_bytes(leaf, extent,
  1658. datal);
  1659. if (disko) {
  1660. inode_add_bytes(inode, datal);
  1661. ret = btrfs_inc_extent_ref(trans, root,
  1662. disko, diskl, 0,
  1663. root->root_key.objectid,
  1664. inode->i_ino,
  1665. new_key.offset - datao);
  1666. BUG_ON(ret);
  1667. }
  1668. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1669. u64 skip = 0;
  1670. u64 trim = 0;
  1671. if (off > key.offset) {
  1672. skip = off - key.offset;
  1673. new_key.offset += skip;
  1674. }
  1675. if (key.offset + datal > off+len)
  1676. trim = key.offset + datal - (off+len);
  1677. if (comp && (skip || trim)) {
  1678. ret = -EINVAL;
  1679. btrfs_end_transaction(trans, root);
  1680. goto out;
  1681. }
  1682. size -= skip + trim;
  1683. datal -= skip + trim;
  1684. ret = btrfs_drop_extents(trans, inode,
  1685. new_key.offset,
  1686. new_key.offset + datal,
  1687. &hint_byte, 1);
  1688. BUG_ON(ret);
  1689. ret = btrfs_insert_empty_item(trans, root, path,
  1690. &new_key, size);
  1691. BUG_ON(ret);
  1692. if (skip) {
  1693. u32 start =
  1694. btrfs_file_extent_calc_inline_size(0);
  1695. memmove(buf+start, buf+start+skip,
  1696. datal);
  1697. }
  1698. leaf = path->nodes[0];
  1699. slot = path->slots[0];
  1700. write_extent_buffer(leaf, buf,
  1701. btrfs_item_ptr_offset(leaf, slot),
  1702. size);
  1703. inode_add_bytes(inode, datal);
  1704. }
  1705. btrfs_mark_buffer_dirty(leaf);
  1706. btrfs_release_path(root, path);
  1707. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1708. /*
  1709. * we round up to the block size at eof when
  1710. * determining which extents to clone above,
  1711. * but shouldn't round up the file size
  1712. */
  1713. endoff = new_key.offset + datal;
  1714. if (endoff > destoff+olen)
  1715. endoff = destoff+olen;
  1716. if (endoff > inode->i_size)
  1717. btrfs_i_size_write(inode, endoff);
  1718. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1719. ret = btrfs_update_inode(trans, root, inode);
  1720. BUG_ON(ret);
  1721. btrfs_end_transaction(trans, root);
  1722. }
  1723. next:
  1724. btrfs_release_path(root, path);
  1725. key.offset++;
  1726. }
  1727. ret = 0;
  1728. out:
  1729. btrfs_release_path(root, path);
  1730. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1731. out_unlock:
  1732. mutex_unlock(&src->i_mutex);
  1733. mutex_unlock(&inode->i_mutex);
  1734. vfree(buf);
  1735. btrfs_free_path(path);
  1736. out_fput:
  1737. fput(src_file);
  1738. out_drop_write:
  1739. mnt_drop_write(file->f_path.mnt);
  1740. return ret;
  1741. }
  1742. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1743. {
  1744. struct btrfs_ioctl_clone_range_args args;
  1745. if (copy_from_user(&args, argp, sizeof(args)))
  1746. return -EFAULT;
  1747. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1748. args.src_length, args.dest_offset);
  1749. }
  1750. /*
  1751. * there are many ways the trans_start and trans_end ioctls can lead
  1752. * to deadlocks. They should only be used by applications that
  1753. * basically own the machine, and have a very in depth understanding
  1754. * of all the possible deadlocks and enospc problems.
  1755. */
  1756. static long btrfs_ioctl_trans_start(struct file *file)
  1757. {
  1758. struct inode *inode = fdentry(file)->d_inode;
  1759. struct btrfs_root *root = BTRFS_I(inode)->root;
  1760. struct btrfs_trans_handle *trans;
  1761. int ret;
  1762. ret = -EPERM;
  1763. if (!capable(CAP_SYS_ADMIN))
  1764. goto out;
  1765. ret = -EINPROGRESS;
  1766. if (file->private_data)
  1767. goto out;
  1768. ret = -EROFS;
  1769. if (btrfs_root_readonly(root))
  1770. goto out;
  1771. ret = mnt_want_write(file->f_path.mnt);
  1772. if (ret)
  1773. goto out;
  1774. mutex_lock(&root->fs_info->trans_mutex);
  1775. root->fs_info->open_ioctl_trans++;
  1776. mutex_unlock(&root->fs_info->trans_mutex);
  1777. ret = -ENOMEM;
  1778. trans = btrfs_start_ioctl_transaction(root, 0);
  1779. if (!trans)
  1780. goto out_drop;
  1781. file->private_data = trans;
  1782. return 0;
  1783. out_drop:
  1784. mutex_lock(&root->fs_info->trans_mutex);
  1785. root->fs_info->open_ioctl_trans--;
  1786. mutex_unlock(&root->fs_info->trans_mutex);
  1787. mnt_drop_write(file->f_path.mnt);
  1788. out:
  1789. return ret;
  1790. }
  1791. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1792. {
  1793. struct inode *inode = fdentry(file)->d_inode;
  1794. struct btrfs_root *root = BTRFS_I(inode)->root;
  1795. struct btrfs_root *new_root;
  1796. struct btrfs_dir_item *di;
  1797. struct btrfs_trans_handle *trans;
  1798. struct btrfs_path *path;
  1799. struct btrfs_key location;
  1800. struct btrfs_disk_key disk_key;
  1801. struct btrfs_super_block *disk_super;
  1802. u64 features;
  1803. u64 objectid = 0;
  1804. u64 dir_id;
  1805. if (!capable(CAP_SYS_ADMIN))
  1806. return -EPERM;
  1807. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1808. return -EFAULT;
  1809. if (!objectid)
  1810. objectid = root->root_key.objectid;
  1811. location.objectid = objectid;
  1812. location.type = BTRFS_ROOT_ITEM_KEY;
  1813. location.offset = (u64)-1;
  1814. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1815. if (IS_ERR(new_root))
  1816. return PTR_ERR(new_root);
  1817. if (btrfs_root_refs(&new_root->root_item) == 0)
  1818. return -ENOENT;
  1819. path = btrfs_alloc_path();
  1820. if (!path)
  1821. return -ENOMEM;
  1822. path->leave_spinning = 1;
  1823. trans = btrfs_start_transaction(root, 1);
  1824. if (!trans) {
  1825. btrfs_free_path(path);
  1826. return -ENOMEM;
  1827. }
  1828. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1829. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1830. dir_id, "default", 7, 1);
  1831. if (IS_ERR_OR_NULL(di)) {
  1832. btrfs_free_path(path);
  1833. btrfs_end_transaction(trans, root);
  1834. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1835. "this isn't going to work\n");
  1836. return -ENOENT;
  1837. }
  1838. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1839. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1840. btrfs_mark_buffer_dirty(path->nodes[0]);
  1841. btrfs_free_path(path);
  1842. disk_super = &root->fs_info->super_copy;
  1843. features = btrfs_super_incompat_flags(disk_super);
  1844. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1845. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1846. btrfs_set_super_incompat_flags(disk_super, features);
  1847. }
  1848. btrfs_end_transaction(trans, root);
  1849. return 0;
  1850. }
  1851. static void get_block_group_info(struct list_head *groups_list,
  1852. struct btrfs_ioctl_space_info *space)
  1853. {
  1854. struct btrfs_block_group_cache *block_group;
  1855. space->total_bytes = 0;
  1856. space->used_bytes = 0;
  1857. space->flags = 0;
  1858. list_for_each_entry(block_group, groups_list, list) {
  1859. space->flags = block_group->flags;
  1860. space->total_bytes += block_group->key.offset;
  1861. space->used_bytes +=
  1862. btrfs_block_group_used(&block_group->item);
  1863. }
  1864. }
  1865. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1866. {
  1867. struct btrfs_ioctl_space_args space_args;
  1868. struct btrfs_ioctl_space_info space;
  1869. struct btrfs_ioctl_space_info *dest;
  1870. struct btrfs_ioctl_space_info *dest_orig;
  1871. struct btrfs_ioctl_space_info *user_dest;
  1872. struct btrfs_space_info *info;
  1873. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1874. BTRFS_BLOCK_GROUP_SYSTEM,
  1875. BTRFS_BLOCK_GROUP_METADATA,
  1876. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1877. int num_types = 4;
  1878. int alloc_size;
  1879. int ret = 0;
  1880. int slot_count = 0;
  1881. int i, c;
  1882. if (copy_from_user(&space_args,
  1883. (struct btrfs_ioctl_space_args __user *)arg,
  1884. sizeof(space_args)))
  1885. return -EFAULT;
  1886. for (i = 0; i < num_types; i++) {
  1887. struct btrfs_space_info *tmp;
  1888. info = NULL;
  1889. rcu_read_lock();
  1890. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1891. list) {
  1892. if (tmp->flags == types[i]) {
  1893. info = tmp;
  1894. break;
  1895. }
  1896. }
  1897. rcu_read_unlock();
  1898. if (!info)
  1899. continue;
  1900. down_read(&info->groups_sem);
  1901. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1902. if (!list_empty(&info->block_groups[c]))
  1903. slot_count++;
  1904. }
  1905. up_read(&info->groups_sem);
  1906. }
  1907. /* space_slots == 0 means they are asking for a count */
  1908. if (space_args.space_slots == 0) {
  1909. space_args.total_spaces = slot_count;
  1910. goto out;
  1911. }
  1912. slot_count = min_t(int, space_args.space_slots, slot_count);
  1913. alloc_size = sizeof(*dest) * slot_count;
  1914. /* we generally have at most 6 or so space infos, one for each raid
  1915. * level. So, a whole page should be more than enough for everyone
  1916. */
  1917. if (alloc_size > PAGE_CACHE_SIZE)
  1918. return -ENOMEM;
  1919. space_args.total_spaces = 0;
  1920. dest = kmalloc(alloc_size, GFP_NOFS);
  1921. if (!dest)
  1922. return -ENOMEM;
  1923. dest_orig = dest;
  1924. /* now we have a buffer to copy into */
  1925. for (i = 0; i < num_types; i++) {
  1926. struct btrfs_space_info *tmp;
  1927. info = NULL;
  1928. rcu_read_lock();
  1929. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1930. list) {
  1931. if (tmp->flags == types[i]) {
  1932. info = tmp;
  1933. break;
  1934. }
  1935. }
  1936. rcu_read_unlock();
  1937. if (!info)
  1938. continue;
  1939. down_read(&info->groups_sem);
  1940. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1941. if (!list_empty(&info->block_groups[c])) {
  1942. get_block_group_info(&info->block_groups[c],
  1943. &space);
  1944. memcpy(dest, &space, sizeof(space));
  1945. dest++;
  1946. space_args.total_spaces++;
  1947. }
  1948. }
  1949. up_read(&info->groups_sem);
  1950. }
  1951. user_dest = (struct btrfs_ioctl_space_info *)
  1952. (arg + sizeof(struct btrfs_ioctl_space_args));
  1953. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1954. ret = -EFAULT;
  1955. kfree(dest_orig);
  1956. out:
  1957. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1958. ret = -EFAULT;
  1959. return ret;
  1960. }
  1961. /*
  1962. * there are many ways the trans_start and trans_end ioctls can lead
  1963. * to deadlocks. They should only be used by applications that
  1964. * basically own the machine, and have a very in depth understanding
  1965. * of all the possible deadlocks and enospc problems.
  1966. */
  1967. long btrfs_ioctl_trans_end(struct file *file)
  1968. {
  1969. struct inode *inode = fdentry(file)->d_inode;
  1970. struct btrfs_root *root = BTRFS_I(inode)->root;
  1971. struct btrfs_trans_handle *trans;
  1972. trans = file->private_data;
  1973. if (!trans)
  1974. return -EINVAL;
  1975. file->private_data = NULL;
  1976. btrfs_end_transaction(trans, root);
  1977. mutex_lock(&root->fs_info->trans_mutex);
  1978. root->fs_info->open_ioctl_trans--;
  1979. mutex_unlock(&root->fs_info->trans_mutex);
  1980. mnt_drop_write(file->f_path.mnt);
  1981. return 0;
  1982. }
  1983. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  1984. {
  1985. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1986. struct btrfs_trans_handle *trans;
  1987. u64 transid;
  1988. trans = btrfs_start_transaction(root, 0);
  1989. transid = trans->transid;
  1990. btrfs_commit_transaction_async(trans, root, 0);
  1991. if (argp)
  1992. if (copy_to_user(argp, &transid, sizeof(transid)))
  1993. return -EFAULT;
  1994. return 0;
  1995. }
  1996. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  1997. {
  1998. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  1999. u64 transid;
  2000. if (argp) {
  2001. if (copy_from_user(&transid, argp, sizeof(transid)))
  2002. return -EFAULT;
  2003. } else {
  2004. transid = 0; /* current trans */
  2005. }
  2006. return btrfs_wait_for_commit(root, transid);
  2007. }
  2008. long btrfs_ioctl(struct file *file, unsigned int
  2009. cmd, unsigned long arg)
  2010. {
  2011. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2012. void __user *argp = (void __user *)arg;
  2013. switch (cmd) {
  2014. case FS_IOC_GETFLAGS:
  2015. return btrfs_ioctl_getflags(file, argp);
  2016. case FS_IOC_SETFLAGS:
  2017. return btrfs_ioctl_setflags(file, argp);
  2018. case FS_IOC_GETVERSION:
  2019. return btrfs_ioctl_getversion(file, argp);
  2020. case BTRFS_IOC_SNAP_CREATE:
  2021. return btrfs_ioctl_snap_create(file, argp, 0);
  2022. case BTRFS_IOC_SNAP_CREATE_V2:
  2023. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2024. case BTRFS_IOC_SUBVOL_CREATE:
  2025. return btrfs_ioctl_snap_create(file, argp, 1);
  2026. case BTRFS_IOC_SNAP_DESTROY:
  2027. return btrfs_ioctl_snap_destroy(file, argp);
  2028. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2029. return btrfs_ioctl_subvol_getflags(file, argp);
  2030. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2031. return btrfs_ioctl_subvol_setflags(file, argp);
  2032. case BTRFS_IOC_DEFAULT_SUBVOL:
  2033. return btrfs_ioctl_default_subvol(file, argp);
  2034. case BTRFS_IOC_DEFRAG:
  2035. return btrfs_ioctl_defrag(file, NULL);
  2036. case BTRFS_IOC_DEFRAG_RANGE:
  2037. return btrfs_ioctl_defrag(file, argp);
  2038. case BTRFS_IOC_RESIZE:
  2039. return btrfs_ioctl_resize(root, argp);
  2040. case BTRFS_IOC_ADD_DEV:
  2041. return btrfs_ioctl_add_dev(root, argp);
  2042. case BTRFS_IOC_RM_DEV:
  2043. return btrfs_ioctl_rm_dev(root, argp);
  2044. case BTRFS_IOC_BALANCE:
  2045. return btrfs_balance(root->fs_info->dev_root);
  2046. case BTRFS_IOC_CLONE:
  2047. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2048. case BTRFS_IOC_CLONE_RANGE:
  2049. return btrfs_ioctl_clone_range(file, argp);
  2050. case BTRFS_IOC_TRANS_START:
  2051. return btrfs_ioctl_trans_start(file);
  2052. case BTRFS_IOC_TRANS_END:
  2053. return btrfs_ioctl_trans_end(file);
  2054. case BTRFS_IOC_TREE_SEARCH:
  2055. return btrfs_ioctl_tree_search(file, argp);
  2056. case BTRFS_IOC_INO_LOOKUP:
  2057. return btrfs_ioctl_ino_lookup(file, argp);
  2058. case BTRFS_IOC_SPACE_INFO:
  2059. return btrfs_ioctl_space_info(root, argp);
  2060. case BTRFS_IOC_SYNC:
  2061. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2062. return 0;
  2063. case BTRFS_IOC_START_SYNC:
  2064. return btrfs_ioctl_start_sync(file, argp);
  2065. case BTRFS_IOC_WAIT_SYNC:
  2066. return btrfs_ioctl_wait_sync(file, argp);
  2067. }
  2068. return -ENOTTY;
  2069. }