ioctl.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441
  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(IS_ERR(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. if (IS_ERR(trans)) {
  784. ret = PTR_ERR(trans);
  785. goto out_unlock;
  786. }
  787. ret = btrfs_grow_device(trans, device, new_size);
  788. btrfs_commit_transaction(trans, root);
  789. } else {
  790. ret = btrfs_shrink_device(device, new_size);
  791. }
  792. out_unlock:
  793. mutex_unlock(&root->fs_info->volume_mutex);
  794. kfree(vol_args);
  795. return ret;
  796. }
  797. static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
  798. char *name,
  799. unsigned long fd,
  800. int subvol,
  801. u64 *transid,
  802. bool readonly)
  803. {
  804. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  805. struct file *src_file;
  806. int namelen;
  807. int ret = 0;
  808. if (root->fs_info->sb->s_flags & MS_RDONLY)
  809. return -EROFS;
  810. namelen = strlen(name);
  811. if (strchr(name, '/')) {
  812. ret = -EINVAL;
  813. goto out;
  814. }
  815. if (subvol) {
  816. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  817. NULL, transid, readonly);
  818. } else {
  819. struct inode *src_inode;
  820. src_file = fget(fd);
  821. if (!src_file) {
  822. ret = -EINVAL;
  823. goto out;
  824. }
  825. src_inode = src_file->f_path.dentry->d_inode;
  826. if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
  827. printk(KERN_INFO "btrfs: Snapshot src from "
  828. "another FS\n");
  829. ret = -EINVAL;
  830. fput(src_file);
  831. goto out;
  832. }
  833. ret = btrfs_mksubvol(&file->f_path, name, namelen,
  834. BTRFS_I(src_inode)->root,
  835. transid, readonly);
  836. fput(src_file);
  837. }
  838. out:
  839. return ret;
  840. }
  841. static noinline int btrfs_ioctl_snap_create(struct file *file,
  842. void __user *arg, int subvol)
  843. {
  844. struct btrfs_ioctl_vol_args *vol_args;
  845. int ret;
  846. vol_args = memdup_user(arg, sizeof(*vol_args));
  847. if (IS_ERR(vol_args))
  848. return PTR_ERR(vol_args);
  849. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  850. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  851. vol_args->fd, subvol,
  852. NULL, false);
  853. kfree(vol_args);
  854. return ret;
  855. }
  856. static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
  857. void __user *arg, int subvol)
  858. {
  859. struct btrfs_ioctl_vol_args_v2 *vol_args;
  860. int ret;
  861. u64 transid = 0;
  862. u64 *ptr = NULL;
  863. bool readonly = false;
  864. vol_args = memdup_user(arg, sizeof(*vol_args));
  865. if (IS_ERR(vol_args))
  866. return PTR_ERR(vol_args);
  867. vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
  868. if (vol_args->flags &
  869. ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
  870. ret = -EOPNOTSUPP;
  871. goto out;
  872. }
  873. if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
  874. ptr = &transid;
  875. if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
  876. readonly = true;
  877. ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
  878. vol_args->fd, subvol,
  879. ptr, readonly);
  880. if (ret == 0 && ptr &&
  881. copy_to_user(arg +
  882. offsetof(struct btrfs_ioctl_vol_args_v2,
  883. transid), ptr, sizeof(*ptr)))
  884. ret = -EFAULT;
  885. out:
  886. kfree(vol_args);
  887. return ret;
  888. }
  889. static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
  890. void __user *arg)
  891. {
  892. struct inode *inode = fdentry(file)->d_inode;
  893. struct btrfs_root *root = BTRFS_I(inode)->root;
  894. int ret = 0;
  895. u64 flags = 0;
  896. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  897. return -EINVAL;
  898. down_read(&root->fs_info->subvol_sem);
  899. if (btrfs_root_readonly(root))
  900. flags |= BTRFS_SUBVOL_RDONLY;
  901. up_read(&root->fs_info->subvol_sem);
  902. if (copy_to_user(arg, &flags, sizeof(flags)))
  903. ret = -EFAULT;
  904. return ret;
  905. }
  906. static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
  907. void __user *arg)
  908. {
  909. struct inode *inode = fdentry(file)->d_inode;
  910. struct btrfs_root *root = BTRFS_I(inode)->root;
  911. struct btrfs_trans_handle *trans;
  912. u64 root_flags;
  913. u64 flags;
  914. int ret = 0;
  915. if (root->fs_info->sb->s_flags & MS_RDONLY)
  916. return -EROFS;
  917. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
  918. return -EINVAL;
  919. if (copy_from_user(&flags, arg, sizeof(flags)))
  920. return -EFAULT;
  921. if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
  922. return -EINVAL;
  923. if (flags & ~BTRFS_SUBVOL_RDONLY)
  924. return -EOPNOTSUPP;
  925. if (!is_owner_or_cap(inode))
  926. return -EACCES;
  927. down_write(&root->fs_info->subvol_sem);
  928. /* nothing to do */
  929. if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
  930. goto out;
  931. root_flags = btrfs_root_flags(&root->root_item);
  932. if (flags & BTRFS_SUBVOL_RDONLY)
  933. btrfs_set_root_flags(&root->root_item,
  934. root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
  935. else
  936. btrfs_set_root_flags(&root->root_item,
  937. root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
  938. trans = btrfs_start_transaction(root, 1);
  939. if (IS_ERR(trans)) {
  940. ret = PTR_ERR(trans);
  941. goto out_reset;
  942. }
  943. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  944. &root->root_key, &root->root_item);
  945. btrfs_commit_transaction(trans, root);
  946. out_reset:
  947. if (ret)
  948. btrfs_set_root_flags(&root->root_item, root_flags);
  949. out:
  950. up_write(&root->fs_info->subvol_sem);
  951. return ret;
  952. }
  953. /*
  954. * helper to check if the subvolume references other subvolumes
  955. */
  956. static noinline int may_destroy_subvol(struct btrfs_root *root)
  957. {
  958. struct btrfs_path *path;
  959. struct btrfs_key key;
  960. int ret;
  961. path = btrfs_alloc_path();
  962. if (!path)
  963. return -ENOMEM;
  964. key.objectid = root->root_key.objectid;
  965. key.type = BTRFS_ROOT_REF_KEY;
  966. key.offset = (u64)-1;
  967. ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
  968. &key, path, 0, 0);
  969. if (ret < 0)
  970. goto out;
  971. BUG_ON(ret == 0);
  972. ret = 0;
  973. if (path->slots[0] > 0) {
  974. path->slots[0]--;
  975. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  976. if (key.objectid == root->root_key.objectid &&
  977. key.type == BTRFS_ROOT_REF_KEY)
  978. ret = -ENOTEMPTY;
  979. }
  980. out:
  981. btrfs_free_path(path);
  982. return ret;
  983. }
  984. static noinline int key_in_sk(struct btrfs_key *key,
  985. struct btrfs_ioctl_search_key *sk)
  986. {
  987. struct btrfs_key test;
  988. int ret;
  989. test.objectid = sk->min_objectid;
  990. test.type = sk->min_type;
  991. test.offset = sk->min_offset;
  992. ret = btrfs_comp_cpu_keys(key, &test);
  993. if (ret < 0)
  994. return 0;
  995. test.objectid = sk->max_objectid;
  996. test.type = sk->max_type;
  997. test.offset = sk->max_offset;
  998. ret = btrfs_comp_cpu_keys(key, &test);
  999. if (ret > 0)
  1000. return 0;
  1001. return 1;
  1002. }
  1003. static noinline int copy_to_sk(struct btrfs_root *root,
  1004. struct btrfs_path *path,
  1005. struct btrfs_key *key,
  1006. struct btrfs_ioctl_search_key *sk,
  1007. char *buf,
  1008. unsigned long *sk_offset,
  1009. int *num_found)
  1010. {
  1011. u64 found_transid;
  1012. struct extent_buffer *leaf;
  1013. struct btrfs_ioctl_search_header sh;
  1014. unsigned long item_off;
  1015. unsigned long item_len;
  1016. int nritems;
  1017. int i;
  1018. int slot;
  1019. int found = 0;
  1020. int ret = 0;
  1021. leaf = path->nodes[0];
  1022. slot = path->slots[0];
  1023. nritems = btrfs_header_nritems(leaf);
  1024. if (btrfs_header_generation(leaf) > sk->max_transid) {
  1025. i = nritems;
  1026. goto advance_key;
  1027. }
  1028. found_transid = btrfs_header_generation(leaf);
  1029. for (i = slot; i < nritems; i++) {
  1030. item_off = btrfs_item_ptr_offset(leaf, i);
  1031. item_len = btrfs_item_size_nr(leaf, i);
  1032. if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
  1033. item_len = 0;
  1034. if (sizeof(sh) + item_len + *sk_offset >
  1035. BTRFS_SEARCH_ARGS_BUFSIZE) {
  1036. ret = 1;
  1037. goto overflow;
  1038. }
  1039. btrfs_item_key_to_cpu(leaf, key, i);
  1040. if (!key_in_sk(key, sk))
  1041. continue;
  1042. sh.objectid = key->objectid;
  1043. sh.offset = key->offset;
  1044. sh.type = key->type;
  1045. sh.len = item_len;
  1046. sh.transid = found_transid;
  1047. /* copy search result header */
  1048. memcpy(buf + *sk_offset, &sh, sizeof(sh));
  1049. *sk_offset += sizeof(sh);
  1050. if (item_len) {
  1051. char *p = buf + *sk_offset;
  1052. /* copy the item */
  1053. read_extent_buffer(leaf, p,
  1054. item_off, item_len);
  1055. *sk_offset += item_len;
  1056. }
  1057. found++;
  1058. if (*num_found >= sk->nr_items)
  1059. break;
  1060. }
  1061. advance_key:
  1062. ret = 0;
  1063. if (key->offset < (u64)-1 && key->offset < sk->max_offset)
  1064. key->offset++;
  1065. else if (key->type < (u8)-1 && key->type < sk->max_type) {
  1066. key->offset = 0;
  1067. key->type++;
  1068. } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
  1069. key->offset = 0;
  1070. key->type = 0;
  1071. key->objectid++;
  1072. } else
  1073. ret = 1;
  1074. overflow:
  1075. *num_found += found;
  1076. return ret;
  1077. }
  1078. static noinline int search_ioctl(struct inode *inode,
  1079. struct btrfs_ioctl_search_args *args)
  1080. {
  1081. struct btrfs_root *root;
  1082. struct btrfs_key key;
  1083. struct btrfs_key max_key;
  1084. struct btrfs_path *path;
  1085. struct btrfs_ioctl_search_key *sk = &args->key;
  1086. struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
  1087. int ret;
  1088. int num_found = 0;
  1089. unsigned long sk_offset = 0;
  1090. path = btrfs_alloc_path();
  1091. if (!path)
  1092. return -ENOMEM;
  1093. if (sk->tree_id == 0) {
  1094. /* search the root of the inode that was passed */
  1095. root = BTRFS_I(inode)->root;
  1096. } else {
  1097. key.objectid = sk->tree_id;
  1098. key.type = BTRFS_ROOT_ITEM_KEY;
  1099. key.offset = (u64)-1;
  1100. root = btrfs_read_fs_root_no_name(info, &key);
  1101. if (IS_ERR(root)) {
  1102. printk(KERN_ERR "could not find root %llu\n",
  1103. sk->tree_id);
  1104. btrfs_free_path(path);
  1105. return -ENOENT;
  1106. }
  1107. }
  1108. key.objectid = sk->min_objectid;
  1109. key.type = sk->min_type;
  1110. key.offset = sk->min_offset;
  1111. max_key.objectid = sk->max_objectid;
  1112. max_key.type = sk->max_type;
  1113. max_key.offset = sk->max_offset;
  1114. path->keep_locks = 1;
  1115. while(1) {
  1116. ret = btrfs_search_forward(root, &key, &max_key, path, 0,
  1117. sk->min_transid);
  1118. if (ret != 0) {
  1119. if (ret > 0)
  1120. ret = 0;
  1121. goto err;
  1122. }
  1123. ret = copy_to_sk(root, path, &key, sk, args->buf,
  1124. &sk_offset, &num_found);
  1125. btrfs_release_path(root, path);
  1126. if (ret || num_found >= sk->nr_items)
  1127. break;
  1128. }
  1129. ret = 0;
  1130. err:
  1131. sk->nr_items = num_found;
  1132. btrfs_free_path(path);
  1133. return ret;
  1134. }
  1135. static noinline int btrfs_ioctl_tree_search(struct file *file,
  1136. void __user *argp)
  1137. {
  1138. struct btrfs_ioctl_search_args *args;
  1139. struct inode *inode;
  1140. int ret;
  1141. if (!capable(CAP_SYS_ADMIN))
  1142. return -EPERM;
  1143. args = memdup_user(argp, sizeof(*args));
  1144. if (IS_ERR(args))
  1145. return PTR_ERR(args);
  1146. inode = fdentry(file)->d_inode;
  1147. ret = search_ioctl(inode, args);
  1148. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1149. ret = -EFAULT;
  1150. kfree(args);
  1151. return ret;
  1152. }
  1153. /*
  1154. * Search INODE_REFs to identify path name of 'dirid' directory
  1155. * in a 'tree_id' tree. and sets path name to 'name'.
  1156. */
  1157. static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
  1158. u64 tree_id, u64 dirid, char *name)
  1159. {
  1160. struct btrfs_root *root;
  1161. struct btrfs_key key;
  1162. char *ptr;
  1163. int ret = -1;
  1164. int slot;
  1165. int len;
  1166. int total_len = 0;
  1167. struct btrfs_inode_ref *iref;
  1168. struct extent_buffer *l;
  1169. struct btrfs_path *path;
  1170. if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
  1171. name[0]='\0';
  1172. return 0;
  1173. }
  1174. path = btrfs_alloc_path();
  1175. if (!path)
  1176. return -ENOMEM;
  1177. ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
  1178. key.objectid = tree_id;
  1179. key.type = BTRFS_ROOT_ITEM_KEY;
  1180. key.offset = (u64)-1;
  1181. root = btrfs_read_fs_root_no_name(info, &key);
  1182. if (IS_ERR(root)) {
  1183. printk(KERN_ERR "could not find root %llu\n", tree_id);
  1184. ret = -ENOENT;
  1185. goto out;
  1186. }
  1187. key.objectid = dirid;
  1188. key.type = BTRFS_INODE_REF_KEY;
  1189. key.offset = (u64)-1;
  1190. while(1) {
  1191. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1192. if (ret < 0)
  1193. goto out;
  1194. l = path->nodes[0];
  1195. slot = path->slots[0];
  1196. if (ret > 0 && slot > 0)
  1197. slot--;
  1198. btrfs_item_key_to_cpu(l, &key, slot);
  1199. if (ret > 0 && (key.objectid != dirid ||
  1200. key.type != BTRFS_INODE_REF_KEY)) {
  1201. ret = -ENOENT;
  1202. goto out;
  1203. }
  1204. iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
  1205. len = btrfs_inode_ref_name_len(l, iref);
  1206. ptr -= len + 1;
  1207. total_len += len + 1;
  1208. if (ptr < name)
  1209. goto out;
  1210. *(ptr + len) = '/';
  1211. read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
  1212. if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
  1213. break;
  1214. btrfs_release_path(root, path);
  1215. key.objectid = key.offset;
  1216. key.offset = (u64)-1;
  1217. dirid = key.objectid;
  1218. }
  1219. if (ptr < name)
  1220. goto out;
  1221. memcpy(name, ptr, total_len);
  1222. name[total_len]='\0';
  1223. ret = 0;
  1224. out:
  1225. btrfs_free_path(path);
  1226. return ret;
  1227. }
  1228. static noinline int btrfs_ioctl_ino_lookup(struct file *file,
  1229. void __user *argp)
  1230. {
  1231. struct btrfs_ioctl_ino_lookup_args *args;
  1232. struct inode *inode;
  1233. int ret;
  1234. if (!capable(CAP_SYS_ADMIN))
  1235. return -EPERM;
  1236. args = memdup_user(argp, sizeof(*args));
  1237. if (IS_ERR(args))
  1238. return PTR_ERR(args);
  1239. inode = fdentry(file)->d_inode;
  1240. if (args->treeid == 0)
  1241. args->treeid = BTRFS_I(inode)->root->root_key.objectid;
  1242. ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
  1243. args->treeid, args->objectid,
  1244. args->name);
  1245. if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
  1246. ret = -EFAULT;
  1247. kfree(args);
  1248. return ret;
  1249. }
  1250. static noinline int btrfs_ioctl_snap_destroy(struct file *file,
  1251. void __user *arg)
  1252. {
  1253. struct dentry *parent = fdentry(file);
  1254. struct dentry *dentry;
  1255. struct inode *dir = parent->d_inode;
  1256. struct inode *inode;
  1257. struct btrfs_root *root = BTRFS_I(dir)->root;
  1258. struct btrfs_root *dest = NULL;
  1259. struct btrfs_ioctl_vol_args *vol_args;
  1260. struct btrfs_trans_handle *trans;
  1261. int namelen;
  1262. int ret;
  1263. int err = 0;
  1264. vol_args = memdup_user(arg, sizeof(*vol_args));
  1265. if (IS_ERR(vol_args))
  1266. return PTR_ERR(vol_args);
  1267. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1268. namelen = strlen(vol_args->name);
  1269. if (strchr(vol_args->name, '/') ||
  1270. strncmp(vol_args->name, "..", namelen) == 0) {
  1271. err = -EINVAL;
  1272. goto out;
  1273. }
  1274. err = mnt_want_write(file->f_path.mnt);
  1275. if (err)
  1276. goto out;
  1277. mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
  1278. dentry = lookup_one_len(vol_args->name, parent, namelen);
  1279. if (IS_ERR(dentry)) {
  1280. err = PTR_ERR(dentry);
  1281. goto out_unlock_dir;
  1282. }
  1283. if (!dentry->d_inode) {
  1284. err = -ENOENT;
  1285. goto out_dput;
  1286. }
  1287. inode = dentry->d_inode;
  1288. dest = BTRFS_I(inode)->root;
  1289. if (!capable(CAP_SYS_ADMIN)){
  1290. /*
  1291. * Regular user. Only allow this with a special mount
  1292. * option, when the user has write+exec access to the
  1293. * subvol root, and when rmdir(2) would have been
  1294. * allowed.
  1295. *
  1296. * Note that this is _not_ check that the subvol is
  1297. * empty or doesn't contain data that we wouldn't
  1298. * otherwise be able to delete.
  1299. *
  1300. * Users who want to delete empty subvols should try
  1301. * rmdir(2).
  1302. */
  1303. err = -EPERM;
  1304. if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
  1305. goto out_dput;
  1306. /*
  1307. * Do not allow deletion if the parent dir is the same
  1308. * as the dir to be deleted. That means the ioctl
  1309. * must be called on the dentry referencing the root
  1310. * of the subvol, not a random directory contained
  1311. * within it.
  1312. */
  1313. err = -EINVAL;
  1314. if (root == dest)
  1315. goto out_dput;
  1316. err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
  1317. if (err)
  1318. goto out_dput;
  1319. /* check if subvolume may be deleted by a non-root user */
  1320. err = btrfs_may_delete(dir, dentry, 1);
  1321. if (err)
  1322. goto out_dput;
  1323. }
  1324. if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
  1325. err = -EINVAL;
  1326. goto out_dput;
  1327. }
  1328. mutex_lock(&inode->i_mutex);
  1329. err = d_invalidate(dentry);
  1330. if (err)
  1331. goto out_unlock;
  1332. down_write(&root->fs_info->subvol_sem);
  1333. err = may_destroy_subvol(dest);
  1334. if (err)
  1335. goto out_up_write;
  1336. trans = btrfs_start_transaction(root, 0);
  1337. if (IS_ERR(trans)) {
  1338. err = PTR_ERR(trans);
  1339. goto out_up_write;
  1340. }
  1341. trans->block_rsv = &root->fs_info->global_block_rsv;
  1342. ret = btrfs_unlink_subvol(trans, root, dir,
  1343. dest->root_key.objectid,
  1344. dentry->d_name.name,
  1345. dentry->d_name.len);
  1346. BUG_ON(ret);
  1347. btrfs_record_root_in_trans(trans, dest);
  1348. memset(&dest->root_item.drop_progress, 0,
  1349. sizeof(dest->root_item.drop_progress));
  1350. dest->root_item.drop_level = 0;
  1351. btrfs_set_root_refs(&dest->root_item, 0);
  1352. if (!xchg(&dest->orphan_item_inserted, 1)) {
  1353. ret = btrfs_insert_orphan_item(trans,
  1354. root->fs_info->tree_root,
  1355. dest->root_key.objectid);
  1356. BUG_ON(ret);
  1357. }
  1358. ret = btrfs_end_transaction(trans, root);
  1359. BUG_ON(ret);
  1360. inode->i_flags |= S_DEAD;
  1361. out_up_write:
  1362. up_write(&root->fs_info->subvol_sem);
  1363. out_unlock:
  1364. mutex_unlock(&inode->i_mutex);
  1365. if (!err) {
  1366. shrink_dcache_sb(root->fs_info->sb);
  1367. btrfs_invalidate_inodes(dest);
  1368. d_delete(dentry);
  1369. }
  1370. out_dput:
  1371. dput(dentry);
  1372. out_unlock_dir:
  1373. mutex_unlock(&dir->i_mutex);
  1374. mnt_drop_write(file->f_path.mnt);
  1375. out:
  1376. kfree(vol_args);
  1377. return err;
  1378. }
  1379. static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
  1380. {
  1381. struct inode *inode = fdentry(file)->d_inode;
  1382. struct btrfs_root *root = BTRFS_I(inode)->root;
  1383. struct btrfs_ioctl_defrag_range_args *range;
  1384. int ret;
  1385. if (btrfs_root_readonly(root))
  1386. return -EROFS;
  1387. ret = mnt_want_write(file->f_path.mnt);
  1388. if (ret)
  1389. return ret;
  1390. switch (inode->i_mode & S_IFMT) {
  1391. case S_IFDIR:
  1392. if (!capable(CAP_SYS_ADMIN)) {
  1393. ret = -EPERM;
  1394. goto out;
  1395. }
  1396. ret = btrfs_defrag_root(root, 0);
  1397. if (ret)
  1398. goto out;
  1399. ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
  1400. break;
  1401. case S_IFREG:
  1402. if (!(file->f_mode & FMODE_WRITE)) {
  1403. ret = -EINVAL;
  1404. goto out;
  1405. }
  1406. range = kzalloc(sizeof(*range), GFP_KERNEL);
  1407. if (!range) {
  1408. ret = -ENOMEM;
  1409. goto out;
  1410. }
  1411. if (argp) {
  1412. if (copy_from_user(range, argp,
  1413. sizeof(*range))) {
  1414. ret = -EFAULT;
  1415. kfree(range);
  1416. goto out;
  1417. }
  1418. /* compression requires us to start the IO */
  1419. if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
  1420. range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
  1421. range->extent_thresh = (u32)-1;
  1422. }
  1423. } else {
  1424. /* the rest are all set to zero by kzalloc */
  1425. range->len = (u64)-1;
  1426. }
  1427. ret = btrfs_defrag_file(file, range);
  1428. kfree(range);
  1429. break;
  1430. default:
  1431. ret = -EINVAL;
  1432. }
  1433. out:
  1434. mnt_drop_write(file->f_path.mnt);
  1435. return ret;
  1436. }
  1437. static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
  1438. {
  1439. struct btrfs_ioctl_vol_args *vol_args;
  1440. int ret;
  1441. if (!capable(CAP_SYS_ADMIN))
  1442. return -EPERM;
  1443. vol_args = memdup_user(arg, sizeof(*vol_args));
  1444. if (IS_ERR(vol_args))
  1445. return PTR_ERR(vol_args);
  1446. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1447. ret = btrfs_init_new_device(root, vol_args->name);
  1448. kfree(vol_args);
  1449. return ret;
  1450. }
  1451. static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
  1452. {
  1453. struct btrfs_ioctl_vol_args *vol_args;
  1454. int ret;
  1455. if (!capable(CAP_SYS_ADMIN))
  1456. return -EPERM;
  1457. if (root->fs_info->sb->s_flags & MS_RDONLY)
  1458. return -EROFS;
  1459. vol_args = memdup_user(arg, sizeof(*vol_args));
  1460. if (IS_ERR(vol_args))
  1461. return PTR_ERR(vol_args);
  1462. vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
  1463. ret = btrfs_rm_device(root, vol_args->name);
  1464. kfree(vol_args);
  1465. return ret;
  1466. }
  1467. static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
  1468. u64 off, u64 olen, u64 destoff)
  1469. {
  1470. struct inode *inode = fdentry(file)->d_inode;
  1471. struct btrfs_root *root = BTRFS_I(inode)->root;
  1472. struct file *src_file;
  1473. struct inode *src;
  1474. struct btrfs_trans_handle *trans;
  1475. struct btrfs_path *path;
  1476. struct extent_buffer *leaf;
  1477. char *buf;
  1478. struct btrfs_key key;
  1479. u32 nritems;
  1480. int slot;
  1481. int ret;
  1482. u64 len = olen;
  1483. u64 bs = root->fs_info->sb->s_blocksize;
  1484. u64 hint_byte;
  1485. /*
  1486. * TODO:
  1487. * - split compressed inline extents. annoying: we need to
  1488. * decompress into destination's address_space (the file offset
  1489. * may change, so source mapping won't do), then recompress (or
  1490. * otherwise reinsert) a subrange.
  1491. * - allow ranges within the same file to be cloned (provided
  1492. * they don't overlap)?
  1493. */
  1494. /* the destination must be opened for writing */
  1495. if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
  1496. return -EINVAL;
  1497. if (btrfs_root_readonly(root))
  1498. return -EROFS;
  1499. ret = mnt_want_write(file->f_path.mnt);
  1500. if (ret)
  1501. return ret;
  1502. src_file = fget(srcfd);
  1503. if (!src_file) {
  1504. ret = -EBADF;
  1505. goto out_drop_write;
  1506. }
  1507. src = src_file->f_dentry->d_inode;
  1508. ret = -EINVAL;
  1509. if (src == inode)
  1510. goto out_fput;
  1511. /* the src must be open for reading */
  1512. if (!(src_file->f_mode & FMODE_READ))
  1513. goto out_fput;
  1514. ret = -EISDIR;
  1515. if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
  1516. goto out_fput;
  1517. ret = -EXDEV;
  1518. if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
  1519. goto out_fput;
  1520. ret = -ENOMEM;
  1521. buf = vmalloc(btrfs_level_size(root, 0));
  1522. if (!buf)
  1523. goto out_fput;
  1524. path = btrfs_alloc_path();
  1525. if (!path) {
  1526. vfree(buf);
  1527. goto out_fput;
  1528. }
  1529. path->reada = 2;
  1530. if (inode < src) {
  1531. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  1532. mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
  1533. } else {
  1534. mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
  1535. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  1536. }
  1537. /* determine range to clone */
  1538. ret = -EINVAL;
  1539. if (off + len > src->i_size || off + len < off)
  1540. goto out_unlock;
  1541. if (len == 0)
  1542. olen = len = src->i_size - off;
  1543. /* if we extend to eof, continue to block boundary */
  1544. if (off + len == src->i_size)
  1545. len = ALIGN(src->i_size, bs) - off;
  1546. /* verify the end result is block aligned */
  1547. if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
  1548. !IS_ALIGNED(destoff, bs))
  1549. goto out_unlock;
  1550. /* do any pending delalloc/csum calc on src, one way or
  1551. another, and lock file content */
  1552. while (1) {
  1553. struct btrfs_ordered_extent *ordered;
  1554. lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1555. ordered = btrfs_lookup_first_ordered_extent(src, off+len);
  1556. if (!ordered &&
  1557. !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
  1558. EXTENT_DELALLOC, 0, NULL))
  1559. break;
  1560. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1561. if (ordered)
  1562. btrfs_put_ordered_extent(ordered);
  1563. btrfs_wait_ordered_range(src, off, len);
  1564. }
  1565. /* clone data */
  1566. key.objectid = src->i_ino;
  1567. key.type = BTRFS_EXTENT_DATA_KEY;
  1568. key.offset = 0;
  1569. while (1) {
  1570. /*
  1571. * note the key will change type as we walk through the
  1572. * tree.
  1573. */
  1574. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1575. if (ret < 0)
  1576. goto out;
  1577. nritems = btrfs_header_nritems(path->nodes[0]);
  1578. if (path->slots[0] >= nritems) {
  1579. ret = btrfs_next_leaf(root, path);
  1580. if (ret < 0)
  1581. goto out;
  1582. if (ret > 0)
  1583. break;
  1584. nritems = btrfs_header_nritems(path->nodes[0]);
  1585. }
  1586. leaf = path->nodes[0];
  1587. slot = path->slots[0];
  1588. btrfs_item_key_to_cpu(leaf, &key, slot);
  1589. if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
  1590. key.objectid != src->i_ino)
  1591. break;
  1592. if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
  1593. struct btrfs_file_extent_item *extent;
  1594. int type;
  1595. u32 size;
  1596. struct btrfs_key new_key;
  1597. u64 disko = 0, diskl = 0;
  1598. u64 datao = 0, datal = 0;
  1599. u8 comp;
  1600. u64 endoff;
  1601. size = btrfs_item_size_nr(leaf, slot);
  1602. read_extent_buffer(leaf, buf,
  1603. btrfs_item_ptr_offset(leaf, slot),
  1604. size);
  1605. extent = btrfs_item_ptr(leaf, slot,
  1606. struct btrfs_file_extent_item);
  1607. comp = btrfs_file_extent_compression(leaf, extent);
  1608. type = btrfs_file_extent_type(leaf, extent);
  1609. if (type == BTRFS_FILE_EXTENT_REG ||
  1610. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1611. disko = btrfs_file_extent_disk_bytenr(leaf,
  1612. extent);
  1613. diskl = btrfs_file_extent_disk_num_bytes(leaf,
  1614. extent);
  1615. datao = btrfs_file_extent_offset(leaf, extent);
  1616. datal = btrfs_file_extent_num_bytes(leaf,
  1617. extent);
  1618. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1619. /* take upper bound, may be compressed */
  1620. datal = btrfs_file_extent_ram_bytes(leaf,
  1621. extent);
  1622. }
  1623. btrfs_release_path(root, path);
  1624. if (key.offset + datal <= off ||
  1625. key.offset >= off+len)
  1626. goto next;
  1627. memcpy(&new_key, &key, sizeof(new_key));
  1628. new_key.objectid = inode->i_ino;
  1629. if (off <= key.offset)
  1630. new_key.offset = key.offset + destoff - off;
  1631. else
  1632. new_key.offset = destoff;
  1633. trans = btrfs_start_transaction(root, 1);
  1634. if (IS_ERR(trans)) {
  1635. ret = PTR_ERR(trans);
  1636. goto out;
  1637. }
  1638. if (type == BTRFS_FILE_EXTENT_REG ||
  1639. type == BTRFS_FILE_EXTENT_PREALLOC) {
  1640. if (off > key.offset) {
  1641. datao += off - key.offset;
  1642. datal -= off - key.offset;
  1643. }
  1644. if (key.offset + datal > off + len)
  1645. datal = off + len - key.offset;
  1646. ret = btrfs_drop_extents(trans, inode,
  1647. new_key.offset,
  1648. new_key.offset + datal,
  1649. &hint_byte, 1);
  1650. BUG_ON(ret);
  1651. ret = btrfs_insert_empty_item(trans, root, path,
  1652. &new_key, size);
  1653. BUG_ON(ret);
  1654. leaf = path->nodes[0];
  1655. slot = path->slots[0];
  1656. write_extent_buffer(leaf, buf,
  1657. btrfs_item_ptr_offset(leaf, slot),
  1658. size);
  1659. extent = btrfs_item_ptr(leaf, slot,
  1660. struct btrfs_file_extent_item);
  1661. /* disko == 0 means it's a hole */
  1662. if (!disko)
  1663. datao = 0;
  1664. btrfs_set_file_extent_offset(leaf, extent,
  1665. datao);
  1666. btrfs_set_file_extent_num_bytes(leaf, extent,
  1667. datal);
  1668. if (disko) {
  1669. inode_add_bytes(inode, datal);
  1670. ret = btrfs_inc_extent_ref(trans, root,
  1671. disko, diskl, 0,
  1672. root->root_key.objectid,
  1673. inode->i_ino,
  1674. new_key.offset - datao);
  1675. BUG_ON(ret);
  1676. }
  1677. } else if (type == BTRFS_FILE_EXTENT_INLINE) {
  1678. u64 skip = 0;
  1679. u64 trim = 0;
  1680. if (off > key.offset) {
  1681. skip = off - key.offset;
  1682. new_key.offset += skip;
  1683. }
  1684. if (key.offset + datal > off+len)
  1685. trim = key.offset + datal - (off+len);
  1686. if (comp && (skip || trim)) {
  1687. ret = -EINVAL;
  1688. btrfs_end_transaction(trans, root);
  1689. goto out;
  1690. }
  1691. size -= skip + trim;
  1692. datal -= skip + trim;
  1693. ret = btrfs_drop_extents(trans, inode,
  1694. new_key.offset,
  1695. new_key.offset + datal,
  1696. &hint_byte, 1);
  1697. BUG_ON(ret);
  1698. ret = btrfs_insert_empty_item(trans, root, path,
  1699. &new_key, size);
  1700. BUG_ON(ret);
  1701. if (skip) {
  1702. u32 start =
  1703. btrfs_file_extent_calc_inline_size(0);
  1704. memmove(buf+start, buf+start+skip,
  1705. datal);
  1706. }
  1707. leaf = path->nodes[0];
  1708. slot = path->slots[0];
  1709. write_extent_buffer(leaf, buf,
  1710. btrfs_item_ptr_offset(leaf, slot),
  1711. size);
  1712. inode_add_bytes(inode, datal);
  1713. }
  1714. btrfs_mark_buffer_dirty(leaf);
  1715. btrfs_release_path(root, path);
  1716. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1717. /*
  1718. * we round up to the block size at eof when
  1719. * determining which extents to clone above,
  1720. * but shouldn't round up the file size
  1721. */
  1722. endoff = new_key.offset + datal;
  1723. if (endoff > destoff+olen)
  1724. endoff = destoff+olen;
  1725. if (endoff > inode->i_size)
  1726. btrfs_i_size_write(inode, endoff);
  1727. BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
  1728. ret = btrfs_update_inode(trans, root, inode);
  1729. BUG_ON(ret);
  1730. btrfs_end_transaction(trans, root);
  1731. }
  1732. next:
  1733. btrfs_release_path(root, path);
  1734. key.offset++;
  1735. }
  1736. ret = 0;
  1737. out:
  1738. btrfs_release_path(root, path);
  1739. unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
  1740. out_unlock:
  1741. mutex_unlock(&src->i_mutex);
  1742. mutex_unlock(&inode->i_mutex);
  1743. vfree(buf);
  1744. btrfs_free_path(path);
  1745. out_fput:
  1746. fput(src_file);
  1747. out_drop_write:
  1748. mnt_drop_write(file->f_path.mnt);
  1749. return ret;
  1750. }
  1751. static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
  1752. {
  1753. struct btrfs_ioctl_clone_range_args args;
  1754. if (copy_from_user(&args, argp, sizeof(args)))
  1755. return -EFAULT;
  1756. return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
  1757. args.src_length, args.dest_offset);
  1758. }
  1759. /*
  1760. * there are many ways the trans_start and trans_end ioctls can lead
  1761. * to deadlocks. They should only be used by applications that
  1762. * basically own the machine, and have a very in depth understanding
  1763. * of all the possible deadlocks and enospc problems.
  1764. */
  1765. static long btrfs_ioctl_trans_start(struct file *file)
  1766. {
  1767. struct inode *inode = fdentry(file)->d_inode;
  1768. struct btrfs_root *root = BTRFS_I(inode)->root;
  1769. struct btrfs_trans_handle *trans;
  1770. int ret;
  1771. ret = -EPERM;
  1772. if (!capable(CAP_SYS_ADMIN))
  1773. goto out;
  1774. ret = -EINPROGRESS;
  1775. if (file->private_data)
  1776. goto out;
  1777. ret = -EROFS;
  1778. if (btrfs_root_readonly(root))
  1779. goto out;
  1780. ret = mnt_want_write(file->f_path.mnt);
  1781. if (ret)
  1782. goto out;
  1783. mutex_lock(&root->fs_info->trans_mutex);
  1784. root->fs_info->open_ioctl_trans++;
  1785. mutex_unlock(&root->fs_info->trans_mutex);
  1786. ret = -ENOMEM;
  1787. trans = btrfs_start_ioctl_transaction(root, 0);
  1788. if (IS_ERR(trans))
  1789. goto out_drop;
  1790. file->private_data = trans;
  1791. return 0;
  1792. out_drop:
  1793. mutex_lock(&root->fs_info->trans_mutex);
  1794. root->fs_info->open_ioctl_trans--;
  1795. mutex_unlock(&root->fs_info->trans_mutex);
  1796. mnt_drop_write(file->f_path.mnt);
  1797. out:
  1798. return ret;
  1799. }
  1800. static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
  1801. {
  1802. struct inode *inode = fdentry(file)->d_inode;
  1803. struct btrfs_root *root = BTRFS_I(inode)->root;
  1804. struct btrfs_root *new_root;
  1805. struct btrfs_dir_item *di;
  1806. struct btrfs_trans_handle *trans;
  1807. struct btrfs_path *path;
  1808. struct btrfs_key location;
  1809. struct btrfs_disk_key disk_key;
  1810. struct btrfs_super_block *disk_super;
  1811. u64 features;
  1812. u64 objectid = 0;
  1813. u64 dir_id;
  1814. if (!capable(CAP_SYS_ADMIN))
  1815. return -EPERM;
  1816. if (copy_from_user(&objectid, argp, sizeof(objectid)))
  1817. return -EFAULT;
  1818. if (!objectid)
  1819. objectid = root->root_key.objectid;
  1820. location.objectid = objectid;
  1821. location.type = BTRFS_ROOT_ITEM_KEY;
  1822. location.offset = (u64)-1;
  1823. new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  1824. if (IS_ERR(new_root))
  1825. return PTR_ERR(new_root);
  1826. if (btrfs_root_refs(&new_root->root_item) == 0)
  1827. return -ENOENT;
  1828. path = btrfs_alloc_path();
  1829. if (!path)
  1830. return -ENOMEM;
  1831. path->leave_spinning = 1;
  1832. trans = btrfs_start_transaction(root, 1);
  1833. if (IS_ERR(trans)) {
  1834. btrfs_free_path(path);
  1835. return PTR_ERR(trans);
  1836. }
  1837. dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
  1838. di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
  1839. dir_id, "default", 7, 1);
  1840. if (IS_ERR_OR_NULL(di)) {
  1841. btrfs_free_path(path);
  1842. btrfs_end_transaction(trans, root);
  1843. printk(KERN_ERR "Umm, you don't have the default dir item, "
  1844. "this isn't going to work\n");
  1845. return -ENOENT;
  1846. }
  1847. btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
  1848. btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
  1849. btrfs_mark_buffer_dirty(path->nodes[0]);
  1850. btrfs_free_path(path);
  1851. disk_super = &root->fs_info->super_copy;
  1852. features = btrfs_super_incompat_flags(disk_super);
  1853. if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
  1854. features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
  1855. btrfs_set_super_incompat_flags(disk_super, features);
  1856. }
  1857. btrfs_end_transaction(trans, root);
  1858. return 0;
  1859. }
  1860. static void get_block_group_info(struct list_head *groups_list,
  1861. struct btrfs_ioctl_space_info *space)
  1862. {
  1863. struct btrfs_block_group_cache *block_group;
  1864. space->total_bytes = 0;
  1865. space->used_bytes = 0;
  1866. space->flags = 0;
  1867. list_for_each_entry(block_group, groups_list, list) {
  1868. space->flags = block_group->flags;
  1869. space->total_bytes += block_group->key.offset;
  1870. space->used_bytes +=
  1871. btrfs_block_group_used(&block_group->item);
  1872. }
  1873. }
  1874. long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
  1875. {
  1876. struct btrfs_ioctl_space_args space_args;
  1877. struct btrfs_ioctl_space_info space;
  1878. struct btrfs_ioctl_space_info *dest;
  1879. struct btrfs_ioctl_space_info *dest_orig;
  1880. struct btrfs_ioctl_space_info *user_dest;
  1881. struct btrfs_space_info *info;
  1882. u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
  1883. BTRFS_BLOCK_GROUP_SYSTEM,
  1884. BTRFS_BLOCK_GROUP_METADATA,
  1885. BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
  1886. int num_types = 4;
  1887. int alloc_size;
  1888. int ret = 0;
  1889. u64 slot_count = 0;
  1890. int i, c;
  1891. if (copy_from_user(&space_args,
  1892. (struct btrfs_ioctl_space_args __user *)arg,
  1893. sizeof(space_args)))
  1894. return -EFAULT;
  1895. for (i = 0; i < num_types; i++) {
  1896. struct btrfs_space_info *tmp;
  1897. info = NULL;
  1898. rcu_read_lock();
  1899. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1900. list) {
  1901. if (tmp->flags == types[i]) {
  1902. info = tmp;
  1903. break;
  1904. }
  1905. }
  1906. rcu_read_unlock();
  1907. if (!info)
  1908. continue;
  1909. down_read(&info->groups_sem);
  1910. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1911. if (!list_empty(&info->block_groups[c]))
  1912. slot_count++;
  1913. }
  1914. up_read(&info->groups_sem);
  1915. }
  1916. /* space_slots == 0 means they are asking for a count */
  1917. if (space_args.space_slots == 0) {
  1918. space_args.total_spaces = slot_count;
  1919. goto out;
  1920. }
  1921. slot_count = min_t(u64, space_args.space_slots, slot_count);
  1922. alloc_size = sizeof(*dest) * slot_count;
  1923. /* we generally have at most 6 or so space infos, one for each raid
  1924. * level. So, a whole page should be more than enough for everyone
  1925. */
  1926. if (alloc_size > PAGE_CACHE_SIZE)
  1927. return -ENOMEM;
  1928. space_args.total_spaces = 0;
  1929. dest = kmalloc(alloc_size, GFP_NOFS);
  1930. if (!dest)
  1931. return -ENOMEM;
  1932. dest_orig = dest;
  1933. /* now we have a buffer to copy into */
  1934. for (i = 0; i < num_types; i++) {
  1935. struct btrfs_space_info *tmp;
  1936. if (!slot_count)
  1937. break;
  1938. info = NULL;
  1939. rcu_read_lock();
  1940. list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
  1941. list) {
  1942. if (tmp->flags == types[i]) {
  1943. info = tmp;
  1944. break;
  1945. }
  1946. }
  1947. rcu_read_unlock();
  1948. if (!info)
  1949. continue;
  1950. down_read(&info->groups_sem);
  1951. for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
  1952. if (!list_empty(&info->block_groups[c])) {
  1953. get_block_group_info(&info->block_groups[c],
  1954. &space);
  1955. memcpy(dest, &space, sizeof(space));
  1956. dest++;
  1957. space_args.total_spaces++;
  1958. slot_count--;
  1959. }
  1960. if (!slot_count)
  1961. break;
  1962. }
  1963. up_read(&info->groups_sem);
  1964. }
  1965. user_dest = (struct btrfs_ioctl_space_info *)
  1966. (arg + sizeof(struct btrfs_ioctl_space_args));
  1967. if (copy_to_user(user_dest, dest_orig, alloc_size))
  1968. ret = -EFAULT;
  1969. kfree(dest_orig);
  1970. out:
  1971. if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
  1972. ret = -EFAULT;
  1973. return ret;
  1974. }
  1975. /*
  1976. * there are many ways the trans_start and trans_end ioctls can lead
  1977. * to deadlocks. They should only be used by applications that
  1978. * basically own the machine, and have a very in depth understanding
  1979. * of all the possible deadlocks and enospc problems.
  1980. */
  1981. long btrfs_ioctl_trans_end(struct file *file)
  1982. {
  1983. struct inode *inode = fdentry(file)->d_inode;
  1984. struct btrfs_root *root = BTRFS_I(inode)->root;
  1985. struct btrfs_trans_handle *trans;
  1986. trans = file->private_data;
  1987. if (!trans)
  1988. return -EINVAL;
  1989. file->private_data = NULL;
  1990. btrfs_end_transaction(trans, root);
  1991. mutex_lock(&root->fs_info->trans_mutex);
  1992. root->fs_info->open_ioctl_trans--;
  1993. mutex_unlock(&root->fs_info->trans_mutex);
  1994. mnt_drop_write(file->f_path.mnt);
  1995. return 0;
  1996. }
  1997. static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
  1998. {
  1999. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2000. struct btrfs_trans_handle *trans;
  2001. u64 transid;
  2002. trans = btrfs_start_transaction(root, 0);
  2003. if (IS_ERR(trans))
  2004. return PTR_ERR(trans);
  2005. transid = trans->transid;
  2006. btrfs_commit_transaction_async(trans, root, 0);
  2007. if (argp)
  2008. if (copy_to_user(argp, &transid, sizeof(transid)))
  2009. return -EFAULT;
  2010. return 0;
  2011. }
  2012. static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
  2013. {
  2014. struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
  2015. u64 transid;
  2016. if (argp) {
  2017. if (copy_from_user(&transid, argp, sizeof(transid)))
  2018. return -EFAULT;
  2019. } else {
  2020. transid = 0; /* current trans */
  2021. }
  2022. return btrfs_wait_for_commit(root, transid);
  2023. }
  2024. long btrfs_ioctl(struct file *file, unsigned int
  2025. cmd, unsigned long arg)
  2026. {
  2027. struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
  2028. void __user *argp = (void __user *)arg;
  2029. switch (cmd) {
  2030. case FS_IOC_GETFLAGS:
  2031. return btrfs_ioctl_getflags(file, argp);
  2032. case FS_IOC_SETFLAGS:
  2033. return btrfs_ioctl_setflags(file, argp);
  2034. case FS_IOC_GETVERSION:
  2035. return btrfs_ioctl_getversion(file, argp);
  2036. case BTRFS_IOC_SNAP_CREATE:
  2037. return btrfs_ioctl_snap_create(file, argp, 0);
  2038. case BTRFS_IOC_SNAP_CREATE_V2:
  2039. return btrfs_ioctl_snap_create_v2(file, argp, 0);
  2040. case BTRFS_IOC_SUBVOL_CREATE:
  2041. return btrfs_ioctl_snap_create(file, argp, 1);
  2042. case BTRFS_IOC_SNAP_DESTROY:
  2043. return btrfs_ioctl_snap_destroy(file, argp);
  2044. case BTRFS_IOC_SUBVOL_GETFLAGS:
  2045. return btrfs_ioctl_subvol_getflags(file, argp);
  2046. case BTRFS_IOC_SUBVOL_SETFLAGS:
  2047. return btrfs_ioctl_subvol_setflags(file, argp);
  2048. case BTRFS_IOC_DEFAULT_SUBVOL:
  2049. return btrfs_ioctl_default_subvol(file, argp);
  2050. case BTRFS_IOC_DEFRAG:
  2051. return btrfs_ioctl_defrag(file, NULL);
  2052. case BTRFS_IOC_DEFRAG_RANGE:
  2053. return btrfs_ioctl_defrag(file, argp);
  2054. case BTRFS_IOC_RESIZE:
  2055. return btrfs_ioctl_resize(root, argp);
  2056. case BTRFS_IOC_ADD_DEV:
  2057. return btrfs_ioctl_add_dev(root, argp);
  2058. case BTRFS_IOC_RM_DEV:
  2059. return btrfs_ioctl_rm_dev(root, argp);
  2060. case BTRFS_IOC_BALANCE:
  2061. return btrfs_balance(root->fs_info->dev_root);
  2062. case BTRFS_IOC_CLONE:
  2063. return btrfs_ioctl_clone(file, arg, 0, 0, 0);
  2064. case BTRFS_IOC_CLONE_RANGE:
  2065. return btrfs_ioctl_clone_range(file, argp);
  2066. case BTRFS_IOC_TRANS_START:
  2067. return btrfs_ioctl_trans_start(file);
  2068. case BTRFS_IOC_TRANS_END:
  2069. return btrfs_ioctl_trans_end(file);
  2070. case BTRFS_IOC_TREE_SEARCH:
  2071. return btrfs_ioctl_tree_search(file, argp);
  2072. case BTRFS_IOC_INO_LOOKUP:
  2073. return btrfs_ioctl_ino_lookup(file, argp);
  2074. case BTRFS_IOC_SPACE_INFO:
  2075. return btrfs_ioctl_space_info(root, argp);
  2076. case BTRFS_IOC_SYNC:
  2077. btrfs_sync_fs(file->f_dentry->d_sb, 1);
  2078. return 0;
  2079. case BTRFS_IOC_START_SYNC:
  2080. return btrfs_ioctl_start_sync(file, argp);
  2081. case BTRFS_IOC_WAIT_SYNC:
  2082. return btrfs_ioctl_wait_sync(file, argp);
  2083. }
  2084. return -ENOTTY;
  2085. }