inode.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2011 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/security.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 "xattr.h"
  28. #include "glock.h"
  29. #include "inode.h"
  30. #include "meta_io.h"
  31. #include "quota.h"
  32. #include "rgrp.h"
  33. #include "trans.h"
  34. #include "util.h"
  35. #include "super.h"
  36. #include "glops.h"
  37. struct gfs2_skip_data {
  38. u64 no_addr;
  39. int skipped;
  40. int non_block;
  41. };
  42. static int iget_test(struct inode *inode, void *opaque)
  43. {
  44. struct gfs2_inode *ip = GFS2_I(inode);
  45. struct gfs2_skip_data *data = opaque;
  46. if (ip->i_no_addr == data->no_addr) {
  47. if (data->non_block &&
  48. inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
  49. data->skipped = 1;
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int iget_set(struct inode *inode, void *opaque)
  57. {
  58. struct gfs2_inode *ip = GFS2_I(inode);
  59. struct gfs2_skip_data *data = opaque;
  60. if (data->skipped)
  61. return -ENOENT;
  62. inode->i_ino = (unsigned long)(data->no_addr);
  63. ip->i_no_addr = data->no_addr;
  64. return 0;
  65. }
  66. struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr, int non_block)
  67. {
  68. unsigned long hash = (unsigned long)no_addr;
  69. struct gfs2_skip_data data;
  70. data.no_addr = no_addr;
  71. data.skipped = 0;
  72. data.non_block = non_block;
  73. return ilookup5(sb, hash, iget_test, &data);
  74. }
  75. static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
  76. int non_block)
  77. {
  78. struct gfs2_skip_data data;
  79. unsigned long hash = (unsigned long)no_addr;
  80. data.no_addr = no_addr;
  81. data.skipped = 0;
  82. data.non_block = non_block;
  83. return iget5_locked(sb, hash, iget_test, iget_set, &data);
  84. }
  85. /**
  86. * gfs2_set_iop - Sets inode operations
  87. * @inode: The inode with correct i_mode filled in
  88. *
  89. * GFS2 lookup code fills in vfs inode contents based on info obtained
  90. * from directory entry inside gfs2_inode_lookup().
  91. */
  92. static void gfs2_set_iop(struct inode *inode)
  93. {
  94. struct gfs2_sbd *sdp = GFS2_SB(inode);
  95. umode_t mode = inode->i_mode;
  96. if (S_ISREG(mode)) {
  97. inode->i_op = &gfs2_file_iops;
  98. if (gfs2_localflocks(sdp))
  99. inode->i_fop = &gfs2_file_fops_nolock;
  100. else
  101. inode->i_fop = &gfs2_file_fops;
  102. } else if (S_ISDIR(mode)) {
  103. inode->i_op = &gfs2_dir_iops;
  104. if (gfs2_localflocks(sdp))
  105. inode->i_fop = &gfs2_dir_fops_nolock;
  106. else
  107. inode->i_fop = &gfs2_dir_fops;
  108. } else if (S_ISLNK(mode)) {
  109. inode->i_op = &gfs2_symlink_iops;
  110. } else {
  111. inode->i_op = &gfs2_file_iops;
  112. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  113. }
  114. }
  115. /**
  116. * gfs2_inode_lookup - Lookup an inode
  117. * @sb: The super block
  118. * @no_addr: The inode number
  119. * @type: The type of the inode
  120. * non_block: Can we block on inodes that are being freed?
  121. *
  122. * Returns: A VFS inode, or an error
  123. */
  124. struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
  125. u64 no_addr, u64 no_formal_ino, int non_block)
  126. {
  127. struct inode *inode;
  128. struct gfs2_inode *ip;
  129. struct gfs2_glock *io_gl = NULL;
  130. int error;
  131. inode = gfs2_iget(sb, no_addr, non_block);
  132. ip = GFS2_I(inode);
  133. if (!inode)
  134. return ERR_PTR(-ENOBUFS);
  135. if (inode->i_state & I_NEW) {
  136. struct gfs2_sbd *sdp = GFS2_SB(inode);
  137. ip->i_no_formal_ino = no_formal_ino;
  138. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
  139. if (unlikely(error))
  140. goto fail;
  141. ip->i_gl->gl_object = ip;
  142. error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
  143. if (unlikely(error))
  144. goto fail_put;
  145. set_bit(GIF_INVALID, &ip->i_flags);
  146. error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
  147. if (unlikely(error))
  148. goto fail_iopen;
  149. ip->i_iopen_gh.gh_gl->gl_object = ip;
  150. gfs2_glock_put(io_gl);
  151. io_gl = NULL;
  152. if (type == DT_UNKNOWN) {
  153. /* Inode glock must be locked already */
  154. error = gfs2_inode_refresh(GFS2_I(inode));
  155. if (error)
  156. goto fail_refresh;
  157. } else {
  158. inode->i_mode = DT2IF(type);
  159. }
  160. gfs2_set_iop(inode);
  161. unlock_new_inode(inode);
  162. }
  163. return inode;
  164. fail_refresh:
  165. ip->i_iopen_gh.gh_gl->gl_object = NULL;
  166. gfs2_glock_dq_uninit(&ip->i_iopen_gh);
  167. fail_iopen:
  168. if (io_gl)
  169. gfs2_glock_put(io_gl);
  170. fail_put:
  171. ip->i_gl->gl_object = NULL;
  172. gfs2_glock_put(ip->i_gl);
  173. fail:
  174. iget_failed(inode);
  175. return ERR_PTR(error);
  176. }
  177. struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
  178. u64 *no_formal_ino, unsigned int blktype)
  179. {
  180. struct super_block *sb = sdp->sd_vfs;
  181. struct gfs2_holder i_gh;
  182. struct inode *inode = NULL;
  183. int error;
  184. /* Must not read in block until block type is verified */
  185. error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
  186. LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
  187. if (error)
  188. return ERR_PTR(error);
  189. error = gfs2_check_blk_type(sdp, no_addr, blktype);
  190. if (error)
  191. goto fail;
  192. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
  193. if (IS_ERR(inode))
  194. goto fail;
  195. /* Two extra checks for NFS only */
  196. if (no_formal_ino) {
  197. error = -ESTALE;
  198. if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
  199. goto fail_iput;
  200. error = -EIO;
  201. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
  202. goto fail_iput;
  203. error = 0;
  204. }
  205. fail:
  206. gfs2_glock_dq_uninit(&i_gh);
  207. return error ? ERR_PTR(error) : inode;
  208. fail_iput:
  209. iput(inode);
  210. goto fail;
  211. }
  212. struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
  213. {
  214. struct qstr qstr;
  215. struct inode *inode;
  216. gfs2_str2qstr(&qstr, name);
  217. inode = gfs2_lookupi(dip, &qstr, 1);
  218. /* gfs2_lookupi has inconsistent callers: vfs
  219. * related routines expect NULL for no entry found,
  220. * gfs2_lookup_simple callers expect ENOENT
  221. * and do not check for NULL.
  222. */
  223. if (inode == NULL)
  224. return ERR_PTR(-ENOENT);
  225. else
  226. return inode;
  227. }
  228. /**
  229. * gfs2_lookupi - Look up a filename in a directory and return its inode
  230. * @d_gh: An initialized holder for the directory glock
  231. * @name: The name of the inode to look for
  232. * @is_root: If 1, ignore the caller's permissions
  233. * @i_gh: An uninitialized holder for the new inode glock
  234. *
  235. * This can be called via the VFS filldir function when NFS is doing
  236. * a readdirplus and the inode which its intending to stat isn't
  237. * already in cache. In this case we must not take the directory glock
  238. * again, since the readdir call will have already taken that lock.
  239. *
  240. * Returns: errno
  241. */
  242. struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
  243. int is_root)
  244. {
  245. struct super_block *sb = dir->i_sb;
  246. struct gfs2_inode *dip = GFS2_I(dir);
  247. struct gfs2_holder d_gh;
  248. int error = 0;
  249. struct inode *inode = NULL;
  250. int unlock = 0;
  251. if (!name->len || name->len > GFS2_FNAMESIZE)
  252. return ERR_PTR(-ENAMETOOLONG);
  253. if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
  254. (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
  255. dir == sb->s_root->d_inode)) {
  256. igrab(dir);
  257. return dir;
  258. }
  259. if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
  260. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
  261. if (error)
  262. return ERR_PTR(error);
  263. unlock = 1;
  264. }
  265. if (!is_root) {
  266. error = gfs2_permission(dir, MAY_EXEC);
  267. if (error)
  268. goto out;
  269. }
  270. inode = gfs2_dir_search(dir, name);
  271. if (IS_ERR(inode))
  272. error = PTR_ERR(inode);
  273. out:
  274. if (unlock)
  275. gfs2_glock_dq_uninit(&d_gh);
  276. if (error == -ENOENT)
  277. return NULL;
  278. return inode ? inode : ERR_PTR(error);
  279. }
  280. /**
  281. * create_ok - OK to create a new on-disk inode here?
  282. * @dip: Directory in which dinode is to be created
  283. * @name: Name of new dinode
  284. * @mode:
  285. *
  286. * Returns: errno
  287. */
  288. static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
  289. umode_t mode)
  290. {
  291. int error;
  292. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  293. if (error)
  294. return error;
  295. /* Don't create entries in an unlinked directory */
  296. if (!dip->i_inode.i_nlink)
  297. return -ENOENT;
  298. error = gfs2_dir_check(&dip->i_inode, name, NULL);
  299. switch (error) {
  300. case -ENOENT:
  301. error = 0;
  302. break;
  303. case 0:
  304. return -EEXIST;
  305. default:
  306. return error;
  307. }
  308. if (dip->i_entries == (u32)-1)
  309. return -EFBIG;
  310. if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
  311. return -EMLINK;
  312. return 0;
  313. }
  314. static void munge_mode_uid_gid(struct gfs2_inode *dip, umode_t *mode,
  315. unsigned int *uid, unsigned int *gid)
  316. {
  317. if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
  318. (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
  319. if (S_ISDIR(*mode))
  320. *mode |= S_ISUID;
  321. else if (dip->i_inode.i_uid != current_fsuid())
  322. *mode &= ~07111;
  323. *uid = dip->i_inode.i_uid;
  324. } else
  325. *uid = current_fsuid();
  326. if (dip->i_inode.i_mode & S_ISGID) {
  327. if (S_ISDIR(*mode))
  328. *mode |= S_ISGID;
  329. *gid = dip->i_inode.i_gid;
  330. } else
  331. *gid = current_fsgid();
  332. }
  333. static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
  334. {
  335. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  336. int error;
  337. int dblocks = 1;
  338. error = gfs2_rindex_update(sdp);
  339. if (error)
  340. fs_warn(sdp, "rindex update returns %d\n", error);
  341. error = gfs2_inplace_reserve(dip, RES_DINODE);
  342. if (error)
  343. goto out;
  344. error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
  345. if (error)
  346. goto out_ipreserv;
  347. error = gfs2_alloc_blocks(dip, no_addr, &dblocks, 1, generation);
  348. gfs2_trans_end(sdp);
  349. out_ipreserv:
  350. gfs2_inplace_release(dip);
  351. out:
  352. return error;
  353. }
  354. static void gfs2_init_dir(struct buffer_head *dibh,
  355. const struct gfs2_inode *parent)
  356. {
  357. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  358. struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
  359. gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
  360. dent->de_inum = di->di_num; /* already GFS2 endian */
  361. dent->de_type = cpu_to_be16(DT_DIR);
  362. dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
  363. gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
  364. gfs2_inum_out(parent, dent);
  365. dent->de_type = cpu_to_be16(DT_DIR);
  366. }
  367. /**
  368. * init_dinode - Fill in a new dinode structure
  369. * @dip: The directory this inode is being created in
  370. * @gl: The glock covering the new inode
  371. * @inum: The inode number
  372. * @mode: The file permissions
  373. * @uid: The uid of the new inode
  374. * @gid: The gid of the new inode
  375. * @generation: The generation number of the new inode
  376. * @dev: The device number (if a device node)
  377. * @symname: The symlink destination (if a symlink)
  378. * @size: The inode size (ignored for directories)
  379. * @bhp: The buffer head (returned to caller)
  380. *
  381. */
  382. static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  383. const struct gfs2_inum_host *inum, umode_t mode,
  384. unsigned int uid, unsigned int gid,
  385. const u64 *generation, dev_t dev, const char *symname,
  386. unsigned size, struct buffer_head **bhp)
  387. {
  388. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  389. struct gfs2_dinode *di;
  390. struct buffer_head *dibh;
  391. struct timespec tv = CURRENT_TIME;
  392. dibh = gfs2_meta_new(gl, inum->no_addr);
  393. gfs2_trans_add_bh(gl, dibh, 1);
  394. gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
  395. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  396. di = (struct gfs2_dinode *)dibh->b_data;
  397. di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
  398. di->di_num.no_addr = cpu_to_be64(inum->no_addr);
  399. di->di_mode = cpu_to_be32(mode);
  400. di->di_uid = cpu_to_be32(uid);
  401. di->di_gid = cpu_to_be32(gid);
  402. di->di_nlink = 0;
  403. di->di_size = cpu_to_be64(size);
  404. di->di_blocks = cpu_to_be64(1);
  405. di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
  406. di->di_major = cpu_to_be32(MAJOR(dev));
  407. di->di_minor = cpu_to_be32(MINOR(dev));
  408. di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
  409. di->di_generation = cpu_to_be64(*generation);
  410. di->di_flags = 0;
  411. di->__pad1 = 0;
  412. di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
  413. di->di_height = 0;
  414. di->__pad2 = 0;
  415. di->__pad3 = 0;
  416. di->di_depth = 0;
  417. di->di_entries = 0;
  418. memset(&di->__pad4, 0, sizeof(di->__pad4));
  419. di->di_eattr = 0;
  420. di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
  421. di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
  422. di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
  423. memset(&di->di_reserved, 0, sizeof(di->di_reserved));
  424. switch(mode & S_IFMT) {
  425. case S_IFREG:
  426. if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
  427. gfs2_tune_get(sdp, gt_new_files_jdata))
  428. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  429. break;
  430. case S_IFDIR:
  431. di->di_flags |= cpu_to_be32(dip->i_diskflags &
  432. GFS2_DIF_INHERIT_JDATA);
  433. di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
  434. di->di_size = cpu_to_be64(sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
  435. di->di_entries = cpu_to_be32(2);
  436. gfs2_init_dir(dibh, dip);
  437. break;
  438. case S_IFLNK:
  439. memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, size);
  440. break;
  441. }
  442. set_buffer_uptodate(dibh);
  443. *bhp = dibh;
  444. }
  445. static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
  446. umode_t mode, const struct gfs2_inum_host *inum,
  447. const u64 *generation, dev_t dev, const char *symname,
  448. unsigned int size, struct buffer_head **bhp)
  449. {
  450. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  451. unsigned int uid, gid;
  452. int error;
  453. munge_mode_uid_gid(dip, &mode, &uid, &gid);
  454. if (!gfs2_qadata_get(dip))
  455. return -ENOMEM;
  456. error = gfs2_quota_lock(dip, uid, gid);
  457. if (error)
  458. goto out;
  459. error = gfs2_quota_check(dip, uid, gid);
  460. if (error)
  461. goto out_quota;
  462. error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
  463. if (error)
  464. goto out_quota;
  465. init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, symname, size, bhp);
  466. gfs2_quota_change(dip, +1, uid, gid);
  467. gfs2_trans_end(sdp);
  468. out_quota:
  469. gfs2_quota_unlock(dip);
  470. out:
  471. gfs2_qadata_put(dip);
  472. return error;
  473. }
  474. static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
  475. struct gfs2_inode *ip)
  476. {
  477. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  478. struct gfs2_qadata *qa;
  479. int alloc_required;
  480. struct buffer_head *dibh;
  481. int error;
  482. qa = gfs2_qadata_get(dip);
  483. if (!qa)
  484. return -ENOMEM;
  485. error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  486. if (error)
  487. goto fail;
  488. error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
  489. if (alloc_required < 0)
  490. goto fail_quota_locks;
  491. if (alloc_required) {
  492. error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
  493. if (error)
  494. goto fail_quota_locks;
  495. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres);
  496. if (error)
  497. goto fail_quota_locks;
  498. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  499. dip->i_rgd->rd_length +
  500. 2 * RES_DINODE +
  501. RES_STATFS + RES_QUOTA, 0);
  502. if (error)
  503. goto fail_ipreserv;
  504. } else {
  505. error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
  506. if (error)
  507. goto fail_quota_locks;
  508. }
  509. error = gfs2_dir_add(&dip->i_inode, name, ip);
  510. if (error)
  511. goto fail_end_trans;
  512. error = gfs2_meta_inode_buffer(ip, &dibh);
  513. if (error)
  514. goto fail_end_trans;
  515. inc_nlink(&ip->i_inode);
  516. if (S_ISDIR(ip->i_inode.i_mode))
  517. inc_nlink(&ip->i_inode);
  518. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  519. gfs2_dinode_out(ip, dibh->b_data);
  520. brelse(dibh);
  521. return 0;
  522. fail_end_trans:
  523. gfs2_trans_end(sdp);
  524. fail_ipreserv:
  525. gfs2_inplace_release(dip);
  526. fail_quota_locks:
  527. gfs2_quota_unlock(dip);
  528. fail:
  529. gfs2_qadata_put(dip);
  530. return error;
  531. }
  532. static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  533. void *fs_info)
  534. {
  535. const struct xattr *xattr;
  536. int err = 0;
  537. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  538. err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
  539. xattr->value_len, 0,
  540. GFS2_EATYPE_SECURITY);
  541. if (err < 0)
  542. break;
  543. }
  544. return err;
  545. }
  546. static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
  547. const struct qstr *qstr)
  548. {
  549. return security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
  550. &gfs2_initxattrs, NULL);
  551. }
  552. /**
  553. * gfs2_create_inode - Create a new inode
  554. * @dir: The parent directory
  555. * @dentry: The new dentry
  556. * @mode: The permissions on the new inode
  557. * @dev: For device nodes, this is the device number
  558. * @symname: For symlinks, this is the link destination
  559. * @size: The initial size of the inode (ignored for directories)
  560. *
  561. * Returns: 0 on success, or error code
  562. */
  563. static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
  564. umode_t mode, dev_t dev, const char *symname,
  565. unsigned int size, int excl)
  566. {
  567. const struct qstr *name = &dentry->d_name;
  568. struct gfs2_holder ghs[2];
  569. struct inode *inode = NULL;
  570. struct gfs2_inode *dip = GFS2_I(dir);
  571. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  572. struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
  573. int error;
  574. u64 generation;
  575. struct buffer_head *bh = NULL;
  576. if (!name->len || name->len > GFS2_FNAMESIZE)
  577. return -ENAMETOOLONG;
  578. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  579. if (error)
  580. goto fail;
  581. error = create_ok(dip, name, mode);
  582. if ((error == -EEXIST) && S_ISREG(mode) && !excl) {
  583. inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  584. gfs2_glock_dq_uninit(ghs);
  585. d_instantiate(dentry, inode);
  586. return IS_ERR(inode) ? PTR_ERR(inode) : 0;
  587. }
  588. if (error)
  589. goto fail_gunlock;
  590. error = alloc_dinode(dip, &inum.no_addr, &generation);
  591. if (error)
  592. goto fail_gunlock;
  593. inum.no_formal_ino = generation;
  594. error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
  595. LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
  596. if (error)
  597. goto fail_gunlock;
  598. error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, symname, size, &bh);
  599. if (error)
  600. goto fail_gunlock2;
  601. inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
  602. inum.no_formal_ino, 0);
  603. if (IS_ERR(inode))
  604. goto fail_gunlock2;
  605. error = gfs2_inode_refresh(GFS2_I(inode));
  606. if (error)
  607. goto fail_gunlock2;
  608. error = gfs2_acl_create(dip, inode);
  609. if (error)
  610. goto fail_gunlock2;
  611. error = gfs2_security_init(dip, GFS2_I(inode), name);
  612. if (error)
  613. goto fail_gunlock2;
  614. error = link_dinode(dip, name, GFS2_I(inode));
  615. if (error)
  616. goto fail_gunlock2;
  617. if (bh)
  618. brelse(bh);
  619. gfs2_trans_end(sdp);
  620. /* Check if we reserved space in the rgrp. Function link_dinode may
  621. not, depending on whether alloc is required. */
  622. if (dip->i_res)
  623. gfs2_inplace_release(dip);
  624. gfs2_quota_unlock(dip);
  625. gfs2_qadata_put(dip);
  626. mark_inode_dirty(inode);
  627. gfs2_glock_dq_uninit_m(2, ghs);
  628. d_instantiate(dentry, inode);
  629. return 0;
  630. fail_gunlock2:
  631. gfs2_glock_dq_uninit(ghs + 1);
  632. fail_gunlock:
  633. gfs2_glock_dq_uninit(ghs);
  634. if (inode && !IS_ERR(inode)) {
  635. set_bit(GIF_ALLOC_FAILED, &GFS2_I(inode)->i_flags);
  636. iput(inode);
  637. }
  638. fail:
  639. if (bh)
  640. brelse(bh);
  641. return error;
  642. }
  643. /**
  644. * gfs2_create - Create a file
  645. * @dir: The directory in which to create the file
  646. * @dentry: The dentry of the new file
  647. * @mode: The mode of the new file
  648. *
  649. * Returns: errno
  650. */
  651. static int gfs2_create(struct inode *dir, struct dentry *dentry,
  652. umode_t mode, struct nameidata *nd)
  653. {
  654. int excl = 0;
  655. if (nd && (nd->flags & LOOKUP_EXCL))
  656. excl = 1;
  657. return gfs2_create_inode(dir, dentry, S_IFREG | mode, 0, NULL, 0, excl);
  658. }
  659. /**
  660. * gfs2_lookup - Look up a filename in a directory and return its inode
  661. * @dir: The directory inode
  662. * @dentry: The dentry of the new inode
  663. * @nd: passed from Linux VFS, ignored by us
  664. *
  665. * Called by the VFS layer. Lock dir and call gfs2_lookupi()
  666. *
  667. * Returns: errno
  668. */
  669. static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
  670. struct nameidata *nd)
  671. {
  672. struct inode *inode = gfs2_lookupi(dir, &dentry->d_name, 0);
  673. if (inode && !IS_ERR(inode)) {
  674. struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
  675. struct gfs2_holder gh;
  676. int error;
  677. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  678. if (error) {
  679. iput(inode);
  680. return ERR_PTR(error);
  681. }
  682. gfs2_glock_dq_uninit(&gh);
  683. }
  684. return d_splice_alias(inode, dentry);
  685. }
  686. /**
  687. * gfs2_link - Link to a file
  688. * @old_dentry: The inode to link
  689. * @dir: Add link to this directory
  690. * @dentry: The name of the link
  691. *
  692. * Link the inode in "old_dentry" into the directory "dir" with the
  693. * name in "dentry".
  694. *
  695. * Returns: errno
  696. */
  697. static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
  698. struct dentry *dentry)
  699. {
  700. struct gfs2_inode *dip = GFS2_I(dir);
  701. struct gfs2_sbd *sdp = GFS2_SB(dir);
  702. struct inode *inode = old_dentry->d_inode;
  703. struct gfs2_inode *ip = GFS2_I(inode);
  704. struct gfs2_holder ghs[2];
  705. struct buffer_head *dibh;
  706. int alloc_required;
  707. int error;
  708. if (S_ISDIR(inode->i_mode))
  709. return -EPERM;
  710. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  711. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  712. error = gfs2_glock_nq(ghs); /* parent */
  713. if (error)
  714. goto out_parent;
  715. error = gfs2_glock_nq(ghs + 1); /* child */
  716. if (error)
  717. goto out_child;
  718. error = -ENOENT;
  719. if (inode->i_nlink == 0)
  720. goto out_gunlock;
  721. error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
  722. if (error)
  723. goto out_gunlock;
  724. error = gfs2_dir_check(dir, &dentry->d_name, NULL);
  725. switch (error) {
  726. case -ENOENT:
  727. break;
  728. case 0:
  729. error = -EEXIST;
  730. default:
  731. goto out_gunlock;
  732. }
  733. error = -EINVAL;
  734. if (!dip->i_inode.i_nlink)
  735. goto out_gunlock;
  736. error = -EFBIG;
  737. if (dip->i_entries == (u32)-1)
  738. goto out_gunlock;
  739. error = -EPERM;
  740. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  741. goto out_gunlock;
  742. error = -EINVAL;
  743. if (!ip->i_inode.i_nlink)
  744. goto out_gunlock;
  745. error = -EMLINK;
  746. if (ip->i_inode.i_nlink == (u32)-1)
  747. goto out_gunlock;
  748. alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
  749. if (error < 0)
  750. goto out_gunlock;
  751. error = 0;
  752. if (alloc_required) {
  753. struct gfs2_qadata *qa = gfs2_qadata_get(dip);
  754. if (!qa) {
  755. error = -ENOMEM;
  756. goto out_gunlock;
  757. }
  758. error = gfs2_quota_lock_check(dip);
  759. if (error)
  760. goto out_alloc;
  761. error = gfs2_inplace_reserve(dip, sdp->sd_max_dirres);
  762. if (error)
  763. goto out_gunlock_q;
  764. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  765. gfs2_rg_blocks(dip) +
  766. 2 * RES_DINODE + RES_STATFS +
  767. RES_QUOTA, 0);
  768. if (error)
  769. goto out_ipres;
  770. } else {
  771. error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
  772. if (error)
  773. goto out_ipres;
  774. }
  775. error = gfs2_meta_inode_buffer(ip, &dibh);
  776. if (error)
  777. goto out_end_trans;
  778. error = gfs2_dir_add(dir, &dentry->d_name, ip);
  779. if (error)
  780. goto out_brelse;
  781. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  782. inc_nlink(&ip->i_inode);
  783. ip->i_inode.i_ctime = CURRENT_TIME;
  784. ihold(inode);
  785. d_instantiate(dentry, inode);
  786. mark_inode_dirty(inode);
  787. out_brelse:
  788. brelse(dibh);
  789. out_end_trans:
  790. gfs2_trans_end(sdp);
  791. out_ipres:
  792. if (alloc_required)
  793. gfs2_inplace_release(dip);
  794. out_gunlock_q:
  795. if (alloc_required)
  796. gfs2_quota_unlock(dip);
  797. out_alloc:
  798. if (alloc_required)
  799. gfs2_qadata_put(dip);
  800. out_gunlock:
  801. gfs2_glock_dq(ghs + 1);
  802. out_child:
  803. gfs2_glock_dq(ghs);
  804. out_parent:
  805. gfs2_holder_uninit(ghs);
  806. gfs2_holder_uninit(ghs + 1);
  807. return error;
  808. }
  809. /*
  810. * gfs2_unlink_ok - check to see that a inode is still in a directory
  811. * @dip: the directory
  812. * @name: the name of the file
  813. * @ip: the inode
  814. *
  815. * Assumes that the lock on (at least) @dip is held.
  816. *
  817. * Returns: 0 if the parent/child relationship is correct, errno if it isn't
  818. */
  819. static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
  820. const struct gfs2_inode *ip)
  821. {
  822. int error;
  823. if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
  824. return -EPERM;
  825. if ((dip->i_inode.i_mode & S_ISVTX) &&
  826. dip->i_inode.i_uid != current_fsuid() &&
  827. ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
  828. return -EPERM;
  829. if (IS_APPEND(&dip->i_inode))
  830. return -EPERM;
  831. error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
  832. if (error)
  833. return error;
  834. error = gfs2_dir_check(&dip->i_inode, name, ip);
  835. if (error)
  836. return error;
  837. return 0;
  838. }
  839. /**
  840. * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
  841. * @dip: The parent directory
  842. * @name: The name of the entry in the parent directory
  843. * @bh: The inode buffer for the inode to be removed
  844. * @inode: The inode to be removed
  845. *
  846. * Called with all the locks and in a transaction. This will only be
  847. * called for a directory after it has been checked to ensure it is empty.
  848. *
  849. * Returns: 0 on success, or an error
  850. */
  851. static int gfs2_unlink_inode(struct gfs2_inode *dip,
  852. const struct dentry *dentry,
  853. struct buffer_head *bh)
  854. {
  855. struct inode *inode = dentry->d_inode;
  856. struct gfs2_inode *ip = GFS2_I(inode);
  857. int error;
  858. error = gfs2_dir_del(dip, dentry);
  859. if (error)
  860. return error;
  861. ip->i_entries = 0;
  862. inode->i_ctime = CURRENT_TIME;
  863. if (S_ISDIR(inode->i_mode))
  864. clear_nlink(inode);
  865. else
  866. drop_nlink(inode);
  867. mark_inode_dirty(inode);
  868. if (inode->i_nlink == 0)
  869. gfs2_unlink_di(inode);
  870. return 0;
  871. }
  872. /**
  873. * gfs2_unlink - Unlink an inode (this does rmdir as well)
  874. * @dir: The inode of the directory containing the inode to unlink
  875. * @dentry: The file itself
  876. *
  877. * This routine uses the type of the inode as a flag to figure out
  878. * whether this is an unlink or an rmdir.
  879. *
  880. * Returns: errno
  881. */
  882. static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
  883. {
  884. struct gfs2_inode *dip = GFS2_I(dir);
  885. struct gfs2_sbd *sdp = GFS2_SB(dir);
  886. struct inode *inode = dentry->d_inode;
  887. struct gfs2_inode *ip = GFS2_I(inode);
  888. struct buffer_head *bh;
  889. struct gfs2_holder ghs[3];
  890. struct gfs2_rgrpd *rgd;
  891. int error = -EROFS;
  892. gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  893. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
  894. rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
  895. if (!rgd)
  896. goto out_inodes;
  897. gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
  898. error = gfs2_glock_nq(ghs); /* parent */
  899. if (error)
  900. goto out_parent;
  901. error = gfs2_glock_nq(ghs + 1); /* child */
  902. if (error)
  903. goto out_child;
  904. error = -ENOENT;
  905. if (inode->i_nlink == 0)
  906. goto out_rgrp;
  907. if (S_ISDIR(inode->i_mode)) {
  908. error = -ENOTEMPTY;
  909. if (ip->i_entries > 2 || inode->i_nlink > 2)
  910. goto out_rgrp;
  911. }
  912. error = gfs2_glock_nq(ghs + 2); /* rgrp */
  913. if (error)
  914. goto out_rgrp;
  915. error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
  916. if (error)
  917. goto out_gunlock;
  918. error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
  919. if (error)
  920. goto out_gunlock;
  921. error = gfs2_meta_inode_buffer(ip, &bh);
  922. if (error)
  923. goto out_end_trans;
  924. error = gfs2_unlink_inode(dip, dentry, bh);
  925. brelse(bh);
  926. out_end_trans:
  927. gfs2_trans_end(sdp);
  928. out_gunlock:
  929. gfs2_glock_dq(ghs + 2);
  930. out_rgrp:
  931. gfs2_glock_dq(ghs + 1);
  932. out_child:
  933. gfs2_glock_dq(ghs);
  934. out_parent:
  935. gfs2_holder_uninit(ghs + 2);
  936. out_inodes:
  937. gfs2_holder_uninit(ghs + 1);
  938. gfs2_holder_uninit(ghs);
  939. return error;
  940. }
  941. /**
  942. * gfs2_symlink - Create a symlink
  943. * @dir: The directory to create the symlink in
  944. * @dentry: The dentry to put the symlink in
  945. * @symname: The thing which the link points to
  946. *
  947. * Returns: errno
  948. */
  949. static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
  950. const char *symname)
  951. {
  952. struct gfs2_sbd *sdp = GFS2_SB(dir);
  953. unsigned int size;
  954. size = strlen(symname);
  955. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
  956. return -ENAMETOOLONG;
  957. return gfs2_create_inode(dir, dentry, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
  958. }
  959. /**
  960. * gfs2_mkdir - Make a directory
  961. * @dir: The parent directory of the new one
  962. * @dentry: The dentry of the new directory
  963. * @mode: The mode of the new directory
  964. *
  965. * Returns: errno
  966. */
  967. static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  968. {
  969. return gfs2_create_inode(dir, dentry, S_IFDIR | mode, 0, NULL, 0, 0);
  970. }
  971. /**
  972. * gfs2_mknod - Make a special file
  973. * @dir: The directory in which the special file will reside
  974. * @dentry: The dentry of the special file
  975. * @mode: The mode of the special file
  976. * @dev: The device specification of the special file
  977. *
  978. */
  979. static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  980. dev_t dev)
  981. {
  982. return gfs2_create_inode(dir, dentry, mode, dev, NULL, 0, 0);
  983. }
  984. /*
  985. * gfs2_ok_to_move - check if it's ok to move a directory to another directory
  986. * @this: move this
  987. * @to: to here
  988. *
  989. * Follow @to back to the root and make sure we don't encounter @this
  990. * Assumes we already hold the rename lock.
  991. *
  992. * Returns: errno
  993. */
  994. static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
  995. {
  996. struct inode *dir = &to->i_inode;
  997. struct super_block *sb = dir->i_sb;
  998. struct inode *tmp;
  999. int error = 0;
  1000. igrab(dir);
  1001. for (;;) {
  1002. if (dir == &this->i_inode) {
  1003. error = -EINVAL;
  1004. break;
  1005. }
  1006. if (dir == sb->s_root->d_inode) {
  1007. error = 0;
  1008. break;
  1009. }
  1010. tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
  1011. if (IS_ERR(tmp)) {
  1012. error = PTR_ERR(tmp);
  1013. break;
  1014. }
  1015. iput(dir);
  1016. dir = tmp;
  1017. }
  1018. iput(dir);
  1019. return error;
  1020. }
  1021. /**
  1022. * gfs2_rename - Rename a file
  1023. * @odir: Parent directory of old file name
  1024. * @odentry: The old dentry of the file
  1025. * @ndir: Parent directory of new file name
  1026. * @ndentry: The new dentry of the file
  1027. *
  1028. * Returns: errno
  1029. */
  1030. static int gfs2_rename(struct inode *odir, struct dentry *odentry,
  1031. struct inode *ndir, struct dentry *ndentry)
  1032. {
  1033. struct gfs2_inode *odip = GFS2_I(odir);
  1034. struct gfs2_inode *ndip = GFS2_I(ndir);
  1035. struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
  1036. struct gfs2_inode *nip = NULL;
  1037. struct gfs2_sbd *sdp = GFS2_SB(odir);
  1038. struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
  1039. struct gfs2_rgrpd *nrgd;
  1040. unsigned int num_gh;
  1041. int dir_rename = 0;
  1042. int alloc_required = 0;
  1043. unsigned int x;
  1044. int error;
  1045. if (ndentry->d_inode) {
  1046. nip = GFS2_I(ndentry->d_inode);
  1047. if (ip == nip)
  1048. return 0;
  1049. }
  1050. if (odip != ndip) {
  1051. error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
  1052. 0, &r_gh);
  1053. if (error)
  1054. goto out;
  1055. if (S_ISDIR(ip->i_inode.i_mode)) {
  1056. dir_rename = 1;
  1057. /* don't move a dirctory into it's subdir */
  1058. error = gfs2_ok_to_move(ip, ndip);
  1059. if (error)
  1060. goto out_gunlock_r;
  1061. }
  1062. }
  1063. num_gh = 1;
  1064. gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
  1065. if (odip != ndip) {
  1066. gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1067. num_gh++;
  1068. }
  1069. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1070. num_gh++;
  1071. if (nip) {
  1072. gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
  1073. num_gh++;
  1074. /* grab the resource lock for unlink flag twiddling
  1075. * this is the case of the target file already existing
  1076. * so we unlink before doing the rename
  1077. */
  1078. nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
  1079. if (nrgd)
  1080. gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
  1081. }
  1082. for (x = 0; x < num_gh; x++) {
  1083. error = gfs2_glock_nq(ghs + x);
  1084. if (error)
  1085. goto out_gunlock;
  1086. }
  1087. error = -ENOENT;
  1088. if (ip->i_inode.i_nlink == 0)
  1089. goto out_gunlock;
  1090. /* Check out the old directory */
  1091. error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
  1092. if (error)
  1093. goto out_gunlock;
  1094. /* Check out the new directory */
  1095. if (nip) {
  1096. error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
  1097. if (error)
  1098. goto out_gunlock;
  1099. if (nip->i_inode.i_nlink == 0) {
  1100. error = -EAGAIN;
  1101. goto out_gunlock;
  1102. }
  1103. if (S_ISDIR(nip->i_inode.i_mode)) {
  1104. if (nip->i_entries < 2) {
  1105. gfs2_consist_inode(nip);
  1106. error = -EIO;
  1107. goto out_gunlock;
  1108. }
  1109. if (nip->i_entries > 2) {
  1110. error = -ENOTEMPTY;
  1111. goto out_gunlock;
  1112. }
  1113. }
  1114. } else {
  1115. error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
  1116. if (error)
  1117. goto out_gunlock;
  1118. error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
  1119. switch (error) {
  1120. case -ENOENT:
  1121. error = 0;
  1122. break;
  1123. case 0:
  1124. error = -EEXIST;
  1125. default:
  1126. goto out_gunlock;
  1127. };
  1128. if (odip != ndip) {
  1129. if (!ndip->i_inode.i_nlink) {
  1130. error = -ENOENT;
  1131. goto out_gunlock;
  1132. }
  1133. if (ndip->i_entries == (u32)-1) {
  1134. error = -EFBIG;
  1135. goto out_gunlock;
  1136. }
  1137. if (S_ISDIR(ip->i_inode.i_mode) &&
  1138. ndip->i_inode.i_nlink == (u32)-1) {
  1139. error = -EMLINK;
  1140. goto out_gunlock;
  1141. }
  1142. }
  1143. }
  1144. /* Check out the dir to be renamed */
  1145. if (dir_rename) {
  1146. error = gfs2_permission(odentry->d_inode, MAY_WRITE);
  1147. if (error)
  1148. goto out_gunlock;
  1149. }
  1150. if (nip == NULL)
  1151. alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
  1152. error = alloc_required;
  1153. if (error < 0)
  1154. goto out_gunlock;
  1155. error = 0;
  1156. if (alloc_required) {
  1157. struct gfs2_qadata *qa = gfs2_qadata_get(ndip);
  1158. if (!qa) {
  1159. error = -ENOMEM;
  1160. goto out_gunlock;
  1161. }
  1162. error = gfs2_quota_lock_check(ndip);
  1163. if (error)
  1164. goto out_alloc;
  1165. error = gfs2_inplace_reserve(ndip, sdp->sd_max_dirres);
  1166. if (error)
  1167. goto out_gunlock_q;
  1168. error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
  1169. gfs2_rg_blocks(ndip) +
  1170. 4 * RES_DINODE + 4 * RES_LEAF +
  1171. RES_STATFS + RES_QUOTA + 4, 0);
  1172. if (error)
  1173. goto out_ipreserv;
  1174. } else {
  1175. error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
  1176. 5 * RES_LEAF + 4, 0);
  1177. if (error)
  1178. goto out_gunlock;
  1179. }
  1180. /* Remove the target file, if it exists */
  1181. if (nip) {
  1182. struct buffer_head *bh;
  1183. error = gfs2_meta_inode_buffer(nip, &bh);
  1184. if (error)
  1185. goto out_end_trans;
  1186. error = gfs2_unlink_inode(ndip, ndentry, bh);
  1187. brelse(bh);
  1188. }
  1189. if (dir_rename) {
  1190. error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
  1191. if (error)
  1192. goto out_end_trans;
  1193. } else {
  1194. struct buffer_head *dibh;
  1195. error = gfs2_meta_inode_buffer(ip, &dibh);
  1196. if (error)
  1197. goto out_end_trans;
  1198. ip->i_inode.i_ctime = CURRENT_TIME;
  1199. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1200. gfs2_dinode_out(ip, dibh->b_data);
  1201. brelse(dibh);
  1202. }
  1203. error = gfs2_dir_del(odip, odentry);
  1204. if (error)
  1205. goto out_end_trans;
  1206. error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
  1207. if (error)
  1208. goto out_end_trans;
  1209. out_end_trans:
  1210. gfs2_trans_end(sdp);
  1211. out_ipreserv:
  1212. if (alloc_required)
  1213. gfs2_inplace_release(ndip);
  1214. out_gunlock_q:
  1215. if (alloc_required)
  1216. gfs2_quota_unlock(ndip);
  1217. out_alloc:
  1218. if (alloc_required)
  1219. gfs2_qadata_put(ndip);
  1220. out_gunlock:
  1221. while (x--) {
  1222. gfs2_glock_dq(ghs + x);
  1223. gfs2_holder_uninit(ghs + x);
  1224. }
  1225. out_gunlock_r:
  1226. if (r_gh.gh_gl)
  1227. gfs2_glock_dq_uninit(&r_gh);
  1228. out:
  1229. return error;
  1230. }
  1231. /**
  1232. * gfs2_follow_link - Follow a symbolic link
  1233. * @dentry: The dentry of the link
  1234. * @nd: Data that we pass to vfs_follow_link()
  1235. *
  1236. * This can handle symlinks of any size.
  1237. *
  1238. * Returns: 0 on success or error code
  1239. */
  1240. static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
  1241. {
  1242. struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
  1243. struct gfs2_holder i_gh;
  1244. struct buffer_head *dibh;
  1245. unsigned int size;
  1246. char *buf;
  1247. int error;
  1248. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
  1249. error = gfs2_glock_nq(&i_gh);
  1250. if (error) {
  1251. gfs2_holder_uninit(&i_gh);
  1252. nd_set_link(nd, ERR_PTR(error));
  1253. return NULL;
  1254. }
  1255. size = (unsigned int)i_size_read(&ip->i_inode);
  1256. if (size == 0) {
  1257. gfs2_consist_inode(ip);
  1258. buf = ERR_PTR(-EIO);
  1259. goto out;
  1260. }
  1261. error = gfs2_meta_inode_buffer(ip, &dibh);
  1262. if (error) {
  1263. buf = ERR_PTR(error);
  1264. goto out;
  1265. }
  1266. buf = kzalloc(size + 1, GFP_NOFS);
  1267. if (!buf)
  1268. buf = ERR_PTR(-ENOMEM);
  1269. else
  1270. memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
  1271. brelse(dibh);
  1272. out:
  1273. gfs2_glock_dq_uninit(&i_gh);
  1274. nd_set_link(nd, buf);
  1275. return NULL;
  1276. }
  1277. static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1278. {
  1279. char *s = nd_get_link(nd);
  1280. if (!IS_ERR(s))
  1281. kfree(s);
  1282. }
  1283. /**
  1284. * gfs2_permission -
  1285. * @inode: The inode
  1286. * @mask: The mask to be tested
  1287. * @flags: Indicates whether this is an RCU path walk or not
  1288. *
  1289. * This may be called from the VFS directly, or from within GFS2 with the
  1290. * inode locked, so we look to see if the glock is already locked and only
  1291. * lock the glock if its not already been done.
  1292. *
  1293. * Returns: errno
  1294. */
  1295. int gfs2_permission(struct inode *inode, int mask)
  1296. {
  1297. struct gfs2_inode *ip;
  1298. struct gfs2_holder i_gh;
  1299. int error;
  1300. int unlock = 0;
  1301. ip = GFS2_I(inode);
  1302. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1303. if (mask & MAY_NOT_BLOCK)
  1304. return -ECHILD;
  1305. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  1306. if (error)
  1307. return error;
  1308. unlock = 1;
  1309. }
  1310. if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
  1311. error = -EACCES;
  1312. else
  1313. error = generic_permission(inode, mask);
  1314. if (unlock)
  1315. gfs2_glock_dq_uninit(&i_gh);
  1316. return error;
  1317. }
  1318. static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1319. {
  1320. setattr_copy(inode, attr);
  1321. mark_inode_dirty(inode);
  1322. return 0;
  1323. }
  1324. /**
  1325. * gfs2_setattr_simple -
  1326. * @ip:
  1327. * @attr:
  1328. *
  1329. * Returns: errno
  1330. */
  1331. int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
  1332. {
  1333. int error;
  1334. if (current->journal_info)
  1335. return __gfs2_setattr_simple(inode, attr);
  1336. error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
  1337. if (error)
  1338. return error;
  1339. error = __gfs2_setattr_simple(inode, attr);
  1340. gfs2_trans_end(GFS2_SB(inode));
  1341. return error;
  1342. }
  1343. static int setattr_chown(struct inode *inode, struct iattr *attr)
  1344. {
  1345. struct gfs2_inode *ip = GFS2_I(inode);
  1346. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1347. u32 ouid, ogid, nuid, ngid;
  1348. int error;
  1349. ouid = inode->i_uid;
  1350. ogid = inode->i_gid;
  1351. nuid = attr->ia_uid;
  1352. ngid = attr->ia_gid;
  1353. if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
  1354. ouid = nuid = NO_QUOTA_CHANGE;
  1355. if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
  1356. ogid = ngid = NO_QUOTA_CHANGE;
  1357. if (!gfs2_qadata_get(ip))
  1358. return -ENOMEM;
  1359. error = gfs2_quota_lock(ip, nuid, ngid);
  1360. if (error)
  1361. goto out_alloc;
  1362. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1363. error = gfs2_quota_check(ip, nuid, ngid);
  1364. if (error)
  1365. goto out_gunlock_q;
  1366. }
  1367. error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
  1368. if (error)
  1369. goto out_gunlock_q;
  1370. error = gfs2_setattr_simple(inode, attr);
  1371. if (error)
  1372. goto out_end_trans;
  1373. if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
  1374. u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
  1375. gfs2_quota_change(ip, -blocks, ouid, ogid);
  1376. gfs2_quota_change(ip, blocks, nuid, ngid);
  1377. }
  1378. out_end_trans:
  1379. gfs2_trans_end(sdp);
  1380. out_gunlock_q:
  1381. gfs2_quota_unlock(ip);
  1382. out_alloc:
  1383. gfs2_qadata_put(ip);
  1384. return error;
  1385. }
  1386. /**
  1387. * gfs2_setattr - Change attributes on an inode
  1388. * @dentry: The dentry which is changing
  1389. * @attr: The structure describing the change
  1390. *
  1391. * The VFS layer wants to change one or more of an inodes attributes. Write
  1392. * that change out to disk.
  1393. *
  1394. * Returns: errno
  1395. */
  1396. static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
  1397. {
  1398. struct inode *inode = dentry->d_inode;
  1399. struct gfs2_inode *ip = GFS2_I(inode);
  1400. struct gfs2_holder i_gh;
  1401. int error;
  1402. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
  1403. if (error)
  1404. return error;
  1405. error = -EPERM;
  1406. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  1407. goto out;
  1408. error = inode_change_ok(inode, attr);
  1409. if (error)
  1410. goto out;
  1411. if (attr->ia_valid & ATTR_SIZE)
  1412. error = gfs2_setattr_size(inode, attr->ia_size);
  1413. else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
  1414. error = setattr_chown(inode, attr);
  1415. else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
  1416. error = gfs2_acl_chmod(ip, attr);
  1417. else
  1418. error = gfs2_setattr_simple(inode, attr);
  1419. out:
  1420. if (!error)
  1421. mark_inode_dirty(inode);
  1422. gfs2_glock_dq_uninit(&i_gh);
  1423. return error;
  1424. }
  1425. /**
  1426. * gfs2_getattr - Read out an inode's attributes
  1427. * @mnt: The vfsmount the inode is being accessed from
  1428. * @dentry: The dentry to stat
  1429. * @stat: The inode's stats
  1430. *
  1431. * This may be called from the VFS directly, or from within GFS2 with the
  1432. * inode locked, so we look to see if the glock is already locked and only
  1433. * lock the glock if its not already been done. Note that its the NFS
  1434. * readdirplus operation which causes this to be called (from filldir)
  1435. * with the glock already held.
  1436. *
  1437. * Returns: errno
  1438. */
  1439. static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
  1440. struct kstat *stat)
  1441. {
  1442. struct inode *inode = dentry->d_inode;
  1443. struct gfs2_inode *ip = GFS2_I(inode);
  1444. struct gfs2_holder gh;
  1445. int error;
  1446. int unlock = 0;
  1447. if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
  1448. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1449. if (error)
  1450. return error;
  1451. unlock = 1;
  1452. }
  1453. generic_fillattr(inode, stat);
  1454. if (unlock)
  1455. gfs2_glock_dq_uninit(&gh);
  1456. return 0;
  1457. }
  1458. static int gfs2_setxattr(struct dentry *dentry, const char *name,
  1459. const void *data, size_t size, int flags)
  1460. {
  1461. struct inode *inode = dentry->d_inode;
  1462. struct gfs2_inode *ip = GFS2_I(inode);
  1463. struct gfs2_holder gh;
  1464. int ret;
  1465. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1466. ret = gfs2_glock_nq(&gh);
  1467. if (ret == 0) {
  1468. ret = generic_setxattr(dentry, name, data, size, flags);
  1469. gfs2_glock_dq(&gh);
  1470. }
  1471. gfs2_holder_uninit(&gh);
  1472. return ret;
  1473. }
  1474. static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
  1475. void *data, size_t size)
  1476. {
  1477. struct inode *inode = dentry->d_inode;
  1478. struct gfs2_inode *ip = GFS2_I(inode);
  1479. struct gfs2_holder gh;
  1480. int ret;
  1481. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
  1482. ret = gfs2_glock_nq(&gh);
  1483. if (ret == 0) {
  1484. ret = generic_getxattr(dentry, name, data, size);
  1485. gfs2_glock_dq(&gh);
  1486. }
  1487. gfs2_holder_uninit(&gh);
  1488. return ret;
  1489. }
  1490. static int gfs2_removexattr(struct dentry *dentry, const char *name)
  1491. {
  1492. struct inode *inode = dentry->d_inode;
  1493. struct gfs2_inode *ip = GFS2_I(inode);
  1494. struct gfs2_holder gh;
  1495. int ret;
  1496. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  1497. ret = gfs2_glock_nq(&gh);
  1498. if (ret == 0) {
  1499. ret = generic_removexattr(dentry, name);
  1500. gfs2_glock_dq(&gh);
  1501. }
  1502. gfs2_holder_uninit(&gh);
  1503. return ret;
  1504. }
  1505. static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  1506. u64 start, u64 len)
  1507. {
  1508. struct gfs2_inode *ip = GFS2_I(inode);
  1509. struct gfs2_holder gh;
  1510. int ret;
  1511. ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
  1512. if (ret)
  1513. return ret;
  1514. mutex_lock(&inode->i_mutex);
  1515. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  1516. if (ret)
  1517. goto out;
  1518. if (gfs2_is_stuffed(ip)) {
  1519. u64 phys = ip->i_no_addr << inode->i_blkbits;
  1520. u64 size = i_size_read(inode);
  1521. u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
  1522. FIEMAP_EXTENT_DATA_INLINE;
  1523. phys += sizeof(struct gfs2_dinode);
  1524. phys += start;
  1525. if (start + len > size)
  1526. len = size - start;
  1527. if (start < size)
  1528. ret = fiemap_fill_next_extent(fieinfo, start, phys,
  1529. len, flags);
  1530. if (ret == 1)
  1531. ret = 0;
  1532. } else {
  1533. ret = __generic_block_fiemap(inode, fieinfo, start, len,
  1534. gfs2_block_map);
  1535. }
  1536. gfs2_glock_dq_uninit(&gh);
  1537. out:
  1538. mutex_unlock(&inode->i_mutex);
  1539. return ret;
  1540. }
  1541. const struct inode_operations gfs2_file_iops = {
  1542. .permission = gfs2_permission,
  1543. .setattr = gfs2_setattr,
  1544. .getattr = gfs2_getattr,
  1545. .setxattr = gfs2_setxattr,
  1546. .getxattr = gfs2_getxattr,
  1547. .listxattr = gfs2_listxattr,
  1548. .removexattr = gfs2_removexattr,
  1549. .fiemap = gfs2_fiemap,
  1550. .get_acl = gfs2_get_acl,
  1551. };
  1552. const struct inode_operations gfs2_dir_iops = {
  1553. .create = gfs2_create,
  1554. .lookup = gfs2_lookup,
  1555. .link = gfs2_link,
  1556. .unlink = gfs2_unlink,
  1557. .symlink = gfs2_symlink,
  1558. .mkdir = gfs2_mkdir,
  1559. .rmdir = gfs2_unlink,
  1560. .mknod = gfs2_mknod,
  1561. .rename = gfs2_rename,
  1562. .permission = gfs2_permission,
  1563. .setattr = gfs2_setattr,
  1564. .getattr = gfs2_getattr,
  1565. .setxattr = gfs2_setxattr,
  1566. .getxattr = gfs2_getxattr,
  1567. .listxattr = gfs2_listxattr,
  1568. .removexattr = gfs2_removexattr,
  1569. .fiemap = gfs2_fiemap,
  1570. .get_acl = gfs2_get_acl,
  1571. };
  1572. const struct inode_operations gfs2_symlink_iops = {
  1573. .readlink = generic_readlink,
  1574. .follow_link = gfs2_follow_link,
  1575. .put_link = gfs2_put_link,
  1576. .permission = gfs2_permission,
  1577. .setattr = gfs2_setattr,
  1578. .getattr = gfs2_getattr,
  1579. .setxattr = gfs2_setxattr,
  1580. .getxattr = gfs2_getxattr,
  1581. .listxattr = gfs2_listxattr,
  1582. .removexattr = gfs2_removexattr,
  1583. .fiemap = gfs2_fiemap,
  1584. .get_acl = gfs2_get_acl,
  1585. };