ops_inode.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/namei.h>
  14. #include <linux/mm.h>
  15. #include <linux/xattr.h>
  16. #include <linux/posix_acl.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include <linux/crc32.h>
  19. #include <linux/fiemap.h>
  20. #include <linux/swap.h>
  21. #include <linux/falloc.h>
  22. #include <asm/uaccess.h>
  23. #include "gfs2.h"
  24. #include "incore.h"
  25. #include "acl.h"
  26. #include "bmap.h"
  27. #include "dir.h"
  28. #include "xattr.h"
  29. #include "glock.h"
  30. #include "inode.h"
  31. #include "meta_io.h"
  32. #include "quota.h"
  33. #include "rgrp.h"
  34. #include "trans.h"
  35. #include "util.h"
  36. #include "super.h"
  37. /**
  38. * gfs2_create - Create a file
  39. * @dir: The directory in which to create the file
  40. * @dentry: The dentry of the new file
  41. * @mode: The mode of the new file
  42. *
  43. * Returns: errno
  44. */
  45. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  46. int mode, struct nameidata *nd)
  47. {
  48. struct gfs2_inode *dip = GFS2_I(dir);
  49. struct gfs2_sbd *sdp = GFS2_SB(dir);
  50. struct gfs2_holder ghs[2];
  51. struct inode *inode;
  52. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  53. for (;;) {
  54. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  55. if (!IS_ERR(inode)) {
  56. gfs2_trans_end(sdp);
  57. if (dip->i_alloc->al_rgd)
  58. gfs2_inplace_release(dip);
  59. gfs2_quota_unlock(dip);
  60. gfs2_alloc_put(dip);
  61. gfs2_glock_dq_uninit_m(2, ghs);
  62. mark_inode_dirty(inode);
  63. break;
  64. } else if (PTR_ERR(inode) != -EEXIST ||
  65. (nd && nd->flags & LOOKUP_EXCL)) {
  66. gfs2_holder_uninit(ghs);
  67. return PTR_ERR(inode);
  68. }
  69. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  70. if (inode) {
  71. if (!IS_ERR(inode)) {
  72. gfs2_holder_uninit(ghs);
  73. break;
  74. } else {
  75. gfs2_holder_uninit(ghs);
  76. return PTR_ERR(inode);
  77. }
  78. }
  79. }
  80. d_instantiate(dentry, inode);
  81. return 0;
  82. }
  83. /**
  84. * gfs2_lookup - Look up a filename in a directory and return its inode
  85. * @dir: The directory inode
  86. * @dentry: The dentry of the new inode
  87. * @nd: passed from Linux VFS, ignored by us
  88. *
  89. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  90. *
  91. * Returns: errno
  92. */
  93. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  94. struct nameidata *nd)
  95. {
  96. struct inode *inode = NULL;
  97. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  98. if (inode && IS_ERR(inode))
  99. return ERR_CAST(inode);
  100. if (inode) {
  101. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  102. struct gfs2_holder gh;
  103. int error;
  104. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  105. if (error) {
  106. iput(inode);
  107. return ERR_PTR(error);
  108. }
  109. gfs2_glock_dq_uninit(&gh);
  110. return d_splice_alias(inode, dentry);
  111. }
  112. d_add(dentry, inode);
  113. return NULL;
  114. }
  115. /**
  116. * gfs2_link - Link to a file
  117. * @old_dentry: The inode to link
  118. * @dir: Add link to this directory
  119. * @dentry: The name of the link
  120. *
  121. * Link the inode in "old_dentry" into the directory "dir" with the
  122. * name in "dentry".
  123. *
  124. * Returns: errno
  125. */
  126. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  127. struct dentry *dentry)
  128. {
  129. struct gfs2_inode *dip = GFS2_I(dir);
  130. struct gfs2_sbd *sdp = GFS2_SB(dir);
  131. struct inode *inode = old_dentry->d_inode;
  132. struct gfs2_inode *ip = GFS2_I(inode);
  133. struct gfs2_holder ghs[2];
  134. int alloc_required;
  135. int error;
  136. if (S_ISDIR(inode->i_mode))
  137. return -EPERM;
  138. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  139. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  140. error = gfs2_glock_nq(ghs); /* parent */
  141. if (error)
  142. goto out_parent;
  143. error = gfs2_glock_nq(ghs + 1); /* child */
  144. if (error)
  145. goto out_child;
  146. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0);
  147. if (error)
  148. goto out_gunlock;
  149. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  150. switch (error) {
  151. case -ENOENT:
  152. break;
  153. case 0:
  154. error = -EEXIST;
  155. default:
  156. goto out_gunlock;
  157. }
  158. error = -EINVAL;
  159. if (!dip->i_inode.i_nlink)
  160. goto out_gunlock;
  161. error = -EFBIG;
  162. if (dip->i_entries == (u32)-1)
  163. goto out_gunlock;
  164. error = -EPERM;
  165. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  166. goto out_gunlock;
  167. error = -EINVAL;
  168. if (!ip->i_inode.i_nlink)
  169. goto out_gunlock;
  170. error = -EMLINK;
  171. if (ip->i_inode.i_nlink == (u32)-1)
  172. goto out_gunlock;
  173. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  174. if (error < 0)
  175. goto out_gunlock;
  176. error = 0;
  177. if (alloc_required) {
  178. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  179. if (!al) {
  180. error = -ENOMEM;
  181. goto out_gunlock;
  182. }
  183. error = gfs2_quota_lock_check(dip);
  184. if (error)
  185. goto out_alloc;
  186. al->al_requested = sdp->sd_max_dirres;
  187. error = gfs2_inplace_reserve(dip);
  188. if (error)
  189. goto out_gunlock_q;
  190. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  191. gfs2_rg_blocks(al) +
  192. 2 * RES_DINODE + RES_STATFS +
  193. RES_QUOTA, 0);
  194. if (error)
  195. goto out_ipres;
  196. } else {
  197. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  198. if (error)
  199. goto out_ipres;
  200. }
  201. error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
  202. if (error)
  203. goto out_end_trans;
  204. error = gfs2_change_nlink(ip, +1);
  205. out_end_trans:
  206. gfs2_trans_end(sdp);
  207. out_ipres:
  208. if (alloc_required)
  209. gfs2_inplace_release(dip);
  210. out_gunlock_q:
  211. if (alloc_required)
  212. gfs2_quota_unlock(dip);
  213. out_alloc:
  214. if (alloc_required)
  215. gfs2_alloc_put(dip);
  216. out_gunlock:
  217. gfs2_glock_dq(ghs + 1);
  218. out_child:
  219. gfs2_glock_dq(ghs);
  220. out_parent:
  221. gfs2_holder_uninit(ghs);
  222. gfs2_holder_uninit(ghs + 1);
  223. if (!error) {
  224. ihold(inode);
  225. d_instantiate(dentry, inode);
  226. mark_inode_dirty(inode);
  227. }
  228. return error;
  229. }
  230. /*
  231. * gfs2_unlink_ok - check to see that a inode is still in a directory
  232. * @dip: the directory
  233. * @name: the name of the file
  234. * @ip: the inode
  235. *
  236. * Assumes that the lock on (at least) @dip is held.
  237. *
  238. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  239. */
  240. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  241. const struct gfs2_inode *ip)
  242. {
  243. int error;
  244. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  245. return -EPERM;
  246. if ((dip->i_inode.i_mode & S_ISVTX) &&
  247. dip->i_inode.i_uid != current_fsuid() &&
  248. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  249. return -EPERM;
  250. if (IS_APPEND(&dip->i_inode))
  251. return -EPERM;
  252. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
  253. if (error)
  254. return error;
  255. error = gfs2_dir_check(&dip->i_inode, name, ip);
  256. if (error)
  257. return error;
  258. return 0;
  259. }
  260. /**
  261. * gfs2_unlink - Unlink a file
  262. * @dir: The inode of the directory containing the file to unlink
  263. * @dentry: The file itself
  264. *
  265. * Unlink a file. Call gfs2_unlinki()
  266. *
  267. * Returns: errno
  268. */
  269. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  270. {
  271. struct gfs2_inode *dip = GFS2_I(dir);
  272. struct gfs2_sbd *sdp = GFS2_SB(dir);
  273. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  274. struct gfs2_holder ghs[3];
  275. struct gfs2_rgrpd *rgd;
  276. struct gfs2_holder ri_gh;
  277. int error;
  278. error = gfs2_rindex_hold(sdp, &ri_gh);
  279. if (error)
  280. return error;
  281. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  282. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  283. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  284. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  285. error = gfs2_glock_nq(ghs); /* parent */
  286. if (error)
  287. goto out_parent;
  288. error = gfs2_glock_nq(ghs + 1); /* child */
  289. if (error)
  290. goto out_child;
  291. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  292. if (error)
  293. goto out_rgrp;
  294. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  295. if (error)
  296. goto out_gunlock;
  297. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  298. if (error)
  299. goto out_gunlock;
  300. error = gfs2_dir_del(dip, &dentry->d_name);
  301. if (error)
  302. goto out_end_trans;
  303. error = gfs2_change_nlink(ip, -1);
  304. out_end_trans:
  305. gfs2_trans_end(sdp);
  306. out_gunlock:
  307. gfs2_glock_dq(ghs + 2);
  308. out_rgrp:
  309. gfs2_holder_uninit(ghs + 2);
  310. gfs2_glock_dq(ghs + 1);
  311. out_child:
  312. gfs2_holder_uninit(ghs + 1);
  313. gfs2_glock_dq(ghs);
  314. out_parent:
  315. gfs2_holder_uninit(ghs);
  316. gfs2_glock_dq_uninit(&ri_gh);
  317. return error;
  318. }
  319. /**
  320. * gfs2_symlink - Create a symlink
  321. * @dir: The directory to create the symlink in
  322. * @dentry: The dentry to put the symlink in
  323. * @symname: The thing which the link points to
  324. *
  325. * Returns: errno
  326. */
  327. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  328. const char *symname)
  329. {
  330. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  331. struct gfs2_sbd *sdp = GFS2_SB(dir);
  332. struct gfs2_holder ghs[2];
  333. struct inode *inode;
  334. struct buffer_head *dibh;
  335. int size;
  336. int error;
  337. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  338. size = strlen(symname);
  339. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  340. return -ENAMETOOLONG;
  341. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  342. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  343. if (IS_ERR(inode)) {
  344. gfs2_holder_uninit(ghs);
  345. return PTR_ERR(inode);
  346. }
  347. ip = ghs[1].gh_gl->gl_object;
  348. i_size_write(inode, size);
  349. error = gfs2_meta_inode_buffer(ip, &dibh);
  350. if (!gfs2_assert_withdraw(sdp, !error)) {
  351. gfs2_dinode_out(ip, dibh->b_data);
  352. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  353. size);
  354. brelse(dibh);
  355. }
  356. gfs2_trans_end(sdp);
  357. if (dip->i_alloc->al_rgd)
  358. gfs2_inplace_release(dip);
  359. gfs2_quota_unlock(dip);
  360. gfs2_alloc_put(dip);
  361. gfs2_glock_dq_uninit_m(2, ghs);
  362. d_instantiate(dentry, inode);
  363. mark_inode_dirty(inode);
  364. return 0;
  365. }
  366. /**
  367. * gfs2_mkdir - Make a directory
  368. * @dir: The parent directory of the new one
  369. * @dentry: The dentry of the new directory
  370. * @mode: The mode of the new directory
  371. *
  372. * Returns: errno
  373. */
  374. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  375. {
  376. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  377. struct gfs2_sbd *sdp = GFS2_SB(dir);
  378. struct gfs2_holder ghs[2];
  379. struct inode *inode;
  380. struct buffer_head *dibh;
  381. int error;
  382. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  383. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  384. if (IS_ERR(inode)) {
  385. gfs2_holder_uninit(ghs);
  386. return PTR_ERR(inode);
  387. }
  388. ip = ghs[1].gh_gl->gl_object;
  389. ip->i_inode.i_nlink = 2;
  390. i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  391. ip->i_diskflags |= GFS2_DIF_JDATA;
  392. ip->i_entries = 2;
  393. error = gfs2_meta_inode_buffer(ip, &dibh);
  394. if (!gfs2_assert_withdraw(sdp, !error)) {
  395. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  396. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  397. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  398. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  399. dent->de_inum = di->di_num; /* already GFS2 endian */
  400. dent->de_type = cpu_to_be16(DT_DIR);
  401. di->di_entries = cpu_to_be32(1);
  402. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  403. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  404. gfs2_inum_out(dip, dent);
  405. dent->de_type = cpu_to_be16(DT_DIR);
  406. gfs2_dinode_out(ip, di);
  407. brelse(dibh);
  408. }
  409. error = gfs2_change_nlink(dip, +1);
  410. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  411. gfs2_trans_end(sdp);
  412. if (dip->i_alloc->al_rgd)
  413. gfs2_inplace_release(dip);
  414. gfs2_quota_unlock(dip);
  415. gfs2_alloc_put(dip);
  416. gfs2_glock_dq_uninit_m(2, ghs);
  417. d_instantiate(dentry, inode);
  418. mark_inode_dirty(inode);
  419. return 0;
  420. }
  421. /**
  422. * gfs2_rmdiri - Remove a directory
  423. * @dip: The parent directory of the directory to be removed
  424. * @name: The name of the directory to be removed
  425. * @ip: The GFS2 inode of the directory to be removed
  426. *
  427. * Assumes Glocks on dip and ip are held
  428. *
  429. * Returns: errno
  430. */
  431. static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
  432. struct gfs2_inode *ip)
  433. {
  434. int error;
  435. if (ip->i_entries != 2) {
  436. if (gfs2_consist_inode(ip))
  437. gfs2_dinode_print(ip);
  438. return -EIO;
  439. }
  440. error = gfs2_dir_del(dip, name);
  441. if (error)
  442. return error;
  443. error = gfs2_change_nlink(dip, -1);
  444. if (error)
  445. return error;
  446. error = gfs2_dir_del(ip, &gfs2_qdot);
  447. if (error)
  448. return error;
  449. error = gfs2_dir_del(ip, &gfs2_qdotdot);
  450. if (error)
  451. return error;
  452. /* It looks odd, but it really should be done twice */
  453. error = gfs2_change_nlink(ip, -1);
  454. if (error)
  455. return error;
  456. error = gfs2_change_nlink(ip, -1);
  457. if (error)
  458. return error;
  459. return error;
  460. }
  461. /**
  462. * gfs2_rmdir - Remove a directory
  463. * @dir: The parent directory of the directory to be removed
  464. * @dentry: The dentry of the directory to remove
  465. *
  466. * Remove a directory. Call gfs2_rmdiri()
  467. *
  468. * Returns: errno
  469. */
  470. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  471. {
  472. struct gfs2_inode *dip = GFS2_I(dir);
  473. struct gfs2_sbd *sdp = GFS2_SB(dir);
  474. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  475. struct gfs2_holder ghs[3];
  476. struct gfs2_rgrpd *rgd;
  477. struct gfs2_holder ri_gh;
  478. int error;
  479. error = gfs2_rindex_hold(sdp, &ri_gh);
  480. if (error)
  481. return error;
  482. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  483. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  484. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  485. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  486. error = gfs2_glock_nq(ghs); /* parent */
  487. if (error)
  488. goto out_parent;
  489. error = gfs2_glock_nq(ghs + 1); /* child */
  490. if (error)
  491. goto out_child;
  492. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  493. if (error)
  494. goto out_rgrp;
  495. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  496. if (error)
  497. goto out_gunlock;
  498. if (ip->i_entries < 2) {
  499. if (gfs2_consist_inode(ip))
  500. gfs2_dinode_print(ip);
  501. error = -EIO;
  502. goto out_gunlock;
  503. }
  504. if (ip->i_entries > 2) {
  505. error = -ENOTEMPTY;
  506. goto out_gunlock;
  507. }
  508. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  509. if (error)
  510. goto out_gunlock;
  511. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  512. gfs2_trans_end(sdp);
  513. out_gunlock:
  514. gfs2_glock_dq(ghs + 2);
  515. out_rgrp:
  516. gfs2_holder_uninit(ghs + 2);
  517. gfs2_glock_dq(ghs + 1);
  518. out_child:
  519. gfs2_holder_uninit(ghs + 1);
  520. gfs2_glock_dq(ghs);
  521. out_parent:
  522. gfs2_holder_uninit(ghs);
  523. gfs2_glock_dq_uninit(&ri_gh);
  524. return error;
  525. }
  526. /**
  527. * gfs2_mknod - Make a special file
  528. * @dir: The directory in which the special file will reside
  529. * @dentry: The dentry of the special file
  530. * @mode: The mode of the special file
  531. * @rdev: The device specification of the special file
  532. *
  533. */
  534. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  535. dev_t dev)
  536. {
  537. struct gfs2_inode *dip = GFS2_I(dir);
  538. struct gfs2_sbd *sdp = GFS2_SB(dir);
  539. struct gfs2_holder ghs[2];
  540. struct inode *inode;
  541. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  542. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  543. if (IS_ERR(inode)) {
  544. gfs2_holder_uninit(ghs);
  545. return PTR_ERR(inode);
  546. }
  547. gfs2_trans_end(sdp);
  548. if (dip->i_alloc->al_rgd)
  549. gfs2_inplace_release(dip);
  550. gfs2_quota_unlock(dip);
  551. gfs2_alloc_put(dip);
  552. gfs2_glock_dq_uninit_m(2, ghs);
  553. d_instantiate(dentry, inode);
  554. mark_inode_dirty(inode);
  555. return 0;
  556. }
  557. /*
  558. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  559. * @this: move this
  560. * @to: to here
  561. *
  562. * Follow @to back to the root and make sure we don't encounter @this
  563. * Assumes we already hold the rename lock.
  564. *
  565. * Returns: errno
  566. */
  567. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  568. {
  569. struct inode *dir = &to->i_inode;
  570. struct super_block *sb = dir->i_sb;
  571. struct inode *tmp;
  572. int error = 0;
  573. igrab(dir);
  574. for (;;) {
  575. if (dir == &this->i_inode) {
  576. error = -EINVAL;
  577. break;
  578. }
  579. if (dir == sb->s_root->d_inode) {
  580. error = 0;
  581. break;
  582. }
  583. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  584. if (IS_ERR(tmp)) {
  585. error = PTR_ERR(tmp);
  586. break;
  587. }
  588. iput(dir);
  589. dir = tmp;
  590. }
  591. iput(dir);
  592. return error;
  593. }
  594. /**
  595. * gfs2_rename - Rename a file
  596. * @odir: Parent directory of old file name
  597. * @odentry: The old dentry of the file
  598. * @ndir: Parent directory of new file name
  599. * @ndentry: The new dentry of the file
  600. *
  601. * Returns: errno
  602. */
  603. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  604. struct inode *ndir, struct dentry *ndentry)
  605. {
  606. struct gfs2_inode *odip = GFS2_I(odir);
  607. struct gfs2_inode *ndip = GFS2_I(ndir);
  608. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  609. struct gfs2_inode *nip = NULL;
  610. struct gfs2_sbd *sdp = GFS2_SB(odir);
  611. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
  612. struct gfs2_rgrpd *nrgd;
  613. unsigned int num_gh;
  614. int dir_rename = 0;
  615. int alloc_required = 0;
  616. unsigned int x;
  617. int error;
  618. if (ndentry->d_inode) {
  619. nip = GFS2_I(ndentry->d_inode);
  620. if (ip == nip)
  621. return 0;
  622. }
  623. error = gfs2_rindex_hold(sdp, &ri_gh);
  624. if (error)
  625. return error;
  626. if (odip != ndip) {
  627. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  628. 0, &r_gh);
  629. if (error)
  630. goto out;
  631. if (S_ISDIR(ip->i_inode.i_mode)) {
  632. dir_rename = 1;
  633. /* don't move a dirctory into it's subdir */
  634. error = gfs2_ok_to_move(ip, ndip);
  635. if (error)
  636. goto out_gunlock_r;
  637. }
  638. }
  639. num_gh = 1;
  640. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  641. if (odip != ndip) {
  642. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  643. num_gh++;
  644. }
  645. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  646. num_gh++;
  647. if (nip) {
  648. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  649. num_gh++;
  650. /* grab the resource lock for unlink flag twiddling
  651. * this is the case of the target file already existing
  652. * so we unlink before doing the rename
  653. */
  654. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  655. if (nrgd)
  656. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  657. }
  658. for (x = 0; x < num_gh; x++) {
  659. error = gfs2_glock_nq(ghs + x);
  660. if (error)
  661. goto out_gunlock;
  662. }
  663. /* Check out the old directory */
  664. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  665. if (error)
  666. goto out_gunlock;
  667. /* Check out the new directory */
  668. if (nip) {
  669. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  670. if (error)
  671. goto out_gunlock;
  672. if (S_ISDIR(nip->i_inode.i_mode)) {
  673. if (nip->i_entries < 2) {
  674. if (gfs2_consist_inode(nip))
  675. gfs2_dinode_print(nip);
  676. error = -EIO;
  677. goto out_gunlock;
  678. }
  679. if (nip->i_entries > 2) {
  680. error = -ENOTEMPTY;
  681. goto out_gunlock;
  682. }
  683. }
  684. } else {
  685. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
  686. if (error)
  687. goto out_gunlock;
  688. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  689. switch (error) {
  690. case -ENOENT:
  691. error = 0;
  692. break;
  693. case 0:
  694. error = -EEXIST;
  695. default:
  696. goto out_gunlock;
  697. };
  698. if (odip != ndip) {
  699. if (!ndip->i_inode.i_nlink) {
  700. error = -EINVAL;
  701. goto out_gunlock;
  702. }
  703. if (ndip->i_entries == (u32)-1) {
  704. error = -EFBIG;
  705. goto out_gunlock;
  706. }
  707. if (S_ISDIR(ip->i_inode.i_mode) &&
  708. ndip->i_inode.i_nlink == (u32)-1) {
  709. error = -EMLINK;
  710. goto out_gunlock;
  711. }
  712. }
  713. }
  714. /* Check out the dir to be renamed */
  715. if (dir_rename) {
  716. error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
  717. if (error)
  718. goto out_gunlock;
  719. }
  720. if (nip == NULL)
  721. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  722. error = alloc_required;
  723. if (error < 0)
  724. goto out_gunlock;
  725. error = 0;
  726. if (alloc_required) {
  727. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  728. if (!al) {
  729. error = -ENOMEM;
  730. goto out_gunlock;
  731. }
  732. error = gfs2_quota_lock_check(ndip);
  733. if (error)
  734. goto out_alloc;
  735. al->al_requested = sdp->sd_max_dirres;
  736. error = gfs2_inplace_reserve_ri(ndip);
  737. if (error)
  738. goto out_gunlock_q;
  739. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  740. gfs2_rg_blocks(al) +
  741. 4 * RES_DINODE + 4 * RES_LEAF +
  742. RES_STATFS + RES_QUOTA + 4, 0);
  743. if (error)
  744. goto out_ipreserv;
  745. } else {
  746. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  747. 5 * RES_LEAF + 4, 0);
  748. if (error)
  749. goto out_gunlock;
  750. }
  751. /* Remove the target file, if it exists */
  752. if (nip) {
  753. if (S_ISDIR(nip->i_inode.i_mode))
  754. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  755. else {
  756. error = gfs2_dir_del(ndip, &ndentry->d_name);
  757. if (error)
  758. goto out_end_trans;
  759. error = gfs2_change_nlink(nip, -1);
  760. }
  761. if (error)
  762. goto out_end_trans;
  763. }
  764. if (dir_rename) {
  765. error = gfs2_change_nlink(ndip, +1);
  766. if (error)
  767. goto out_end_trans;
  768. error = gfs2_change_nlink(odip, -1);
  769. if (error)
  770. goto out_end_trans;
  771. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  772. if (error)
  773. goto out_end_trans;
  774. } else {
  775. struct buffer_head *dibh;
  776. error = gfs2_meta_inode_buffer(ip, &dibh);
  777. if (error)
  778. goto out_end_trans;
  779. ip->i_inode.i_ctime = CURRENT_TIME;
  780. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  781. gfs2_dinode_out(ip, dibh->b_data);
  782. brelse(dibh);
  783. }
  784. error = gfs2_dir_del(odip, &odentry->d_name);
  785. if (error)
  786. goto out_end_trans;
  787. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  788. if (error)
  789. goto out_end_trans;
  790. out_end_trans:
  791. gfs2_trans_end(sdp);
  792. out_ipreserv:
  793. if (alloc_required)
  794. gfs2_inplace_release(ndip);
  795. out_gunlock_q:
  796. if (alloc_required)
  797. gfs2_quota_unlock(ndip);
  798. out_alloc:
  799. if (alloc_required)
  800. gfs2_alloc_put(ndip);
  801. out_gunlock:
  802. while (x--) {
  803. gfs2_glock_dq(ghs + x);
  804. gfs2_holder_uninit(ghs + x);
  805. }
  806. out_gunlock_r:
  807. if (r_gh.gh_gl)
  808. gfs2_glock_dq_uninit(&r_gh);
  809. out:
  810. gfs2_glock_dq_uninit(&ri_gh);
  811. return error;
  812. }
  813. /**
  814. * gfs2_follow_link - Follow a symbolic link
  815. * @dentry: The dentry of the link
  816. * @nd: Data that we pass to vfs_follow_link()
  817. *
  818. * This can handle symlinks of any size.
  819. *
  820. * Returns: 0 on success or error code
  821. */
  822. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  823. {
  824. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  825. struct gfs2_holder i_gh;
  826. struct buffer_head *dibh;
  827. unsigned int x, size;
  828. char *buf;
  829. int error;
  830. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  831. error = gfs2_glock_nq(&i_gh);
  832. if (error) {
  833. gfs2_holder_uninit(&i_gh);
  834. nd_set_link(nd, ERR_PTR(error));
  835. return NULL;
  836. }
  837. size = (unsigned int)i_size_read(&ip->i_inode);
  838. if (size == 0) {
  839. gfs2_consist_inode(ip);
  840. buf = ERR_PTR(-EIO);
  841. goto out;
  842. }
  843. error = gfs2_meta_inode_buffer(ip, &dibh);
  844. if (error) {
  845. buf = ERR_PTR(error);
  846. goto out;
  847. }
  848. x = size + 1;
  849. buf = kmalloc(x, GFP_NOFS);
  850. if (!buf)
  851. buf = ERR_PTR(-ENOMEM);
  852. else
  853. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
  854. brelse(dibh);
  855. out:
  856. gfs2_glock_dq_uninit(&i_gh);
  857. nd_set_link(nd, buf);
  858. return NULL;
  859. }
  860. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  861. {
  862. char *s = nd_get_link(nd);
  863. if (!IS_ERR(s))
  864. kfree(s);
  865. }
  866. /**
  867. * gfs2_permission -
  868. * @inode:
  869. * @mask:
  870. * @nd: passed from Linux VFS, ignored by us
  871. *
  872. * This may be called from the VFS directly, or from within GFS2 with the
  873. * inode locked, so we look to see if the glock is already locked and only
  874. * lock the glock if its not already been done.
  875. *
  876. * Returns: errno
  877. */
  878. int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
  879. {
  880. struct gfs2_inode *ip;
  881. struct gfs2_holder i_gh;
  882. int error;
  883. int unlock = 0;
  884. if (flags & IPERM_FLAG_RCU)
  885. return -ECHILD;
  886. ip = GFS2_I(inode);
  887. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  888. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  889. if (error)
  890. return error;
  891. unlock = 1;
  892. }
  893. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  894. error = -EACCES;
  895. else
  896. error = generic_permission(inode, mask, flags, gfs2_check_acl);
  897. if (unlock)
  898. gfs2_glock_dq_uninit(&i_gh);
  899. return error;
  900. }
  901. static int setattr_chown(struct inode *inode, struct iattr *attr)
  902. {
  903. struct gfs2_inode *ip = GFS2_I(inode);
  904. struct gfs2_sbd *sdp = GFS2_SB(inode);
  905. u32 ouid, ogid, nuid, ngid;
  906. int error;
  907. ouid = inode->i_uid;
  908. ogid = inode->i_gid;
  909. nuid = attr->ia_uid;
  910. ngid = attr->ia_gid;
  911. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  912. ouid = nuid = NO_QUOTA_CHANGE;
  913. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  914. ogid = ngid = NO_QUOTA_CHANGE;
  915. if (!gfs2_alloc_get(ip))
  916. return -ENOMEM;
  917. error = gfs2_quota_lock(ip, nuid, ngid);
  918. if (error)
  919. goto out_alloc;
  920. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  921. error = gfs2_quota_check(ip, nuid, ngid);
  922. if (error)
  923. goto out_gunlock_q;
  924. }
  925. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  926. if (error)
  927. goto out_gunlock_q;
  928. error = gfs2_setattr_simple(ip, attr);
  929. if (error)
  930. goto out_end_trans;
  931. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  932. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  933. gfs2_quota_change(ip, -blocks, ouid, ogid);
  934. gfs2_quota_change(ip, blocks, nuid, ngid);
  935. }
  936. out_end_trans:
  937. gfs2_trans_end(sdp);
  938. out_gunlock_q:
  939. gfs2_quota_unlock(ip);
  940. out_alloc:
  941. gfs2_alloc_put(ip);
  942. return error;
  943. }
  944. /**
  945. * gfs2_setattr - Change attributes on an inode
  946. * @dentry: The dentry which is changing
  947. * @attr: The structure describing the change
  948. *
  949. * The VFS layer wants to change one or more of an inodes attributes. Write
  950. * that change out to disk.
  951. *
  952. * Returns: errno
  953. */
  954. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  955. {
  956. struct inode *inode = dentry->d_inode;
  957. struct gfs2_inode *ip = GFS2_I(inode);
  958. struct gfs2_holder i_gh;
  959. int error;
  960. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  961. if (error)
  962. return error;
  963. error = -EPERM;
  964. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  965. goto out;
  966. error = inode_change_ok(inode, attr);
  967. if (error)
  968. goto out;
  969. if (attr->ia_valid & ATTR_SIZE)
  970. error = gfs2_setattr_size(inode, attr->ia_size);
  971. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  972. error = setattr_chown(inode, attr);
  973. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  974. error = gfs2_acl_chmod(ip, attr);
  975. else
  976. error = gfs2_setattr_simple(ip, attr);
  977. out:
  978. gfs2_glock_dq_uninit(&i_gh);
  979. if (!error)
  980. mark_inode_dirty(inode);
  981. return error;
  982. }
  983. /**
  984. * gfs2_getattr - Read out an inode's attributes
  985. * @mnt: The vfsmount the inode is being accessed from
  986. * @dentry: The dentry to stat
  987. * @stat: The inode's stats
  988. *
  989. * This may be called from the VFS directly, or from within GFS2 with the
  990. * inode locked, so we look to see if the glock is already locked and only
  991. * lock the glock if its not already been done. Note that its the NFS
  992. * readdirplus operation which causes this to be called (from filldir)
  993. * with the glock already held.
  994. *
  995. * Returns: errno
  996. */
  997. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  998. struct kstat *stat)
  999. {
  1000. struct inode *inode = dentry->d_inode;
  1001. struct gfs2_inode *ip = GFS2_I(inode);
  1002. struct gfs2_holder gh;
  1003. int error;
  1004. int unlock = 0;
  1005. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1006. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1007. if (error)
  1008. return error;
  1009. unlock = 1;
  1010. }
  1011. generic_fillattr(inode, stat);
  1012. if (unlock)
  1013. gfs2_glock_dq_uninit(&gh);
  1014. return 0;
  1015. }
  1016. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1017. const void *data, size_t size, int flags)
  1018. {
  1019. struct inode *inode = dentry->d_inode;
  1020. struct gfs2_inode *ip = GFS2_I(inode);
  1021. struct gfs2_holder gh;
  1022. int ret;
  1023. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1024. ret = gfs2_glock_nq(&gh);
  1025. if (ret == 0) {
  1026. ret = generic_setxattr(dentry, name, data, size, flags);
  1027. gfs2_glock_dq(&gh);
  1028. }
  1029. gfs2_holder_uninit(&gh);
  1030. return ret;
  1031. }
  1032. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1033. void *data, size_t size)
  1034. {
  1035. struct inode *inode = dentry->d_inode;
  1036. struct gfs2_inode *ip = GFS2_I(inode);
  1037. struct gfs2_holder gh;
  1038. int ret;
  1039. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1040. ret = gfs2_glock_nq(&gh);
  1041. if (ret == 0) {
  1042. ret = generic_getxattr(dentry, name, data, size);
  1043. gfs2_glock_dq(&gh);
  1044. }
  1045. gfs2_holder_uninit(&gh);
  1046. return ret;
  1047. }
  1048. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1049. {
  1050. struct inode *inode = dentry->d_inode;
  1051. struct gfs2_inode *ip = GFS2_I(inode);
  1052. struct gfs2_holder gh;
  1053. int ret;
  1054. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1055. ret = gfs2_glock_nq(&gh);
  1056. if (ret == 0) {
  1057. ret = generic_removexattr(dentry, name);
  1058. gfs2_glock_dq(&gh);
  1059. }
  1060. gfs2_holder_uninit(&gh);
  1061. return ret;
  1062. }
  1063. static void empty_write_end(struct page *page, unsigned from,
  1064. unsigned to)
  1065. {
  1066. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  1067. page_zero_new_buffers(page, from, to);
  1068. flush_dcache_page(page);
  1069. mark_page_accessed(page);
  1070. if (!gfs2_is_writeback(ip))
  1071. gfs2_page_add_databufs(ip, page, from, to);
  1072. block_commit_write(page, from, to);
  1073. }
  1074. static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
  1075. {
  1076. unsigned start, end, next;
  1077. struct buffer_head *bh, *head;
  1078. int error;
  1079. if (!page_has_buffers(page)) {
  1080. error = __block_write_begin(page, from, to - from, gfs2_block_map);
  1081. if (unlikely(error))
  1082. return error;
  1083. empty_write_end(page, from, to);
  1084. return 0;
  1085. }
  1086. bh = head = page_buffers(page);
  1087. next = end = 0;
  1088. while (next < from) {
  1089. next += bh->b_size;
  1090. bh = bh->b_this_page;
  1091. }
  1092. start = next;
  1093. do {
  1094. next += bh->b_size;
  1095. if (buffer_mapped(bh)) {
  1096. if (end) {
  1097. error = __block_write_begin(page, start, end - start,
  1098. gfs2_block_map);
  1099. if (unlikely(error))
  1100. return error;
  1101. empty_write_end(page, start, end);
  1102. end = 0;
  1103. }
  1104. start = next;
  1105. }
  1106. else
  1107. end = next;
  1108. bh = bh->b_this_page;
  1109. } while (next < to);
  1110. if (end) {
  1111. error = __block_write_begin(page, start, end - start, gfs2_block_map);
  1112. if (unlikely(error))
  1113. return error;
  1114. empty_write_end(page, start, end);
  1115. }
  1116. return 0;
  1117. }
  1118. static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
  1119. int mode)
  1120. {
  1121. struct gfs2_inode *ip = GFS2_I(inode);
  1122. struct buffer_head *dibh;
  1123. int error;
  1124. u64 start = offset >> PAGE_CACHE_SHIFT;
  1125. unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
  1126. u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
  1127. pgoff_t curr;
  1128. struct page *page;
  1129. unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
  1130. unsigned int from, to;
  1131. if (!end_offset)
  1132. end_offset = PAGE_CACHE_SIZE;
  1133. error = gfs2_meta_inode_buffer(ip, &dibh);
  1134. if (unlikely(error))
  1135. goto out;
  1136. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1137. if (gfs2_is_stuffed(ip)) {
  1138. error = gfs2_unstuff_dinode(ip, NULL);
  1139. if (unlikely(error))
  1140. goto out;
  1141. }
  1142. curr = start;
  1143. offset = start << PAGE_CACHE_SHIFT;
  1144. from = start_offset;
  1145. to = PAGE_CACHE_SIZE;
  1146. while (curr <= end) {
  1147. page = grab_cache_page_write_begin(inode->i_mapping, curr,
  1148. AOP_FLAG_NOFS);
  1149. if (unlikely(!page)) {
  1150. error = -ENOMEM;
  1151. goto out;
  1152. }
  1153. if (curr == end)
  1154. to = end_offset;
  1155. error = write_empty_blocks(page, from, to);
  1156. if (!error && offset + to > inode->i_size &&
  1157. !(mode & FALLOC_FL_KEEP_SIZE)) {
  1158. i_size_write(inode, offset + to);
  1159. }
  1160. unlock_page(page);
  1161. page_cache_release(page);
  1162. if (error)
  1163. goto out;
  1164. curr++;
  1165. offset += PAGE_CACHE_SIZE;
  1166. from = 0;
  1167. }
  1168. gfs2_dinode_out(ip, dibh->b_data);
  1169. mark_inode_dirty(inode);
  1170. brelse(dibh);
  1171. out:
  1172. return error;
  1173. }
  1174. static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
  1175. unsigned int *data_blocks, unsigned int *ind_blocks)
  1176. {
  1177. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1178. unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
  1179. unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
  1180. for (tmp = max_data; tmp > sdp->sd_diptrs;) {
  1181. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  1182. max_data -= tmp;
  1183. }
  1184. /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
  1185. so it might end up with fewer data blocks */
  1186. if (max_data <= *data_blocks)
  1187. return;
  1188. *data_blocks = max_data;
  1189. *ind_blocks = max_blocks - max_data;
  1190. *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
  1191. if (*len > max) {
  1192. *len = max;
  1193. gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
  1194. }
  1195. }
  1196. static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1197. loff_t len)
  1198. {
  1199. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1200. struct gfs2_inode *ip = GFS2_I(inode);
  1201. unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
  1202. loff_t bytes, max_bytes;
  1203. struct gfs2_alloc *al;
  1204. int error;
  1205. loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
  1206. next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
  1207. /* We only support the FALLOC_FL_KEEP_SIZE mode */
  1208. if (mode & ~FALLOC_FL_KEEP_SIZE)
  1209. return -EOPNOTSUPP;
  1210. offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
  1211. sdp->sd_sb.sb_bsize_shift;
  1212. len = next - offset;
  1213. bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
  1214. if (!bytes)
  1215. bytes = UINT_MAX;
  1216. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  1217. error = gfs2_glock_nq(&ip->i_gh);
  1218. if (unlikely(error))
  1219. goto out_uninit;
  1220. if (!gfs2_write_alloc_required(ip, offset, len))
  1221. goto out_unlock;
  1222. while (len > 0) {
  1223. if (len < bytes)
  1224. bytes = len;
  1225. al = gfs2_alloc_get(ip);
  1226. if (!al) {
  1227. error = -ENOMEM;
  1228. goto out_unlock;
  1229. }
  1230. error = gfs2_quota_lock_check(ip);
  1231. if (error)
  1232. goto out_alloc_put;
  1233. retry:
  1234. gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
  1235. al->al_requested = data_blocks + ind_blocks;
  1236. error = gfs2_inplace_reserve(ip);
  1237. if (error) {
  1238. if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
  1239. bytes >>= 1;
  1240. goto retry;
  1241. }
  1242. goto out_qunlock;
  1243. }
  1244. max_bytes = bytes;
  1245. calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
  1246. al->al_requested = data_blocks + ind_blocks;
  1247. rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
  1248. RES_RG_HDR + gfs2_rg_blocks(al);
  1249. if (gfs2_is_jdata(ip))
  1250. rblocks += data_blocks ? data_blocks : 1;
  1251. error = gfs2_trans_begin(sdp, rblocks,
  1252. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  1253. if (error)
  1254. goto out_trans_fail;
  1255. error = fallocate_chunk(inode, offset, max_bytes, mode);
  1256. gfs2_trans_end(sdp);
  1257. if (error)
  1258. goto out_trans_fail;
  1259. len -= max_bytes;
  1260. offset += max_bytes;
  1261. gfs2_inplace_release(ip);
  1262. gfs2_quota_unlock(ip);
  1263. gfs2_alloc_put(ip);
  1264. }
  1265. goto out_unlock;
  1266. out_trans_fail:
  1267. gfs2_inplace_release(ip);
  1268. out_qunlock:
  1269. gfs2_quota_unlock(ip);
  1270. out_alloc_put:
  1271. gfs2_alloc_put(ip);
  1272. out_unlock:
  1273. gfs2_glock_dq(&ip->i_gh);
  1274. out_uninit:
  1275. gfs2_holder_uninit(&ip->i_gh);
  1276. return error;
  1277. }
  1278. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1279. u64 start, u64 len)
  1280. {
  1281. struct gfs2_inode *ip = GFS2_I(inode);
  1282. struct gfs2_holder gh;
  1283. int ret;
  1284. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1285. if (ret)
  1286. return ret;
  1287. mutex_lock(&inode->i_mutex);
  1288. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1289. if (ret)
  1290. goto out;
  1291. if (gfs2_is_stuffed(ip)) {
  1292. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1293. u64 size = i_size_read(inode);
  1294. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1295. FIEMAP_EXTENT_DATA_INLINE;
  1296. phys += sizeof(struct gfs2_dinode);
  1297. phys += start;
  1298. if (start + len > size)
  1299. len = size - start;
  1300. if (start < size)
  1301. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1302. len, flags);
  1303. if (ret == 1)
  1304. ret = 0;
  1305. } else {
  1306. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1307. gfs2_block_map);
  1308. }
  1309. gfs2_glock_dq_uninit(&gh);
  1310. out:
  1311. mutex_unlock(&inode->i_mutex);
  1312. return ret;
  1313. }
  1314. const struct inode_operations gfs2_file_iops = {
  1315. .permission = gfs2_permission,
  1316. .setattr = gfs2_setattr,
  1317. .getattr = gfs2_getattr,
  1318. .setxattr = gfs2_setxattr,
  1319. .getxattr = gfs2_getxattr,
  1320. .listxattr = gfs2_listxattr,
  1321. .removexattr = gfs2_removexattr,
  1322. .fallocate = gfs2_fallocate,
  1323. .fiemap = gfs2_fiemap,
  1324. };
  1325. const struct inode_operations gfs2_dir_iops = {
  1326. .create = gfs2_create,
  1327. .lookup = gfs2_lookup,
  1328. .link = gfs2_link,
  1329. .unlink = gfs2_unlink,
  1330. .symlink = gfs2_symlink,
  1331. .mkdir = gfs2_mkdir,
  1332. .rmdir = gfs2_rmdir,
  1333. .mknod = gfs2_mknod,
  1334. .rename = gfs2_rename,
  1335. .permission = gfs2_permission,
  1336. .setattr = gfs2_setattr,
  1337. .getattr = gfs2_getattr,
  1338. .setxattr = gfs2_setxattr,
  1339. .getxattr = gfs2_getxattr,
  1340. .listxattr = gfs2_listxattr,
  1341. .removexattr = gfs2_removexattr,
  1342. .fiemap = gfs2_fiemap,
  1343. };
  1344. const struct inode_operations gfs2_symlink_iops = {
  1345. .readlink = generic_readlink,
  1346. .follow_link = gfs2_follow_link,
  1347. .put_link = gfs2_put_link,
  1348. .permission = gfs2_permission,
  1349. .setattr = gfs2_setattr,
  1350. .getattr = gfs2_getattr,
  1351. .setxattr = gfs2_setxattr,
  1352. .getxattr = gfs2_getxattr,
  1353. .listxattr = gfs2_listxattr,
  1354. .removexattr = gfs2_removexattr,
  1355. .fiemap = gfs2_fiemap,
  1356. };