ops_inode.c 36 KB

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