ops_inode.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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/lm_interface.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 "ops_dentry.h"
  33. #include "ops_inode.h"
  34. #include "quota.h"
  35. #include "rgrp.h"
  36. #include "trans.h"
  37. #include "util.h"
  38. /**
  39. * gfs2_create - Create a file
  40. * @dir: The directory in which to create the file
  41. * @dentry: The dentry of the new file
  42. * @mode: The mode of the new file
  43. *
  44. * Returns: errno
  45. */
  46. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  47. int mode, struct nameidata *nd)
  48. {
  49. struct gfs2_inode *dip = GFS2_I(dir);
  50. struct gfs2_sbd *sdp = GFS2_SB(dir);
  51. struct gfs2_holder ghs[2];
  52. struct inode *inode;
  53. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  54. for (;;) {
  55. inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
  56. if (!IS_ERR(inode)) {
  57. gfs2_trans_end(sdp);
  58. if (dip->i_alloc.al_rgd)
  59. gfs2_inplace_release(dip);
  60. gfs2_quota_unlock(dip);
  61. gfs2_alloc_put(dip);
  62. gfs2_glock_dq_uninit_m(2, ghs);
  63. mark_inode_dirty(inode);
  64. break;
  65. } else if (PTR_ERR(inode) != -EEXIST ||
  66. (nd->intent.open.flags & O_EXCL)) {
  67. gfs2_holder_uninit(ghs);
  68. return PTR_ERR(inode);
  69. }
  70. inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
  71. if (inode) {
  72. if (!IS_ERR(inode)) {
  73. gfs2_holder_uninit(ghs);
  74. break;
  75. } else {
  76. gfs2_holder_uninit(ghs);
  77. return PTR_ERR(inode);
  78. }
  79. }
  80. }
  81. d_instantiate(dentry, inode);
  82. return 0;
  83. }
  84. /**
  85. * gfs2_lookup - Look up a filename in a directory and return its inode
  86. * @dir: The directory inode
  87. * @dentry: The dentry of the new inode
  88. * @nd: passed from Linux VFS, ignored by us
  89. *
  90. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  91. *
  92. * Returns: errno
  93. */
  94. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  95. struct nameidata *nd)
  96. {
  97. struct inode *inode = NULL;
  98. dentry->d_op = &gfs2_dops;
  99. inode = gfs2_lookupi(dir, &dentry->d_name, 0, nd);
  100. if (inode && IS_ERR(inode))
  101. return ERR_PTR(PTR_ERR(inode));
  102. if (inode)
  103. return d_splice_alias(inode, dentry);
  104. d_add(dentry, inode);
  105. return NULL;
  106. }
  107. /**
  108. * gfs2_link - Link to a file
  109. * @old_dentry: The inode to link
  110. * @dir: Add link to this directory
  111. * @dentry: The name of the link
  112. *
  113. * Link the inode in "old_dentry" into the directory "dir" with the
  114. * name in "dentry".
  115. *
  116. * Returns: errno
  117. */
  118. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  119. struct dentry *dentry)
  120. {
  121. struct gfs2_inode *dip = GFS2_I(dir);
  122. struct gfs2_sbd *sdp = GFS2_SB(dir);
  123. struct inode *inode = old_dentry->d_inode;
  124. struct gfs2_inode *ip = GFS2_I(inode);
  125. struct gfs2_holder ghs[2];
  126. int alloc_required;
  127. int error;
  128. if (S_ISDIR(inode->i_mode))
  129. return -EPERM;
  130. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  131. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  132. error = gfs2_glock_nq_m(2, ghs);
  133. if (error)
  134. goto out;
  135. error = permission(dir, MAY_WRITE | MAY_EXEC, NULL);
  136. if (error)
  137. goto out_gunlock;
  138. error = gfs2_dir_search(dir, &dentry->d_name, NULL, NULL);
  139. switch (error) {
  140. case -ENOENT:
  141. break;
  142. case 0:
  143. error = -EEXIST;
  144. default:
  145. goto out_gunlock;
  146. }
  147. error = -EINVAL;
  148. if (!dip->i_inode.i_nlink)
  149. goto out_gunlock;
  150. error = -EFBIG;
  151. if (dip->i_di.di_entries == (u32)-1)
  152. goto out_gunlock;
  153. error = -EPERM;
  154. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  155. goto out_gunlock;
  156. error = -EINVAL;
  157. if (!ip->i_inode.i_nlink)
  158. goto out_gunlock;
  159. error = -EMLINK;
  160. if (ip->i_inode.i_nlink == (u32)-1)
  161. goto out_gunlock;
  162. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  163. if (error < 0)
  164. goto out_gunlock;
  165. error = 0;
  166. if (alloc_required) {
  167. struct gfs2_alloc *al = gfs2_alloc_get(dip);
  168. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  169. if (error)
  170. goto out_alloc;
  171. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  172. if (error)
  173. goto out_gunlock_q;
  174. al->al_requested = sdp->sd_max_dirres;
  175. error = gfs2_inplace_reserve(dip);
  176. if (error)
  177. goto out_gunlock_q;
  178. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  179. al->al_rgd->rd_ri.ri_length +
  180. 2 * RES_DINODE + RES_STATFS +
  181. RES_QUOTA, 0);
  182. if (error)
  183. goto out_ipres;
  184. } else {
  185. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  186. if (error)
  187. goto out_ipres;
  188. }
  189. error = gfs2_dir_add(dir, &dentry->d_name, &ip->i_num,
  190. IF2DT(inode->i_mode));
  191. if (error)
  192. goto out_end_trans;
  193. error = gfs2_change_nlink(ip, +1);
  194. out_end_trans:
  195. gfs2_trans_end(sdp);
  196. out_ipres:
  197. if (alloc_required)
  198. gfs2_inplace_release(dip);
  199. out_gunlock_q:
  200. if (alloc_required)
  201. gfs2_quota_unlock(dip);
  202. out_alloc:
  203. if (alloc_required)
  204. gfs2_alloc_put(dip);
  205. out_gunlock:
  206. gfs2_glock_dq_m(2, ghs);
  207. out:
  208. gfs2_holder_uninit(ghs);
  209. gfs2_holder_uninit(ghs + 1);
  210. if (!error) {
  211. atomic_inc(&inode->i_count);
  212. d_instantiate(dentry, inode);
  213. mark_inode_dirty(inode);
  214. }
  215. return error;
  216. }
  217. /**
  218. * gfs2_unlink - Unlink a file
  219. * @dir: The inode of the directory containing the file to unlink
  220. * @dentry: The file itself
  221. *
  222. * Unlink a file. Call gfs2_unlinki()
  223. *
  224. * Returns: errno
  225. */
  226. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  227. {
  228. struct gfs2_inode *dip = GFS2_I(dir);
  229. struct gfs2_sbd *sdp = GFS2_SB(dir);
  230. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  231. struct gfs2_holder ghs[3];
  232. struct gfs2_rgrpd *rgd;
  233. struct gfs2_holder ri_gh;
  234. int error;
  235. error = gfs2_rindex_hold(sdp, &ri_gh);
  236. if (error)
  237. return error;
  238. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  239. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  240. rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
  241. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  242. error = gfs2_glock_nq_m(3, ghs);
  243. if (error)
  244. goto out;
  245. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  246. if (error)
  247. goto out_gunlock;
  248. error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
  249. if (error)
  250. goto out_gunlock;
  251. error = gfs2_dir_del(dip, &dentry->d_name);
  252. if (error)
  253. goto out_end_trans;
  254. error = gfs2_change_nlink(ip, -1);
  255. out_end_trans:
  256. gfs2_trans_end(sdp);
  257. out_gunlock:
  258. gfs2_glock_dq_m(3, ghs);
  259. out:
  260. gfs2_holder_uninit(ghs);
  261. gfs2_holder_uninit(ghs + 1);
  262. gfs2_holder_uninit(ghs + 2);
  263. gfs2_glock_dq_uninit(&ri_gh);
  264. return error;
  265. }
  266. /**
  267. * gfs2_symlink - Create a symlink
  268. * @dir: The directory to create the symlink in
  269. * @dentry: The dentry to put the symlink in
  270. * @symname: The thing which the link points to
  271. *
  272. * Returns: errno
  273. */
  274. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  275. const char *symname)
  276. {
  277. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  278. struct gfs2_sbd *sdp = GFS2_SB(dir);
  279. struct gfs2_holder ghs[2];
  280. struct inode *inode;
  281. struct buffer_head *dibh;
  282. int size;
  283. int error;
  284. /* Must be stuffed with a null terminator for gfs2_follow_link() */
  285. size = strlen(symname);
  286. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  287. return -ENAMETOOLONG;
  288. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  289. inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
  290. if (IS_ERR(inode)) {
  291. gfs2_holder_uninit(ghs);
  292. return PTR_ERR(inode);
  293. }
  294. ip = ghs[1].gh_gl->gl_object;
  295. ip->i_di.di_size = size;
  296. error = gfs2_meta_inode_buffer(ip, &dibh);
  297. if (!gfs2_assert_withdraw(sdp, !error)) {
  298. gfs2_dinode_out(ip, dibh->b_data);
  299. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
  300. size);
  301. brelse(dibh);
  302. }
  303. gfs2_trans_end(sdp);
  304. if (dip->i_alloc.al_rgd)
  305. gfs2_inplace_release(dip);
  306. gfs2_quota_unlock(dip);
  307. gfs2_alloc_put(dip);
  308. gfs2_glock_dq_uninit_m(2, ghs);
  309. d_instantiate(dentry, inode);
  310. mark_inode_dirty(inode);
  311. return 0;
  312. }
  313. /**
  314. * gfs2_mkdir - Make a directory
  315. * @dir: The parent directory of the new one
  316. * @dentry: The dentry of the new directory
  317. * @mode: The mode of the new directory
  318. *
  319. * Returns: errno
  320. */
  321. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  322. {
  323. struct gfs2_inode *dip = GFS2_I(dir), *ip;
  324. struct gfs2_sbd *sdp = GFS2_SB(dir);
  325. struct gfs2_holder ghs[2];
  326. struct inode *inode;
  327. struct buffer_head *dibh;
  328. int error;
  329. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  330. inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
  331. if (IS_ERR(inode)) {
  332. gfs2_holder_uninit(ghs);
  333. return PTR_ERR(inode);
  334. }
  335. ip = ghs[1].gh_gl->gl_object;
  336. ip->i_inode.i_nlink = 2;
  337. ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
  338. ip->i_di.di_flags |= GFS2_DIF_JDATA;
  339. ip->i_di.di_entries = 2;
  340. error = gfs2_meta_inode_buffer(ip, &dibh);
  341. if (!gfs2_assert_withdraw(sdp, !error)) {
  342. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  343. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  344. struct qstr str;
  345. gfs2_str2qstr(&str, ".");
  346. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  347. gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
  348. dent->de_inum = di->di_num; /* already GFS2 endian */
  349. dent->de_type = cpu_to_be16(DT_DIR);
  350. di->di_entries = cpu_to_be32(1);
  351. gfs2_str2qstr(&str, "..");
  352. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  353. gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  354. gfs2_inum_out(&dip->i_num, &dent->de_inum);
  355. dent->de_type = cpu_to_be16(DT_DIR);
  356. gfs2_dinode_out(ip, di);
  357. brelse(dibh);
  358. }
  359. error = gfs2_change_nlink(dip, +1);
  360. gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
  361. gfs2_trans_end(sdp);
  362. if (dip->i_alloc.al_rgd)
  363. gfs2_inplace_release(dip);
  364. gfs2_quota_unlock(dip);
  365. gfs2_alloc_put(dip);
  366. gfs2_glock_dq_uninit_m(2, ghs);
  367. d_instantiate(dentry, inode);
  368. mark_inode_dirty(inode);
  369. return 0;
  370. }
  371. /**
  372. * gfs2_rmdir - Remove a directory
  373. * @dir: The parent directory of the directory to be removed
  374. * @dentry: The dentry of the directory to remove
  375. *
  376. * Remove a directory. Call gfs2_rmdiri()
  377. *
  378. * Returns: errno
  379. */
  380. static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
  381. {
  382. struct gfs2_inode *dip = GFS2_I(dir);
  383. struct gfs2_sbd *sdp = GFS2_SB(dir);
  384. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  385. struct gfs2_holder ghs[3];
  386. struct gfs2_rgrpd *rgd;
  387. struct gfs2_holder ri_gh;
  388. int error;
  389. error = gfs2_rindex_hold(sdp, &ri_gh);
  390. if (error)
  391. return error;
  392. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  393. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  394. rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
  395. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  396. error = gfs2_glock_nq_m(3, ghs);
  397. if (error)
  398. goto out;
  399. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  400. if (error)
  401. goto out_gunlock;
  402. if (ip->i_di.di_entries < 2) {
  403. if (gfs2_consist_inode(ip))
  404. gfs2_dinode_print(ip);
  405. error = -EIO;
  406. goto out_gunlock;
  407. }
  408. if (ip->i_di.di_entries > 2) {
  409. error = -ENOTEMPTY;
  410. goto out_gunlock;
  411. }
  412. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
  413. if (error)
  414. goto out_gunlock;
  415. error = gfs2_rmdiri(dip, &dentry->d_name, ip);
  416. gfs2_trans_end(sdp);
  417. out_gunlock:
  418. gfs2_glock_dq_m(3, ghs);
  419. out:
  420. gfs2_holder_uninit(ghs);
  421. gfs2_holder_uninit(ghs + 1);
  422. gfs2_holder_uninit(ghs + 2);
  423. gfs2_glock_dq_uninit(&ri_gh);
  424. return error;
  425. }
  426. /**
  427. * gfs2_mknod - Make a special file
  428. * @dir: The directory in which the special file will reside
  429. * @dentry: The dentry of the special file
  430. * @mode: The mode of the special file
  431. * @rdev: The device specification of the special file
  432. *
  433. */
  434. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
  435. dev_t dev)
  436. {
  437. struct gfs2_inode *dip = GFS2_I(dir);
  438. struct gfs2_sbd *sdp = GFS2_SB(dir);
  439. struct gfs2_holder ghs[2];
  440. struct inode *inode;
  441. gfs2_holder_init(dip->i_gl, 0, 0, ghs);
  442. inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
  443. if (IS_ERR(inode)) {
  444. gfs2_holder_uninit(ghs);
  445. return PTR_ERR(inode);
  446. }
  447. gfs2_trans_end(sdp);
  448. if (dip->i_alloc.al_rgd)
  449. gfs2_inplace_release(dip);
  450. gfs2_quota_unlock(dip);
  451. gfs2_alloc_put(dip);
  452. gfs2_glock_dq_uninit_m(2, ghs);
  453. d_instantiate(dentry, inode);
  454. mark_inode_dirty(inode);
  455. return 0;
  456. }
  457. /**
  458. * gfs2_rename - Rename a file
  459. * @odir: Parent directory of old file name
  460. * @odentry: The old dentry of the file
  461. * @ndir: Parent directory of new file name
  462. * @ndentry: The new dentry of the file
  463. *
  464. * Returns: errno
  465. */
  466. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  467. struct inode *ndir, struct dentry *ndentry)
  468. {
  469. struct gfs2_inode *odip = GFS2_I(odir);
  470. struct gfs2_inode *ndip = GFS2_I(ndir);
  471. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  472. struct gfs2_inode *nip = NULL;
  473. struct gfs2_sbd *sdp = GFS2_SB(odir);
  474. struct gfs2_holder ghs[5], r_gh;
  475. struct gfs2_rgrpd *nrgd;
  476. unsigned int num_gh;
  477. int dir_rename = 0;
  478. int alloc_required;
  479. unsigned int x;
  480. int error;
  481. if (ndentry->d_inode) {
  482. nip = GFS2_I(ndentry->d_inode);
  483. if (ip == nip)
  484. return 0;
  485. }
  486. /* Make sure we aren't trying to move a dirctory into it's subdir */
  487. if (S_ISDIR(ip->i_inode.i_mode) && odip != ndip) {
  488. dir_rename = 1;
  489. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 0,
  490. &r_gh);
  491. if (error)
  492. goto out;
  493. error = gfs2_ok_to_move(ip, ndip);
  494. if (error)
  495. goto out_gunlock_r;
  496. }
  497. num_gh = 1;
  498. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  499. if (odip != ndip) {
  500. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  501. num_gh++;
  502. }
  503. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  504. num_gh++;
  505. if (nip) {
  506. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  507. num_gh++;
  508. /* grab the resource lock for unlink flag twiddling
  509. * this is the case of the target file already existing
  510. * so we unlink before doing the rename
  511. */
  512. nrgd = gfs2_blk2rgrpd(sdp, nip->i_num.no_addr);
  513. if (nrgd)
  514. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  515. }
  516. error = gfs2_glock_nq_m(num_gh, ghs);
  517. if (error)
  518. goto out_uninit;
  519. /* Check out the old directory */
  520. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  521. if (error)
  522. goto out_gunlock;
  523. /* Check out the new directory */
  524. if (nip) {
  525. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  526. if (error)
  527. goto out_gunlock;
  528. if (S_ISDIR(nip->i_inode.i_mode)) {
  529. if (nip->i_di.di_entries < 2) {
  530. if (gfs2_consist_inode(nip))
  531. gfs2_dinode_print(nip);
  532. error = -EIO;
  533. goto out_gunlock;
  534. }
  535. if (nip->i_di.di_entries > 2) {
  536. error = -ENOTEMPTY;
  537. goto out_gunlock;
  538. }
  539. }
  540. } else {
  541. error = permission(ndir, MAY_WRITE | MAY_EXEC, NULL);
  542. if (error)
  543. goto out_gunlock;
  544. error = gfs2_dir_search(ndir, &ndentry->d_name, NULL, NULL);
  545. switch (error) {
  546. case -ENOENT:
  547. error = 0;
  548. break;
  549. case 0:
  550. error = -EEXIST;
  551. default:
  552. goto out_gunlock;
  553. };
  554. if (odip != ndip) {
  555. if (!ndip->i_inode.i_nlink) {
  556. error = -EINVAL;
  557. goto out_gunlock;
  558. }
  559. if (ndip->i_di.di_entries == (u32)-1) {
  560. error = -EFBIG;
  561. goto out_gunlock;
  562. }
  563. if (S_ISDIR(ip->i_inode.i_mode) &&
  564. ndip->i_inode.i_nlink == (u32)-1) {
  565. error = -EMLINK;
  566. goto out_gunlock;
  567. }
  568. }
  569. }
  570. /* Check out the dir to be renamed */
  571. if (dir_rename) {
  572. error = permission(odentry->d_inode, MAY_WRITE, NULL);
  573. if (error)
  574. goto out_gunlock;
  575. }
  576. alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  577. if (error < 0)
  578. goto out_gunlock;
  579. error = 0;
  580. if (alloc_required) {
  581. struct gfs2_alloc *al = gfs2_alloc_get(ndip);
  582. error = gfs2_quota_lock(ndip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  583. if (error)
  584. goto out_alloc;
  585. error = gfs2_quota_check(ndip, ndip->i_inode.i_uid, ndip->i_inode.i_gid);
  586. if (error)
  587. goto out_gunlock_q;
  588. al->al_requested = sdp->sd_max_dirres;
  589. error = gfs2_inplace_reserve(ndip);
  590. if (error)
  591. goto out_gunlock_q;
  592. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  593. al->al_rgd->rd_ri.ri_length +
  594. 4 * RES_DINODE + 4 * RES_LEAF +
  595. RES_STATFS + RES_QUOTA + 4, 0);
  596. if (error)
  597. goto out_ipreserv;
  598. } else {
  599. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  600. 5 * RES_LEAF + 4, 0);
  601. if (error)
  602. goto out_gunlock;
  603. }
  604. /* Remove the target file, if it exists */
  605. if (nip) {
  606. if (S_ISDIR(nip->i_inode.i_mode))
  607. error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
  608. else {
  609. error = gfs2_dir_del(ndip, &ndentry->d_name);
  610. if (error)
  611. goto out_end_trans;
  612. error = gfs2_change_nlink(nip, -1);
  613. }
  614. if (error)
  615. goto out_end_trans;
  616. }
  617. if (dir_rename) {
  618. struct qstr name;
  619. gfs2_str2qstr(&name, "..");
  620. error = gfs2_change_nlink(ndip, +1);
  621. if (error)
  622. goto out_end_trans;
  623. error = gfs2_change_nlink(odip, -1);
  624. if (error)
  625. goto out_end_trans;
  626. error = gfs2_dir_mvino(ip, &name, &ndip->i_num, DT_DIR);
  627. if (error)
  628. goto out_end_trans;
  629. } else {
  630. struct buffer_head *dibh;
  631. error = gfs2_meta_inode_buffer(ip, &dibh);
  632. if (error)
  633. goto out_end_trans;
  634. ip->i_inode.i_ctime = CURRENT_TIME_SEC;
  635. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  636. gfs2_dinode_out(ip, dibh->b_data);
  637. brelse(dibh);
  638. }
  639. error = gfs2_dir_del(odip, &odentry->d_name);
  640. if (error)
  641. goto out_end_trans;
  642. error = gfs2_dir_add(ndir, &ndentry->d_name, &ip->i_num,
  643. IF2DT(ip->i_inode.i_mode));
  644. if (error)
  645. goto out_end_trans;
  646. out_end_trans:
  647. gfs2_trans_end(sdp);
  648. out_ipreserv:
  649. if (alloc_required)
  650. gfs2_inplace_release(ndip);
  651. out_gunlock_q:
  652. if (alloc_required)
  653. gfs2_quota_unlock(ndip);
  654. out_alloc:
  655. if (alloc_required)
  656. gfs2_alloc_put(ndip);
  657. out_gunlock:
  658. gfs2_glock_dq_m(num_gh, ghs);
  659. out_uninit:
  660. for (x = 0; x < num_gh; x++)
  661. gfs2_holder_uninit(ghs + x);
  662. out_gunlock_r:
  663. if (dir_rename)
  664. gfs2_glock_dq_uninit(&r_gh);
  665. out:
  666. return error;
  667. }
  668. /**
  669. * gfs2_readlink - Read the value of a symlink
  670. * @dentry: the symlink
  671. * @buf: the buffer to read the symlink data into
  672. * @size: the size of the buffer
  673. *
  674. * Returns: errno
  675. */
  676. static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
  677. int user_size)
  678. {
  679. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  680. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  681. unsigned int len = GFS2_FAST_NAME_SIZE;
  682. int error;
  683. error = gfs2_readlinki(ip, &buf, &len);
  684. if (error)
  685. return error;
  686. if (user_size > len - 1)
  687. user_size = len - 1;
  688. if (copy_to_user(user_buf, buf, user_size))
  689. error = -EFAULT;
  690. else
  691. error = user_size;
  692. if (buf != array)
  693. kfree(buf);
  694. return error;
  695. }
  696. /**
  697. * gfs2_follow_link - Follow a symbolic link
  698. * @dentry: The dentry of the link
  699. * @nd: Data that we pass to vfs_follow_link()
  700. *
  701. * This can handle symlinks of any size. It is optimised for symlinks
  702. * under GFS2_FAST_NAME_SIZE.
  703. *
  704. * Returns: 0 on success or error code
  705. */
  706. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  707. {
  708. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  709. char array[GFS2_FAST_NAME_SIZE], *buf = array;
  710. unsigned int len = GFS2_FAST_NAME_SIZE;
  711. int error;
  712. error = gfs2_readlinki(ip, &buf, &len);
  713. if (!error) {
  714. error = vfs_follow_link(nd, buf);
  715. if (buf != array)
  716. kfree(buf);
  717. }
  718. return ERR_PTR(error);
  719. }
  720. /**
  721. * gfs2_permission -
  722. * @inode:
  723. * @mask:
  724. * @nd: passed from Linux VFS, ignored by us
  725. *
  726. * This may be called from the VFS directly, or from within GFS2 with the
  727. * inode locked, so we look to see if the glock is already locked and only
  728. * lock the glock if its not already been done.
  729. *
  730. * Returns: errno
  731. */
  732. static int gfs2_permission(struct inode *inode, int mask, struct nameidata *nd)
  733. {
  734. struct gfs2_inode *ip = GFS2_I(inode);
  735. struct gfs2_holder i_gh;
  736. int error;
  737. int unlock = 0;
  738. if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
  739. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  740. if (error)
  741. return error;
  742. unlock = 1;
  743. }
  744. error = generic_permission(inode, mask, gfs2_check_acl);
  745. if (unlock)
  746. gfs2_glock_dq_uninit(&i_gh);
  747. return error;
  748. }
  749. static int setattr_size(struct inode *inode, struct iattr *attr)
  750. {
  751. struct gfs2_inode *ip = GFS2_I(inode);
  752. int error;
  753. if (attr->ia_size != ip->i_di.di_size) {
  754. error = vmtruncate(inode, attr->ia_size);
  755. if (error)
  756. return error;
  757. }
  758. error = gfs2_truncatei(ip, attr->ia_size);
  759. if (error)
  760. return error;
  761. return error;
  762. }
  763. static int setattr_chown(struct inode *inode, struct iattr *attr)
  764. {
  765. struct gfs2_inode *ip = GFS2_I(inode);
  766. struct gfs2_sbd *sdp = GFS2_SB(inode);
  767. struct buffer_head *dibh;
  768. u32 ouid, ogid, nuid, ngid;
  769. int error;
  770. ouid = inode->i_uid;
  771. ogid = inode->i_gid;
  772. nuid = attr->ia_uid;
  773. ngid = attr->ia_gid;
  774. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  775. ouid = nuid = NO_QUOTA_CHANGE;
  776. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  777. ogid = ngid = NO_QUOTA_CHANGE;
  778. gfs2_alloc_get(ip);
  779. error = gfs2_quota_lock(ip, nuid, ngid);
  780. if (error)
  781. goto out_alloc;
  782. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  783. error = gfs2_quota_check(ip, nuid, ngid);
  784. if (error)
  785. goto out_gunlock_q;
  786. }
  787. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  788. if (error)
  789. goto out_gunlock_q;
  790. error = gfs2_meta_inode_buffer(ip, &dibh);
  791. if (error)
  792. goto out_end_trans;
  793. error = inode_setattr(inode, attr);
  794. gfs2_assert_warn(sdp, !error);
  795. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  796. gfs2_dinode_out(ip, dibh->b_data);
  797. brelse(dibh);
  798. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  799. gfs2_quota_change(ip, -ip->i_di.di_blocks, ouid, ogid);
  800. gfs2_quota_change(ip, ip->i_di.di_blocks, nuid, ngid);
  801. }
  802. out_end_trans:
  803. gfs2_trans_end(sdp);
  804. out_gunlock_q:
  805. gfs2_quota_unlock(ip);
  806. out_alloc:
  807. gfs2_alloc_put(ip);
  808. return error;
  809. }
  810. /**
  811. * gfs2_setattr - Change attributes on an inode
  812. * @dentry: The dentry which is changing
  813. * @attr: The structure describing the change
  814. *
  815. * The VFS layer wants to change one or more of an inodes attributes. Write
  816. * that change out to disk.
  817. *
  818. * Returns: errno
  819. */
  820. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  821. {
  822. struct inode *inode = dentry->d_inode;
  823. struct gfs2_inode *ip = GFS2_I(inode);
  824. struct gfs2_holder i_gh;
  825. int error;
  826. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  827. if (error)
  828. return error;
  829. error = -EPERM;
  830. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  831. goto out;
  832. error = inode_change_ok(inode, attr);
  833. if (error)
  834. goto out;
  835. if (attr->ia_valid & ATTR_SIZE)
  836. error = setattr_size(inode, attr);
  837. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  838. error = setattr_chown(inode, attr);
  839. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  840. error = gfs2_acl_chmod(ip, attr);
  841. else
  842. error = gfs2_setattr_simple(ip, attr);
  843. out:
  844. gfs2_glock_dq_uninit(&i_gh);
  845. if (!error)
  846. mark_inode_dirty(inode);
  847. return error;
  848. }
  849. /**
  850. * gfs2_getattr - Read out an inode's attributes
  851. * @mnt: The vfsmount the inode is being accessed from
  852. * @dentry: The dentry to stat
  853. * @stat: The inode's stats
  854. *
  855. * This may be called from the VFS directly, or from within GFS2 with the
  856. * inode locked, so we look to see if the glock is already locked and only
  857. * lock the glock if its not already been done. Note that its the NFS
  858. * readdirplus operation which causes this to be called (from filldir)
  859. * with the glock already held.
  860. *
  861. * Returns: errno
  862. */
  863. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  864. struct kstat *stat)
  865. {
  866. struct inode *inode = dentry->d_inode;
  867. struct gfs2_inode *ip = GFS2_I(inode);
  868. struct gfs2_holder gh;
  869. int error;
  870. int unlock = 0;
  871. if (gfs2_glock_is_locked_by_me(ip->i_gl) == 0) {
  872. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  873. if (error)
  874. return error;
  875. unlock = 1;
  876. }
  877. generic_fillattr(inode, stat);
  878. if (unlock)
  879. gfs2_glock_dq_uninit(&gh);
  880. return 0;
  881. }
  882. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  883. const void *data, size_t size, int flags)
  884. {
  885. struct inode *inode = dentry->d_inode;
  886. struct gfs2_ea_request er;
  887. memset(&er, 0, sizeof(struct gfs2_ea_request));
  888. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  889. if (er.er_type == GFS2_EATYPE_UNUSED)
  890. return -EOPNOTSUPP;
  891. er.er_data = (char *)data;
  892. er.er_name_len = strlen(er.er_name);
  893. er.er_data_len = size;
  894. er.er_flags = flags;
  895. gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
  896. return gfs2_ea_set(GFS2_I(inode), &er);
  897. }
  898. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  899. void *data, size_t size)
  900. {
  901. struct gfs2_ea_request er;
  902. memset(&er, 0, sizeof(struct gfs2_ea_request));
  903. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  904. if (er.er_type == GFS2_EATYPE_UNUSED)
  905. return -EOPNOTSUPP;
  906. er.er_data = data;
  907. er.er_name_len = strlen(er.er_name);
  908. er.er_data_len = size;
  909. return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
  910. }
  911. static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
  912. {
  913. struct gfs2_ea_request er;
  914. memset(&er, 0, sizeof(struct gfs2_ea_request));
  915. er.er_data = (size) ? buffer : NULL;
  916. er.er_data_len = size;
  917. return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
  918. }
  919. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  920. {
  921. struct gfs2_ea_request er;
  922. memset(&er, 0, sizeof(struct gfs2_ea_request));
  923. er.er_type = gfs2_ea_name2type(name, &er.er_name);
  924. if (er.er_type == GFS2_EATYPE_UNUSED)
  925. return -EOPNOTSUPP;
  926. er.er_name_len = strlen(er.er_name);
  927. return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
  928. }
  929. const struct inode_operations gfs2_file_iops = {
  930. .permission = gfs2_permission,
  931. .setattr = gfs2_setattr,
  932. .getattr = gfs2_getattr,
  933. .setxattr = gfs2_setxattr,
  934. .getxattr = gfs2_getxattr,
  935. .listxattr = gfs2_listxattr,
  936. .removexattr = gfs2_removexattr,
  937. };
  938. const struct inode_operations gfs2_dev_iops = {
  939. .permission = gfs2_permission,
  940. .setattr = gfs2_setattr,
  941. .getattr = gfs2_getattr,
  942. .setxattr = gfs2_setxattr,
  943. .getxattr = gfs2_getxattr,
  944. .listxattr = gfs2_listxattr,
  945. .removexattr = gfs2_removexattr,
  946. };
  947. const struct inode_operations gfs2_dir_iops = {
  948. .create = gfs2_create,
  949. .lookup = gfs2_lookup,
  950. .link = gfs2_link,
  951. .unlink = gfs2_unlink,
  952. .symlink = gfs2_symlink,
  953. .mkdir = gfs2_mkdir,
  954. .rmdir = gfs2_rmdir,
  955. .mknod = gfs2_mknod,
  956. .rename = gfs2_rename,
  957. .permission = gfs2_permission,
  958. .setattr = gfs2_setattr,
  959. .getattr = gfs2_getattr,
  960. .setxattr = gfs2_setxattr,
  961. .getxattr = gfs2_getxattr,
  962. .listxattr = gfs2_listxattr,
  963. .removexattr = gfs2_removexattr,
  964. };
  965. const struct inode_operations gfs2_symlink_iops = {
  966. .readlink = gfs2_readlink,
  967. .follow_link = gfs2_follow_link,
  968. .permission = gfs2_permission,
  969. .setattr = gfs2_setattr,
  970. .getattr = gfs2_getattr,
  971. .setxattr = gfs2_setxattr,
  972. .getxattr = gfs2_getxattr,
  973. .listxattr = gfs2_listxattr,
  974. .removexattr = gfs2_removexattr,
  975. };