ops_inode.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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/utsname.h>
  15. #include <linux/mm.h>
  16. #include <linux/xattr.h>
  17. #include <linux/posix_acl.h>
  18. #include <linux/gfs2_ondisk.h>
  19. #include <linux/crc32.h>
  20. #include <linux/fiemap.h>
  21. #include <asm/uaccess.h>
  22. #include "gfs2.h"
  23. #include "incore.h"
  24. #include "acl.h"
  25. #include "bmap.h"
  26. #include "dir.h"
  27. #include "eaops.h"
  28. #include "eattr.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. al->al_rgd->rd_length +
  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. atomic_inc(&inode->i_count);
  226. d_instantiate(dentry, inode);
  227. mark_inode_dirty(inode);
  228. }
  229. return error;
  230. }
  231. /**
  232. * gfs2_unlink - Unlink a file
  233. * @dir: The inode of the directory containing the file to unlink
  234. * @dentry: The file itself
  235. *
  236. * Unlink a file. Call gfs2_unlinki()
  237. *
  238. * Returns: errno
  239. */
  240. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  241. {
  242. struct gfs2_inode *dip = GFS2_I(dir);
  243. struct gfs2_sbd *sdp = GFS2_SB(dir);
  244. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  245. struct gfs2_holder ghs[3];
  246. struct gfs2_rgrpd *rgd;
  247. struct gfs2_holder ri_gh;
  248. int error;
  249. error = gfs2_rindex_hold(sdp, &ri_gh);
  250. if (error)
  251. return error;
  252. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  253. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  254. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  255. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  256. error = gfs2_glock_nq(ghs); /* parent */
  257. if (error)
  258. goto out_parent;
  259. error = gfs2_glock_nq(ghs + 1); /* child */
  260. if (error)
  261. goto out_child;
  262. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  263. if (error)
  264. goto out_rgrp;
  265. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  266. if (error)
  267. goto out_gunlock;
  268. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  269. if (error)
  270. goto out_rgrp;
  271. error = gfs2_dir_del(dip, &dentry->d_name);
  272. if (error)
  273. goto out_end_trans;
  274. error = gfs2_change_nlink(ip, -1);
  275. out_end_trans:
  276. gfs2_trans_end(sdp);
  277. out_gunlock:
  278. gfs2_glock_dq(ghs + 2);
  279. out_rgrp:
  280. gfs2_holder_uninit(ghs + 2);
  281. gfs2_glock_dq(ghs + 1);
  282. out_child:
  283. gfs2_holder_uninit(ghs + 1);
  284. gfs2_glock_dq(ghs);
  285. out_parent:
  286. gfs2_holder_uninit(ghs);
  287. gfs2_glock_dq_uninit(&ri_gh);
  288. return error;
  289. }
  290. /**
  291. * gfs2_symlink - Create a symlink
  292. * @dir: The directory to create the symlink in
  293. * @dentry: The dentry to put the symlink in
  294. * @symname: The thing which the link points to
  295. *
  296. * Returns: errno
  297. */
  298. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  299. const char *symname)
  300. {
  301. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  302. struct gfs2_sbd *sdp = GFS2_SB(dir);
  303. struct gfs2_holder ghs[2];
  304. struct inode *inode;
  305. struct buffer_head *dibh;
  306. int size;
  307. int error;
  308. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  309. size = strlen(symname);
  310. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  311. return -ENAMETOOLONG;
  312. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  313. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  314. if (IS_ERR(inode)) {
  315. gfs2_holder_uninit(ghs);
  316. return PTR_ERR(inode);
  317. }
  318. ip = ghs[1].gh_gl->gl_object;
  319. ip->i_disksize = size;
  320. error = gfs2_meta_inode_buffer(ip, &dibh);
  321. if (!gfs2_assert_withdraw(sdp, !error)) {
  322. gfs2_dinode_out(ip, dibh->b_data);
  323. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  324. size);
  325. brelse(dibh);
  326. }
  327. gfs2_trans_end(sdp);
  328. if (dip->i_alloc->al_rgd)
  329. gfs2_inplace_release(dip);
  330. gfs2_quota_unlock(dip);
  331. gfs2_alloc_put(dip);
  332. gfs2_glock_dq_uninit_m(2, ghs);
  333. d_instantiate(dentry, inode);
  334. mark_inode_dirty(inode);
  335. return 0;
  336. }
  337. /**
  338. * gfs2_mkdir - Make a directory
  339. * @dir: The parent directory of the new one
  340. * @dentry: The dentry of the new directory
  341. * @mode: The mode of the new directory
  342. *
  343. * Returns: errno
  344. */
  345. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  346. {
  347. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  348. struct gfs2_sbd *sdp = GFS2_SB(dir);
  349. struct gfs2_holder ghs[2];
  350. struct inode *inode;
  351. struct buffer_head *dibh;
  352. int error;
  353. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  354. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  355. if (IS_ERR(inode)) {
  356. gfs2_holder_uninit(ghs);
  357. return PTR_ERR(inode);
  358. }
  359. ip = ghs[1].gh_gl->gl_object;
  360. ip->i_inode.i_nlink = 2;
  361. ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  362. ip->i_diskflags |= GFS2_DIF_JDATA;
  363. ip->i_entries = 2;
  364. error = gfs2_meta_inode_buffer(ip, &dibh);
  365. if (!gfs2_assert_withdraw(sdp, !error)) {
  366. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  367. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  368. struct qstr str;
  369. gfs2_str2qstr(&str, ".");
  370. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  371. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  372. dent->de_inum = di->di_num; /* already GFS2 endian */
  373. dent->de_type = cpu_to_be16(DT_DIR);
  374. di->di_entries = cpu_to_be32(1);
  375. gfs2_str2qstr(&str, "..");
  376. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  377. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  378. gfs2_inum_out(dip, dent);
  379. dent->de_type = cpu_to_be16(DT_DIR);
  380. gfs2_dinode_out(ip, di);
  381. brelse(dibh);
  382. }
  383. error = gfs2_change_nlink(dip, +1);
  384. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  385. gfs2_trans_end(sdp);
  386. if (dip->i_alloc->al_rgd)
  387. gfs2_inplace_release(dip);
  388. gfs2_quota_unlock(dip);
  389. gfs2_alloc_put(dip);
  390. gfs2_glock_dq_uninit_m(2, ghs);
  391. d_instantiate(dentry, inode);
  392. mark_inode_dirty(inode);
  393. return 0;
  394. }
  395. /**
  396. * gfs2_rmdir - Remove a directory
  397. * @dir: The parent directory of the directory to be removed
  398. * @dentry: The dentry of the directory to remove
  399. *
  400. * Remove a directory. Call gfs2_rmdiri()
  401. *
  402. * Returns: errno
  403. */
  404. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  405. {
  406. struct gfs2_inode *dip = GFS2_I(dir);
  407. struct gfs2_sbd *sdp = GFS2_SB(dir);
  408. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  409. struct gfs2_holder ghs[3];
  410. struct gfs2_rgrpd *rgd;
  411. struct gfs2_holder ri_gh;
  412. int error;
  413. error = gfs2_rindex_hold(sdp, &ri_gh);
  414. if (error)
  415. return error;
  416. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  417. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  418. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  419. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  420. error = gfs2_glock_nq(ghs); /* parent */
  421. if (error)
  422. goto out_parent;
  423. error = gfs2_glock_nq(ghs + 1); /* child */
  424. if (error)
  425. goto out_child;
  426. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  427. if (error)
  428. goto out_rgrp;
  429. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  430. if (error)
  431. goto out_gunlock;
  432. if (ip->i_entries < 2) {
  433. if (gfs2_consist_inode(ip))
  434. gfs2_dinode_print(ip);
  435. error = -EIO;
  436. goto out_gunlock;
  437. }
  438. if (ip->i_entries > 2) {
  439. error = -ENOTEMPTY;
  440. goto out_gunlock;
  441. }
  442. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  443. if (error)
  444. goto out_gunlock;
  445. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  446. gfs2_trans_end(sdp);
  447. out_gunlock:
  448. gfs2_glock_dq(ghs + 2);
  449. out_rgrp:
  450. gfs2_holder_uninit(ghs + 2);
  451. gfs2_glock_dq(ghs + 1);
  452. out_child:
  453. gfs2_holder_uninit(ghs + 1);
  454. gfs2_glock_dq(ghs);
  455. out_parent:
  456. gfs2_holder_uninit(ghs);
  457. gfs2_glock_dq_uninit(&ri_gh);
  458. return error;
  459. }
  460. /**
  461. * gfs2_mknod - Make a special file
  462. * @dir: The directory in which the special file will reside
  463. * @dentry: The dentry of the special file
  464. * @mode: The mode of the special file
  465. * @rdev: The device specification of the special file
  466. *
  467. */
  468. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  469. dev_t dev)
  470. {
  471. struct gfs2_inode *dip = GFS2_I(dir);
  472. struct gfs2_sbd *sdp = GFS2_SB(dir);
  473. struct gfs2_holder ghs[2];
  474. struct inode *inode;
  475. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  476. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  477. if (IS_ERR(inode)) {
  478. gfs2_holder_uninit(ghs);
  479. return PTR_ERR(inode);
  480. }
  481. gfs2_trans_end(sdp);
  482. if (dip->i_alloc->al_rgd)
  483. gfs2_inplace_release(dip);
  484. gfs2_quota_unlock(dip);
  485. gfs2_alloc_put(dip);
  486. gfs2_glock_dq_uninit_m(2, ghs);
  487. d_instantiate(dentry, inode);
  488. mark_inode_dirty(inode);
  489. return 0;
  490. }
  491. /*
  492. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  493. * @this: move this
  494. * @to: to here
  495. *
  496. * Follow @to back to the root and make sure we don't encounter @this
  497. * Assumes we already hold the rename lock.
  498. *
  499. * Returns: errno
  500. */
  501. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  502. {
  503. struct inode *dir = &to->i_inode;
  504. struct super_block *sb = dir->i_sb;
  505. struct inode *tmp;
  506. struct qstr dotdot;
  507. int error = 0;
  508. gfs2_str2qstr(&dotdot, "..");
  509. igrab(dir);
  510. for (;;) {
  511. if (dir == &this->i_inode) {
  512. error = -EINVAL;
  513. break;
  514. }
  515. if (dir == sb->s_root->d_inode) {
  516. error = 0;
  517. break;
  518. }
  519. tmp = gfs2_lookupi(dir, &dotdot, 1);
  520. if (IS_ERR(tmp)) {
  521. error = PTR_ERR(tmp);
  522. break;
  523. }
  524. iput(dir);
  525. dir = tmp;
  526. }
  527. iput(dir);
  528. return error;
  529. }
  530. /**
  531. * gfs2_rename - Rename a file
  532. * @odir: Parent directory of old file name
  533. * @odentry: The old dentry of the file
  534. * @ndir: Parent directory of new file name
  535. * @ndentry: The new dentry of the file
  536. *
  537. * Returns: errno
  538. */
  539. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  540. struct inode *ndir, struct dentry *ndentry)
  541. {
  542. struct gfs2_inode *odip = GFS2_I(odir);
  543. struct gfs2_inode *ndip = GFS2_I(ndir);
  544. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  545. struct gfs2_inode *nip = NULL;
  546. struct gfs2_sbd *sdp = GFS2_SB(odir);
  547. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  548. struct gfs2_rgrpd *nrgd;
  549. unsigned int num_gh;
  550. int dir_rename = 0;
  551. int alloc_required;
  552. unsigned int x;
  553. int error;
  554. if (ndentry->d_inode) {
  555. nip = GFS2_I(ndentry->d_inode);
  556. if (ip == nip)
  557. return 0;
  558. }
  559. if (odip != ndip) {
  560. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  561. 0, &r_gh);
  562. if (error)
  563. goto out;
  564. if (S_ISDIR(ip->i_inode.i_mode)) {
  565. dir_rename = 1;
  566. /* don't move a dirctory into it's subdir */
  567. error = gfs2_ok_to_move(ip, ndip);
  568. if (error)
  569. goto out_gunlock_r;
  570. }
  571. }
  572. num_gh = 1;
  573. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  574. if (odip != ndip) {
  575. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  576. num_gh++;
  577. }
  578. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  579. num_gh++;
  580. if (nip) {
  581. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  582. num_gh++;
  583. /* grab the resource lock for unlink flag twiddling
  584. * this is the case of the target file already existing
  585. * so we unlink before doing the rename
  586. */
  587. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  588. if (nrgd)
  589. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  590. }
  591. for (x = 0; x < num_gh; x++) {
  592. error = gfs2_glock_nq(ghs + x);
  593. if (error)
  594. goto out_gunlock;
  595. }
  596. /* Check out the old directory */
  597. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  598. if (error)
  599. goto out_gunlock;
  600. /* Check out the new directory */
  601. if (nip) {
  602. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  603. if (error)
  604. goto out_gunlock;
  605. if (S_ISDIR(nip->i_inode.i_mode)) {
  606. if (nip->i_entries < 2) {
  607. if (gfs2_consist_inode(nip))
  608. gfs2_dinode_print(nip);
  609. error = -EIO;
  610. goto out_gunlock;
  611. }
  612. if (nip->i_entries > 2) {
  613. error = -ENOTEMPTY;
  614. goto out_gunlock;
  615. }
  616. }
  617. } else {
  618. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  619. if (error)
  620. goto out_gunlock;
  621. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  622. switch (error) {
  623. case -ENOENT:
  624. error = 0;
  625. break;
  626. case 0:
  627. error = -EEXIST;
  628. default:
  629. goto out_gunlock;
  630. };
  631. if (odip != ndip) {
  632. if (!ndip->i_inode.i_nlink) {
  633. error = -EINVAL;
  634. goto out_gunlock;
  635. }
  636. if (ndip->i_entries == (u32)-1) {
  637. error = -EFBIG;
  638. goto out_gunlock;
  639. }
  640. if (S_ISDIR(ip->i_inode.i_mode) &&
  641. ndip->i_inode.i_nlink == (u32)-1) {
  642. error = -EMLINK;
  643. goto out_gunlock;
  644. }
  645. }
  646. }
  647. /* Check out the dir to be renamed */
  648. if (dir_rename) {
  649. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  650. if (error)
  651. goto out_gunlock;
  652. }
  653. alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  654. if (error < 0)
  655. goto out_gunlock;
  656. error = 0;
  657. if (alloc_required) {
  658. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  659. if (!al) {
  660. error = -ENOMEM;
  661. goto out_gunlock;
  662. }
  663. error = gfs2_quota_lock_check(ndip);
  664. if (error)
  665. goto out_alloc;
  666. al->al_requested = sdp->sd_max_dirres;
  667. error = gfs2_inplace_reserve(ndip);
  668. if (error)
  669. goto out_gunlock_q;
  670. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  671. al->al_rgd->rd_length +
  672. 4 * RES_DINODE + 4 * RES_LEAF +
  673. RES_STATFS + RES_QUOTA + 4, 0);
  674. if (error)
  675. goto out_ipreserv;
  676. } else {
  677. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  678. 5 * RES_LEAF + 4, 0);
  679. if (error)
  680. goto out_gunlock;
  681. }
  682. /* Remove the target file, if it exists */
  683. if (nip) {
  684. if (S_ISDIR(nip->i_inode.i_mode))
  685. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  686. else {
  687. error = gfs2_dir_del(ndip, &ndentry->d_name);
  688. if (error)
  689. goto out_end_trans;
  690. error = gfs2_change_nlink(nip, -1);
  691. }
  692. if (error)
  693. goto out_end_trans;
  694. }
  695. if (dir_rename) {
  696. struct qstr name;
  697. gfs2_str2qstr(&name, "..");
  698. error = gfs2_change_nlink(ndip, +1);
  699. if (error)
  700. goto out_end_trans;
  701. error = gfs2_change_nlink(odip, -1);
  702. if (error)
  703. goto out_end_trans;
  704. error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
  705. if (error)
  706. goto out_end_trans;
  707. } else {
  708. struct buffer_head *dibh;
  709. error = gfs2_meta_inode_buffer(ip, &dibh);
  710. if (error)
  711. goto out_end_trans;
  712. ip->i_inode.i_ctime = CURRENT_TIME;
  713. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  714. gfs2_dinode_out(ip, dibh->b_data);
  715. brelse(dibh);
  716. }
  717. error = gfs2_dir_del(odip, &odentry->d_name);
  718. if (error)
  719. goto out_end_trans;
  720. error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
  721. if (error)
  722. goto out_end_trans;
  723. out_end_trans:
  724. gfs2_trans_end(sdp);
  725. out_ipreserv:
  726. if (alloc_required)
  727. gfs2_inplace_release(ndip);
  728. out_gunlock_q:
  729. if (alloc_required)
  730. gfs2_quota_unlock(ndip);
  731. out_alloc:
  732. if (alloc_required)
  733. gfs2_alloc_put(ndip);
  734. out_gunlock:
  735. while (x--) {
  736. gfs2_glock_dq(ghs + x);
  737. gfs2_holder_uninit(ghs + x);
  738. }
  739. out_gunlock_r:
  740. if (r_gh.gh_gl)
  741. gfs2_glock_dq_uninit(&r_gh);
  742. out:
  743. return error;
  744. }
  745. /**
  746. * gfs2_readlink - Read the value of a symlink
  747. * @dentry: the symlink
  748. * @buf: the buffer to read the symlink data into
  749. * @size: the size of the buffer
  750. *
  751. * Returns: errno
  752. */
  753. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  754. int user_size)
  755. {
  756. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  757. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  758. unsigned int len = GFS2_FAST_NAME_SIZE;
  759. int error;
  760. error = gfs2_readlinki(ip, &buf, &len);
  761. if (error)
  762. return error;
  763. if (user_size > len - 1)
  764. user_size = len - 1;
  765. if (copy_to_user(user_buf, buf, user_size))
  766. error = -EFAULT;
  767. else
  768. error = user_size;
  769. if (buf != array)
  770. kfree(buf);
  771. return error;
  772. }
  773. /**
  774. * gfs2_follow_link - Follow a symbolic link
  775. * @dentry: The dentry of the link
  776. * @nd: Data that we pass to vfs_follow_link()
  777. *
  778. * This can handle symlinks of any size. It is optimised for symlinks
  779. * under GFS2_FAST_NAME_SIZE.
  780. *
  781. * Returns: 0 on success or error code
  782. */
  783. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  784. {
  785. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  786. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  787. unsigned int len = GFS2_FAST_NAME_SIZE;
  788. int error;
  789. error = gfs2_readlinki(ip, &buf, &len);
  790. if (!error) {
  791. error = vfs_follow_link(nd, buf);
  792. if (buf != array)
  793. kfree(buf);
  794. }
  795. return ERR_PTR(error);
  796. }
  797. /**
  798. * gfs2_permission -
  799. * @inode:
  800. * @mask:
  801. * @nd: passed from Linux VFS, ignored by us
  802. *
  803. * This may be called from the VFS directly, or from within GFS2 with the
  804. * inode locked, so we look to see if the glock is already locked and only
  805. * lock the glock if its not already been done.
  806. *
  807. * Returns: errno
  808. */
  809. int gfs2_permission(struct inode *inode, int mask)
  810. {
  811. struct gfs2_inode *ip = GFS2_I(inode);
  812. struct gfs2_holder i_gh;
  813. int error;
  814. int unlock = 0;
  815. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  816. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  817. if (error)
  818. return error;
  819. unlock = 1;
  820. }
  821. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  822. error = -EACCES;
  823. else
  824. error = generic_permission(inode, mask, gfs2_check_acl);
  825. if (unlock)
  826. gfs2_glock_dq_uninit(&i_gh);
  827. return error;
  828. }
  829. static int setattr_size(struct inode *inode, struct iattr *attr)
  830. {
  831. struct gfs2_inode *ip = GFS2_I(inode);
  832. struct gfs2_sbd *sdp = GFS2_SB(inode);
  833. int error;
  834. if (attr->ia_size != ip->i_disksize) {
  835. error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
  836. if (error)
  837. return error;
  838. error = vmtruncate(inode, attr->ia_size);
  839. gfs2_trans_end(sdp);
  840. if (error)
  841. return error;
  842. }
  843. error = gfs2_truncatei(ip, attr->ia_size);
  844. if (error && (inode->i_size != ip->i_disksize))
  845. i_size_write(inode, ip->i_disksize);
  846. return error;
  847. }
  848. static int setattr_chown(struct inode *inode, struct iattr *attr)
  849. {
  850. struct gfs2_inode *ip = GFS2_I(inode);
  851. struct gfs2_sbd *sdp = GFS2_SB(inode);
  852. struct buffer_head *dibh;
  853. u32 ouid, ogid, nuid, ngid;
  854. int error;
  855. ouid = inode->i_uid;
  856. ogid = inode->i_gid;
  857. nuid = attr->ia_uid;
  858. ngid = attr->ia_gid;
  859. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  860. ouid = nuid = NO_QUOTA_CHANGE;
  861. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  862. ogid = ngid = NO_QUOTA_CHANGE;
  863. if (!gfs2_alloc_get(ip))
  864. return -ENOMEM;
  865. error = gfs2_quota_lock(ip, nuid, ngid);
  866. if (error)
  867. goto out_alloc;
  868. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  869. error = gfs2_quota_check(ip, nuid, ngid);
  870. if (error)
  871. goto out_gunlock_q;
  872. }
  873. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  874. if (error)
  875. goto out_gunlock_q;
  876. error = gfs2_meta_inode_buffer(ip, &dibh);
  877. if (error)
  878. goto out_end_trans;
  879. error = inode_setattr(inode, attr);
  880. gfs2_assert_warn(sdp, !error);
  881. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  882. gfs2_dinode_out(ip, dibh->b_data);
  883. brelse(dibh);
  884. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  885. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  886. gfs2_quota_change(ip, -blocks, ouid, ogid);
  887. gfs2_quota_change(ip, blocks, nuid, ngid);
  888. }
  889. out_end_trans:
  890. gfs2_trans_end(sdp);
  891. out_gunlock_q:
  892. gfs2_quota_unlock(ip);
  893. out_alloc:
  894. gfs2_alloc_put(ip);
  895. return error;
  896. }
  897. /**
  898. * gfs2_setattr - Change attributes on an inode
  899. * @dentry: The dentry which is changing
  900. * @attr: The structure describing the change
  901. *
  902. * The VFS layer wants to change one or more of an inodes attributes. Write
  903. * that change out to disk.
  904. *
  905. * Returns: errno
  906. */
  907. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  908. {
  909. struct inode *inode = dentry->d_inode;
  910. struct gfs2_inode *ip = GFS2_I(inode);
  911. struct gfs2_holder i_gh;
  912. int error;
  913. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  914. if (error)
  915. return error;
  916. error = -EPERM;
  917. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  918. goto out;
  919. error = inode_change_ok(inode, attr);
  920. if (error)
  921. goto out;
  922. if (attr->ia_valid & ATTR_SIZE)
  923. error = setattr_size(inode, attr);
  924. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  925. error = setattr_chown(inode, attr);
  926. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  927. error = gfs2_acl_chmod(ip, attr);
  928. else
  929. error = gfs2_setattr_simple(ip, attr);
  930. out:
  931. gfs2_glock_dq_uninit(&i_gh);
  932. if (!error)
  933. mark_inode_dirty(inode);
  934. return error;
  935. }
  936. /**
  937. * gfs2_getattr - Read out an inode's attributes
  938. * @mnt: The vfsmount the inode is being accessed from
  939. * @dentry: The dentry to stat
  940. * @stat: The inode's stats
  941. *
  942. * This may be called from the VFS directly, or from within GFS2 with the
  943. * inode locked, so we look to see if the glock is already locked and only
  944. * lock the glock if its not already been done. Note that its the NFS
  945. * readdirplus operation which causes this to be called (from filldir)
  946. * with the glock already held.
  947. *
  948. * Returns: errno
  949. */
  950. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  951. struct kstat *stat)
  952. {
  953. struct inode *inode = dentry->d_inode;
  954. struct gfs2_inode *ip = GFS2_I(inode);
  955. struct gfs2_holder gh;
  956. int error;
  957. int unlock = 0;
  958. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  959. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  960. if (error)
  961. return error;
  962. unlock = 1;
  963. }
  964. generic_fillattr(inode, stat);
  965. if (unlock)
  966. gfs2_glock_dq_uninit(&gh);
  967. return 0;
  968. }
  969. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  970. const void *data, size_t size, int flags)
  971. {
  972. struct inode *inode = dentry->d_inode;
  973. struct gfs2_ea_request er;
  974. memset(&er, 0, sizeof(struct gfs2_ea_request));
  975. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  976. if (er.er_type == GFS2_EATYPE_UNUSED)
  977. return -EOPNOTSUPP;
  978. er.er_data = (char *)data;
  979. er.er_name_len = strlen(er.er_name);
  980. er.er_data_len = size;
  981. er.er_flags = flags;
  982. gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
  983. return gfs2_ea_set(GFS2_I(inode), &er);
  984. }
  985. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  986. void *data, size_t size)
  987. {
  988. struct gfs2_ea_request er;
  989. memset(&er, 0, sizeof(struct gfs2_ea_request));
  990. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  991. if (er.er_type == GFS2_EATYPE_UNUSED)
  992. return -EOPNOTSUPP;
  993. er.er_data = data;
  994. er.er_name_len = strlen(er.er_name);
  995. er.er_data_len = size;
  996. return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
  997. }
  998. static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  999. {
  1000. struct gfs2_ea_request er;
  1001. memset(&er, 0, sizeof(struct gfs2_ea_request));
  1002. er.er_data = (size) ? buffer : NULL;
  1003. er.er_data_len = size;
  1004. return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
  1005. }
  1006. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1007. {
  1008. struct gfs2_ea_request er;
  1009. memset(&er, 0, sizeof(struct gfs2_ea_request));
  1010. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  1011. if (er.er_type == GFS2_EATYPE_UNUSED)
  1012. return -EOPNOTSUPP;
  1013. er.er_name_len = strlen(er.er_name);
  1014. return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
  1015. }
  1016. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1017. u64 start, u64 len)
  1018. {
  1019. struct gfs2_inode *ip = GFS2_I(inode);
  1020. struct gfs2_holder gh;
  1021. int ret;
  1022. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1023. if (ret)
  1024. return ret;
  1025. mutex_lock(&inode->i_mutex);
  1026. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1027. if (ret)
  1028. goto out;
  1029. if (gfs2_is_stuffed(ip)) {
  1030. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1031. u64 size = i_size_read(inode);
  1032. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1033. FIEMAP_EXTENT_DATA_INLINE;
  1034. phys += sizeof(struct gfs2_dinode);
  1035. phys += start;
  1036. if (start + len > size)
  1037. len = size - start;
  1038. if (start < size)
  1039. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1040. len, flags);
  1041. if (ret == 1)
  1042. ret = 0;
  1043. } else {
  1044. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1045. gfs2_block_map);
  1046. }
  1047. gfs2_glock_dq_uninit(&gh);
  1048. out:
  1049. mutex_unlock(&inode->i_mutex);
  1050. return ret;
  1051. }
  1052. const struct inode_operations gfs2_file_iops = {
  1053. .permission = gfs2_permission,
  1054. .setattr = gfs2_setattr,
  1055. .getattr = gfs2_getattr,
  1056. .setxattr = gfs2_setxattr,
  1057. .getxattr = gfs2_getxattr,
  1058. .listxattr = gfs2_listxattr,
  1059. .removexattr = gfs2_removexattr,
  1060. .fiemap = gfs2_fiemap,
  1061. };
  1062. const struct inode_operations gfs2_dir_iops = {
  1063. .create = gfs2_create,
  1064. .lookup = gfs2_lookup,
  1065. .link = gfs2_link,
  1066. .unlink = gfs2_unlink,
  1067. .symlink = gfs2_symlink,
  1068. .mkdir = gfs2_mkdir,
  1069. .rmdir = gfs2_rmdir,
  1070. .mknod = gfs2_mknod,
  1071. .rename = gfs2_rename,
  1072. .permission = gfs2_permission,
  1073. .setattr = gfs2_setattr,
  1074. .getattr = gfs2_getattr,
  1075. .setxattr = gfs2_setxattr,
  1076. .getxattr = gfs2_getxattr,
  1077. .listxattr = gfs2_listxattr,
  1078. .removexattr = gfs2_removexattr,
  1079. .fiemap = gfs2_fiemap,
  1080. };
  1081. const struct inode_operations gfs2_symlink_iops = {
  1082. .readlink = gfs2_readlink,
  1083. .follow_link = gfs2_follow_link,
  1084. .permission = gfs2_permission,
  1085. .setattr = gfs2_setattr,
  1086. .getattr = gfs2_getattr,
  1087. .setxattr = gfs2_setxattr,
  1088. .getxattr = gfs2_getxattr,
  1089. .listxattr = gfs2_listxattr,
  1090. .removexattr = gfs2_removexattr,
  1091. .fiemap = gfs2_fiemap,
  1092. };